开发者

How to build first and last of month as timestamp, with given timestamp?

Assuming a given Timestamp timestamp:

I am using Joda Time to build the first day of the month:

DateTime dateTime = new DateTime(timestamp);    
MutableDateTime firstOfMonth = dateTime.toMutableDateTime();
firstOfMonth.setDayOfMonth(1);
firstOfMonth.setTime(0, 0, 0, 0);

and the last day of the month:

MutableDateTime lastOfMonth = firstOfMonth.toMutableDateTime();
lastOfMonth.addMonths(1);
lastOfMonth.addMillis(-1);

But I wond开发者_如何学Cered that calculating firstOfMonth and lastOfMonth this needs so much code. Is there a cleaner way?


I found this in the documentation

DateTime dt = ...
DateTime firstDayOfMonth = dt.dayOfMonth().withMinimumValue();
DateTime lastDayOfMonth = dt.dayOfMonth().withMaximumValue();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜