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();
精彩评论