简体   繁体   English

转换为java.util.Date时,Joda LocalDateTime IllegalFieldValueException

[英]Joda LocalDateTime IllegalFieldValueException when converting to java.util.Date

I'm using JodaTime for the first time and don't understand what is wrong with this: 我第一次使用JodaTime并且不明白这有什么问题:

LocalDateTime localDateTime = new LocalDateTime(1965, 4, 8, 15, 16, 17, 18);
Date date = localDateTime.toDate();

when I run this I get: 当我运行这个时,我得到:

org.joda.time.IllegalFieldValueException: Value -982 for millisOfSecond must be in the range [0,999]

but if I do this (no millis), I don't get the exception: 但如果我这样做(没有毫米),我不会得到例外:

LocalDateTime localDateTime = new LocalDateTime(1965, 4, 8, 15, 16, 17);
Date date = localDateTime.toDate();

And I don't get the exception with (ie year = 1975): 我没有得到例外(即年= 1975年):

LocalDateTime localDateTime = new LocalDateTime(1975, 4, 8, 15, 16, 17, 18);
Date date = localDateTime.toDate();

What's going on? 这是怎么回事? Are there some restrictions about converting to java.util.Date? 转换为java.util.Date有一些限制吗?

The following is usually right. 以下通常是正确的。 But not this time. 但不是这次。

Always use .getMillis() in Joda - it will give you millisecond time range from Epoch and allow flawless (Java API) Date() construction - like this: new Date(millis) . 始终在Joda中使用 .getMillis() - 它将为您提供Epoch的毫秒时间范围,并允许完美无缺的(Java API) Date()构造 - 如下所示: new Date(millis)

So, summing up: 所以,总结一下:

 
 
 
  
  LocalDateTime localDateTime = new LocalDateTime(1975, 4, 8, 15, 16, 17, 18); Date date = new Date(localDateTime.getLocalMillis());
 
  

This is the solution that actually works: 这是实际工作的解决方案:

 LocalDateTime ldt = new LocalDateTime( 1975, 4, 8, 15, 16, 17, 18 ); DateTime dt = new DateTime( ldt.getYear(), ldt.getMonthOfYear(), ldt.getDayOfMonth(), ldt.getHourOfDay(), ldt.getMinuteOfHour(), ldt.getSecondOfMinute(), ldt.getMillisOfSecond()); Date d = new Date( dt.getMillis() ); 

And 1965 is before Epoch... i don't know how it affects Date and millis, but, yeah, just saying. 并且1965年在Epoch之前...我不知道它如何影响Date和millis,但是,是的,只是说。

  LocalDateTime localDateTime = new LocalDateTime(1965, 4, 8, 15, 16, 17, 18);
  Date date = localDateTime.toDateTime().toDate();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将 java.util.Date 转换为 org.joda.time.LocalDateTime 时出现问题 - Problem converting java.util.Date to org.joda.time.LocalDateTime 在 java.time.LocalDateTime 和 java.util.Date 之间转换 - Converting between java.time.LocalDateTime and java.util.Date 转换为java.util.Date时,将一天2CE的LocalDateTime转换一天 - Converting a LocalDateTime of the year 2CE out by one day when converting to java.util.Date 使用Groovy(或Java)如何将org.joda.time.LocalDateTime转换为java.util.date? - Using Groovy(or Java) how can I convert a org.joda.time.LocalDateTime to a java.util.date? 将Joda LocalDate或java.util.Date转换为LocalDateTime,在开始时间有时间 - Convert Joda LocalDate or java.util.Date to LocalDateTime having time at Start of the Day 使用joda的java.util.Date类 - java.util.Date class using joda 使用ZoneId.systemDefault()将java.util.Date转换为java.time.LocalDateTime - Converting java.util.Date to java.time.LocalDateTime using ZoneId.systemDefault() 将java.sql.Date和java.util.Date转换为org.joda.time.LocalDate - Converting java.sql.Date & java.util.Date to org.joda.time.LocalDate 找不到能够从类型 [java.time.LocalDateTime] 转换为类型 [java.util.Date] 的转换器 - No converter found capable of converting from type [java.time.LocalDateTime] to type [java.util.Date] 将 LocalDate 转换为 java.util.Date 时缺少时间信息 - Time information is missing when converting LocalDate to java.util.Date
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM