我使用的是带有Apache格式的LGoodDatePicker和NetbeansIDE 12.2 (https://github.com/LGoodDatePicker/LGoodDatePicker),并且需要以YYYY DD格式获取日期。我用的是这个代码:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String date = sdf.format(datePicker1.getDate());但我知道这个错误:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Cannot format given Object as a Date有什么建议吗?谢谢。
发布于 2021-03-18 14:33:33
这个DatePicker的方法DatePicker返回一个java.time.LocalDate,而不是一个java.util.Date。这实际上是错误消息告诉您的,它期望得到一个java.util.Date,但得到了其他信息。
这意味着您不应该尝试使用java.text.SimpleDateFormat来格式化它,在这里使用一个java.time.format.DateTimeFormatter:
String date = datePicker1.getDate().format(DateTimeFormatter.ISO_LOCAL_DATE);或使用DateTimeFormatter的方法DateTimeFormatter定义自定义模式。
String date = datePicker1.getDate().format(DateTimeFormatter.ofPattern("uuuu-MM-dd");在这种情况下,您甚至可以使用LocalDate的LocalDate方法以获得所需格式的String:
String date = datePicker1.getDate().toString();https://stackoverflow.com/questions/66692801
复制相似问题