Java 使用DateTimeFormatter和SimpleDataFormat检索格式化日期获取错误
这是我的输入日期:Java 使用DateTimeFormatter和SimpleDataFormat检索格式化日期获取错误,java,android,datetime-format,simpledateformat,datetimeformatter,Java,Android,Datetime Format,Simpledateformat,Datetimeformatter,这是我的输入日期: String myDate = "2010-02-19"; 我希望从上述日期获得如下输出: Friday, 19-Feb-2010 我尝试了很多方法来获得精确的输出,但还没有成功 以下是我尝试使用DateTimeFormatter和SimpleDateFormat的代码: button.setOnClickListener(new View.OnClickListener() { @Override public voi
String myDate = "2010-02-19";
我希望从上述日期获得如下输出:
Friday, 19-Feb-2010
我尝试了很多方法来获得精确的输出,但还没有成功
以下是我尝试使用DateTimeFormatter和SimpleDateFormat的代码:
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String myDate = "2010-02-19";
String strDateTimeFormatter, strSimpleDateFormat;
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("EEEE, dd-MMM-yyyy", Locale.US);
LocalDate localDate = LocalDate.parse(myDate, dateTimeFormatter);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEEE, dd-MMM-yyyy");
strSimpleDateFormat = simpleDateFormat.format(myDate);
strDateTimeFormatter = String.valueOf(localDate);
Log.d("TAG", "DateTimeFormatter: "+strDateTimeFormatter);
Log.d("TAG", "SimpleDateFormat: "+strSimpleDateFormat);
}
});
我从以下行获取java.time.format.DateTimeParseException:无法在索引0处解析文本“2010-02-19”:
LocalDate localDate = LocalDate.parse(myDate, dateTimeFormatter);
如何获得我想要的实际输出?但还没有成功。
奇怪的是,您尝试的输出显示了什么?ParseException,因为YYYY-MM-dd
无法用EEEE,dd-MMM-YYYY
解析您正在使用的是java.time、现代java日期和时间API及其DateTimeFormatter
,那很好。然后不要试图涉及到SimpleDataFormat
,因为它是一个麻烦且过时的类。不要解析格式化程序,因为您正在解析默认格式的字符串。只是LocalDate LocalDate=LocalDate.parse(myDate)代码>。之后,localDate.format(dateTimeFormatter)
将为您提供2010年2月19日星期五
。返回与myDate相同的内容。@Ole V.V。