Java 字符串日期到本地日期的转换
我的应用程序提供了一个日期格式,我正在尝试将该字符串转换为LocalDateJava 字符串日期到本地日期的转换,java,string,java-time,localdate,datetime-parsing,Java,String,Java Time,Localdate,Datetime Parsing,我的应用程序提供了一个日期格式,我正在尝试将该字符串转换为LocalDate import java.time.LocalDate; import java.time.format.DateTimeFormatter; public class DateStringUtil { /** * @param args */ public static void main(String[] args) { // TODO Auto-generat
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class DateStringUtil {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd/MM/yyyy hh:mm");
//SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm");
String date ="26 Aug 2019 09:46:09,469";
LocalDate ld = LocalDate.parse(date, dtf);
}
}
我犯了一个错误。你知道怎么解决吗
Exception in thread "main" java.time.format.DateTimeParseException: Text '26 Aug 2019 09:46:09,469' could not be parsed at index 2
at java.time.format.DateTimeFormatter.parseResolved0(Unknown Source)
at java.time.format.DateTimeFormatter.parse(Unknown Source)
at java.time.LocalDate.parse(Unknown Source)
at src.DateStringUtil.main(DateStringUtil.java:24)
您输入的日期应与模式(“dd/MM/yyyy hh:MM”)中指定的格式相同代码>
您可能需要参考此格式。您输入的日期应与模式(“dd/MM/yyyy hh:MM”)中指定的日期时间格式相同代码>
您可能需要参考此格式。您的日期时间格式与输入日期不匹配:
- 您的格式要求日期为
模式:dd/MM/yyyy hh:MM
26/08/2019 09:46
- 您的日期字符串是
dd-MMM-yyyy-hh:mm:ss,SSS模式中的日期:
2019年8月26日09:46:09469
DateTimeFormatter
文档提供了有关您可以使用的模式的更多信息:
编辑:如果希望以不同的格式输出本地日期,请创建第二个DateTimeFormatter
,并将其传递给方法
import java.time.LocalDateTime;
导入java.time.format.DateTimeFormatter;
公共类DateStringUtil{
公共静态void main(字符串[]args){
DateTimeFormatter inputDtf=模式的DateTimeFormatter.of(“dd-MMM-yyyy-HH:mm:ss,SSS”);
字符串日期=“2019年8月26日09:46:09469”;
LocalDateTime ld=LocalDateTime.parse(日期,inputDtf);
DateTimeFormatter outputDtf=模式的DateTimeFormatter.of(“dd/MM/yyyy HH:MM”);
字符串outputDate=ld.format(outputDtf);
}
}
您的日期时间格式与输入日期不匹配:
- 您的格式要求日期为
模式:dd/MM/yyyy hh:MM
26/08/2019 09:46
- 您的日期字符串是
dd-MMM-yyyy-hh:mm:ss,SSS模式中的日期:
2019年8月26日09:46:09469
DateTimeFormatter
文档提供了有关您可以使用的模式的更多信息:
编辑:如果希望以不同的格式输出本地日期,请创建第二个DateTimeFormatter
,并将其传递给方法
import java.time.LocalDateTime;
导入java.time.format.DateTimeFormatter;
公共类DateStringUtil{
公共静态void main(字符串[]args){
DateTimeFormatter inputDtf=模式的DateTimeFormatter.of(“dd-MMM-yyyy-HH:mm:ss,SSS”);
字符串日期=“2019年8月26日09:46:09469”;
LocalDateTime ld=LocalDateTime.parse(日期,inputDtf);
DateTimeFormatter outputDtf=模式的DateTimeFormatter.of(“dd/MM/yyyy HH:MM”);
字符串outputDate=ld.format(outputDtf);
}
}
您的日期格式字符串错误:它应该是“26/08/2019 09:46”
。查看文档,找出您需要的模式:如果我的输入日期格式不同,并且我希望输出的格式不同,那么必须使用SimpleDataFormat?只需创建第二个DateTimeFormatter
,并在LocalDate
上调用它即可(更新答案)。您的日期格式字符串错误:它应该是“26/08/2019 09:46”
。查看文档,找出您需要的模式:如果我的输入日期格式不同,并且我希望以不同的格式输出,则必须使用SimpleDateFormat?只需创建第二个DateTimeFormatter
,并在您的LocalDate
(更新的答案)上调用它。通常,我们会更改代码以匹配输入数据,而不是相反。@BillyBrown这只是一个指出问题所在的示例,谢谢你的否决。如果你只是指出问题所在,那么请使用注释。谢谢,请使用+10,很高兴我们通常会更改代码以匹配输入数据,而不是相反。@BillyBrown这只是一个指出问题所在的示例,谢谢你的否决投票。如果你只是指出问题所在,那么就用一条评论。谢谢,拿+10并高兴一点,最后一行我在线程“main”中遇到异常java.time.temporal.UnsupportedTemporalTypeException:不支持的字段:ClockHourOfAmPm。ld将于2019-08-26发布,没有时间成分。那么,如果我们尝试将该日期格式化为hh:mm:ss,它将如何工作?还有其他方法吗?@JAVA\u CAT使用LocalDateTime
而不是LocalDate
。我还将hh
更改为hh
,因为后者是24小时时钟,而前者是12小时时钟。最后一行我在线程“main”java.time.temporal.UnsupportedTemporalTypeException:Unsupported字段:ClockHourOfAmPm中收到异常。ld将于2019-08-26发布,没有时间成分。那么,如果我们尝试将该日期格式化为hh:mm:ss,它将如何工作?还有其他方法吗?@JAVA\u CAT使用LocalDateTime
而不是LocalDate
。我还将hh
更改为hh
,因为后者是24小时制,而前者是12小时制。
public static void main(String args[]) {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd/MM/yyyy hh:mm");
//SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm");
String date ="26/08/2019 09:46";
LocalDate ld = LocalDate.parse(date, dtf);
}