Reputation: 415
final Calendar tentativeStartDate = Calendar.getInstance();
tentativeStartDate.set(Calendar.MILLISECOND, 0);
tentativeStartDate.set(Calendar.SECOND, 0);
tentativeStartDate.set(Calendar.MINUTE, 0);
tentativeStartDate.set(Calendar.HOUR, 0);
tentativeStartDate.set(Calendar.DATE, 1);
tentativeStartDate.add(Calendar.MONTH, -3);
I want start of 3 months prior to current month. eg if today is june 18, i want midnight April 1 at 0 mins/seconds. [ April 1st, 00:00:00 ]
But this logic is not giving me the right answer. Is there a better/different way in Java ?
Upvotes: 0
Views: 839
Reputation: 48444
With Java 8, you can use a fluent idiom as follows:
Zoned
ZonedDateTime.now([your zone ID, e.g. ZoneId.systemDefault()])
// month offset
.minusMonths(2)
// start at day 1
.withDayOfMonth(1)
// change time-of-day to when the day starts
.truncatedTo(ChronoUnit.DAYS)
Local
LocalDate.now([your zone ID, e.g. ZoneId.systemDefault()])
.minusMonths(2)
.withDayOfMonth(1)
Note
I set the month offset to 2
since you want April.
Upvotes: 7