如何在Java中解析任意的日期时间格式为 yyyy-MM-dd。

huangapple go评论82阅读模式
英文:

How to Parse Any Datetime format to yyyy-MM-dd in java

问题

以下是翻译好的内容:

我有一个动态填充的日期字段,我需要将该字段格式化为 yyyy-MM-dd 格式。

对于格式为 1994-08-01 14:37:44 的输入日期,这会导致异常:

java.time.format.DateTimeParseException: 无法解析文本 '1994-08-01 14:37:44',在索引 10 处发现未解析的文本

这是我尝试的许多其他方法之一:LocalDateTime.parse("1994-08-01 14:37:44", DateTimeFormatter.ofPattern("yyyy-MM-dd"));

有没有办法将所有日期/日期时间转换为 yyyy-MM-dd 格式?

请帮忙,谢谢。

英文:

I have a date field that is populated dynamically, and I need that field in the format yyyy-MM-dd

For a input date of format 1994-08-01 14:37:44 this is giving a Exception

java.time.format.DateTimeParseException: Text '1994-08-01 14:37:44' could not be parsed, unparsed text found at index 10

This is one of the many other ways I tried LocalDateTime.parse("1994-08-01 14:37:44",DateTimeFormatter.ofPattern(yyyy-MM-dd));

Is there a way to convert all date/datetime to yyyy-MM-dd format?

please help

Thanks

答案1

得分: 1

用这种方式尝试一下。你可以提取LocalDate部分。

LocalDate ldt = LocalDateTime.parse("1994-08-01 14:37:44",
                DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        
System.out.println(ldt.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));

输出

1994-08-01
英文:

Try it like this. You can extract the LocalDate part.

LocalDate ldt = LocalDateTime.parse("1994-08-01 14:37:44",
				DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
		
System.out.println(ldt.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));

Prints

1994-08-01

答案2

得分: 1

你有一个日期和时间组件,但你只使用了日期格式来解析它为LocalDateTime值,这会失败,因为LocalDateTime需要时间组件才能正常工作。

首先解析完整的文本:

String input = "1994-08-01 14:37:44";
LocalDateTime ldt = LocalDateTime.parse(input, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

然后使用DateTimeFormatter按你想要的方式进行格式化:

String formatted = DateTimeFormatter.ofPattern("yyyy-MM-dd").format(ldt);
System.out.println(formatted);

打印结果为:

1994-08-01

根据你的需求,你还可以将LocalDateTime值转换为LocalDate并进行格式化,这会得到相同的结果,但你可能会在其他情况下需要LocalDate,谁知道呢...

String formatted = ldt.toLocalDate().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
英文:

You have a date and time component but you're only using a date format to parse it to a LocalDateTime value, this will fail because LocalDateTime needs the time component in order to work

Start by parsing the full text

String input = "1994-08-01 14:37:44";
LocalDateTime ldt = LocalDateTime.parse(input, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

Then use a DateTimeFormatter to format it the way you want

String formatted = DateTimeFormatter.ofPattern("yyyy-MM-dd").format(ldt);
System.out.println(formatted);

which prints

1994-08-01

Depending on your needs, you could also convert the LocalDateTime value to a LocalDate and format it, it's the same result, but you might have need of the LocalDate for other things, who knows...

String formatted = ldt.toLocalDate().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));

huangapple
  • 本文由 发表于 2020年8月15日 08:15:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/63421435.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定