Gis*_*nas 0 java time jodatime
我试着用这个:
secondsFromMidnight = Seconds.secondsBetween(localDateTime.toLocalDate(),
localDateTime).getSeconds();
Run Code Online (Sandbox Code Playgroud)
但它引发了一个例外(参见下文).我认为这是一个很好的方法,但我还没有成功地适应我的情况.如果我写这个:
DateTime dateTimeFromMidnight = new DateMidnight(localDateTime.getChronology()).toDateTime();
Duration duration = new Duration(dateTimeFromMidnight, localDateTime.toDateTime());
Run Code Online (Sandbox Code Playgroud)
它与时区混淆(我少了1小时).
如果您的解决方案也可轻松适应数小时,分钟等,那么这是一个优势.
我绝对需要使用LocalDateTime作为输入类型,请不要在其他类中发布解决方案.
Exception in thread "main" java.lang.IllegalArgumentException: ReadablePartial objects must have the same set of fields
at org.joda.time.base.BaseSingleFieldPeriod.between(BaseSingleFieldPeriod.java:92)
at org.joda.time.Seconds.secondsBetween(Seconds.java:124)
Run Code Online (Sandbox Code Playgroud)
如果您想知道一天中的时间,从午夜开始的几秒钟内,最简单的方法是使用getMillisOfDay():
int secondsOfDay = localDateTime.getMillisOfDay() / 1000;
Run Code Online (Sandbox Code Playgroud)
(DateTimeConstants.MILLIS_PER_SECOND如果你真的想使用 - 在这种情况下我认为"每秒1000毫秒"的知识很容易.)
当然,您可以使用相同的方法分钟和小时.
如果你真的想要它作为一个时期,你可以用来在同一天的午夜LocalDateTime.withMillisOfDay(0)获得一个LocalDateTime,例如
secondsFromMidnight = Seconds.secondsBetween(localDateTime.withMillisOfDay(0),
localDateTime)
.getSeconds();
Run Code Online (Sandbox Code Playgroud)
...但我个人可能只是使用getMillisOfDay.
| 归档时间: |
|
| 查看次数: |
2019 次 |
| 最近记录: |