分析LocalDateTime(Java 8)时无法从TemporalAccessor获取LocalDateTime

分析LocalDateTime(Java 8)时无法从TemporalAccessor获取LocalDateTime,java,datetime,java-8,datetime-format,Java,Datetime,Java 8,Datetime Format,我只是尝试在Java8中将日期字符串转换为DateTime对象。运行以下行时: DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd"); LocalDateTime dt = LocalDateTime.parse("20140218", formatter); 我得到以下错误: Exception in thread "main" java.time.format.DateTimeParseException

我只是尝试在Java8中将日期字符串转换为DateTime对象。运行以下行时:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
LocalDateTime dt = LocalDateTime.parse("20140218", formatter);
我得到以下错误:

Exception in thread "main" java.time.format.DateTimeParseException: 
Text '20140218' could not be parsed: 
Unable to obtain LocalDateTime from TemporalAccessor: 
{},ISO resolved to 2014-02-18 of type java.time.format.Parsed
    at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1918)
    at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1853)
    at java.time.LocalDateTime.parse(LocalDateTime.java:492)

语法与所建议的完全相同,但我得到一个例外。我使用的是
JDK-8u25

,结果证明Java不接受裸日期值作为DateTime。使用LocalDate而不是LocalDateTime解决了以下问题:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
LocalDate dt = LocalDate.parse("20140218", formatter);

这是一条非常不清楚且毫无帮助的错误消息。经过多次尝试和错误后,我发现如果不尝试解析时间,
LocalDateTime
将给出上述错误。通过使用
LocalDate
,它可以正常工作


这方面的文档记录得很差,相关的异常也没有什么帮助。

如果确实需要将日期转换为LocalDateTime对象,可以使用LocalDate.atStartOfDay()。这将在指定日期为您提供一个LocalDateTime对象,并将小时、分钟和秒字段设置为0:

final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
LocalDateTime time = LocalDate.parse("20140218", formatter).atStartOfDay();

如果有人(像我一样)再次阅读本主题,那么正确答案应该是
DateTimeFormatter
定义,例如:

private static DateTimeFormatter DATE_FORMAT =  
            new DateTimeFormatterBuilder().appendPattern("dd/MM/yyyy[ [HH][:mm][:ss][.SSS]]")
            .parseDefaulting(ChronoField.HOUR_OF_DAY, 0)
            .parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0)
            .parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0)
            .toFormatter(); 
如果出现可选字段,则应设置这些字段。其余的代码应该完全相同。

继续扩展..:即使使用
LocalDate
而不是
LocalDateTime
时,我也遇到了同样的问题。问题是我已经使用
和ResolverStyle(ResolverStyle.STRICT)创建了我的
日期时间格式表
,因此我不得不使用日期模式
uummdd
而不是
yyyyMMdd
(即“年”而不是“纪年”)


(此解决方案最初是对Retraphy答案的评论,但我被鼓励将其作为一个独立的答案发布,因为它显然对许多人都很有效。)

如果日期字符串不包含小时、分钟等的任何值,则无法直接将其转换为
LocalDateTime
。您只能将其转换为
LocalDate
,因为字符串表示年、月和日期组件,这样做是正确的

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMdd");
LocalDate ld = LocalDate.parse("20180306", dtf); // 2018-03-06
无论如何,您可以将其转换为
LocalDateTime

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMdd");
LocalDate ld = LocalDate.parse("20180306", dtf);
LocalDateTime ldt = LocalDateTime.of(ld, LocalTime.of(0,0)); // 2018-03-06T00:00

对于任何一个像我一样犯了这个错误的人:

无法从TemporalAccessor获取LocalDateTime:{hourofhampm=0,MinuteOfHour=0}

它来自下面一行:

LocalDateTime.parse(date, DateTimeFormatter.ofPattern("M/d/yy h:mm"));
事实证明,这是因为我在0小时内使用了12小时模式,而不是24小时模式

通过使用大写字母H将小时模式更改为24小时模式可以修复此问题:

LocalDateTime.parse(date, DateTimeFormatter.ofPattern("M/d/yy H:mm"));
试试这个:

DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("MM-dd-yyyy"); 
LocalDate fromLocalDate = LocalDate.parse(fromdstrong textate, dateTimeFormatter);
你可以添加任何你想要的格式。那对我有用

这个很好用

public class DateDemo {
    public static void main(String[] args) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy hh:mm");
        String date = "16-08-2018 12:10";
        LocalDate localDate = LocalDate.parse(date, formatter);
        System.out.println("VALUE="+localDate);

        DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm");
        LocalDateTime parse = LocalDateTime.parse(date, formatter1);
        System.out.println("VALUE1="+parse);
    }
}
输出:

VALUE=2018-08-16
VALUE1=2018-08-16T12:10

我遇到这个问题是因为我的输入字符串中没有年份:

输入字符串:
6月8日星期二晚上10:00
格式化程序:
DateTimeFormatter.of模式(“EEEE,MMMM d'at'h:mm a”,Locale.US)

我知道年份,所以我只是把它加上去:

输入字符串:
2021年6月8日星期二下午6:30

格式化程序:
DateTimeFormatter.of模式(“EEEE,MMMM d'at'h:mm a uuuu”,Locale.US)

更正:设置为当天开始时的任何时间。
LocalDateTime.from
似乎没有必要,因为
atStartOfDay()
已经返回
LocalDateTime
。scala:val-time=LocalDate.parse(“20171220”,DateTimeFormatter.of模式(“yyyyMMdd”)。atStartOfDay.format(DateTimeFormatter.of模式(“yyyy-MM-dd HH:MM:ss”))值得一提的是:即使在使用
LocalDate
而不是
LocalDateTime
时,我也遇到了同样的问题。问题是我使用
创建了
DateTimeFormatter
。使用了ResolverStyle(ResolverStyle.STRICT);
,因此我不得不使用日期模式
uummdd
而不是
yyyyMMdd
(即“年”而不是“纪年”)!@ZeroOne的评论应作为所选答案的一部分。@gourabix我已将我的评论作为一个单独的答案发布,您可以对该答案进行投票。最终它将在投票数上超过已接受的答案…@iulian david的回答在这里更有用,实际上解决了解析可能有或可能没有tim的日期时间的问题这是这种方法的真正通用解决方案:Long getFileTimestamp(字符串文件、模式模式、datetimeformatterdtf、int组);这里有不同的模式和DateTimeFormatter,指定了时间和时间。它可以是静态字段吗?我在Javadoc中没有发现以这种方式创建的实例是线程安全的。记住在调用
appendPattern
后添加
parseDefaulting
。否则它将给出
DateTimeParseException
。when在模式中使用
HH
数小时,我得到了上午小时的00,以确保格式正确。直到我将格式设置为
yyyy-mm-dd
时,此修复程序才对我起作用。原因是我将格式设置为
yyy-mm-dd
“在这个链接@Peru中得到了回答,为什么你使用LocalDateTime而没有时间?这节省了我的时间,谢谢!
VALUE=2018-08-16
VALUE1=2018-08-16T12:10