开发者

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

开发者 https://www.devze.com 2023-04-01 16:34 出处:网络
Assuming a given Timestamp timestamp: I am using Joda Time to build the first day of the month: DateTime dateTime = new DateTime(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

精彩评论

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