我之前从未使用过JodaTime,但回答了这个问题,如何在一个月内获得序数平日.
我尝试了它并想出了这个丑陋的代码来取消下面的所有字段:
DateTime startOfMonth =
input.withDayOfMonth(1)
.withHourOfDay(0) // there
.withMinuteOfHour(0) // has got to
.withSecondOfMinute(0) // be a shorter way
.withMillisOfSecond(0); // to do this
Run Code Online (Sandbox Code Playgroud)
Date startOfMonth = DateUtils.truncate(input, Calendar.MONTH);
Run Code Online (Sandbox Code Playgroud)
在JodaTime中,首选的成语是什么?
Sou*_*ink 43
您可以使用roundFloorCopy ()来模仿DateUtils.truncate
Date truncateMonth = DateUtils.truncate(input, Calendar.MONTH);
-> DateTime truncateMonth = input.dayOfMonth().roundFloorCopy();
Date truncateMinute = DateUtils.truncate(input, Calendar.MINUTE);
-> DateTime truncateMinute = input.minuteOfDay().roundFloorCopy();
Date truncateHourOfDay = DateUtils.truncate(input, Calendar.HOUR_OF_DAY);
-> DateTime truncateHourOfDay = input.hourOfDay().roundFloorCopy()
Run Code Online (Sandbox Code Playgroud)
Eri*_*son 41
使用该withMillisOfDay()
方法缩短语法.
DateTime startOfMonth = input.withDayOfMonth(1).withMillisOfDay(0);
Boz*_*zho 21
看看DateMidnight
.
DateTime startOfMonth = new DateTime(new DateMidnight(input.withDayOfMonth(1)));
Run Code Online (Sandbox Code Playgroud)
更新:2013-08-16作者:JodaStephen:Joda-Time的2.3版本弃用了,DateMidnight
因为它是一个非常糟糕的类.
所以使用:
DateTime startOfMonth = input.withDayOfMonth(1).withTimeAtStartOfDay();
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
25464 次 |
最近记录: |