I wish to get the exact date of first day of last month at 00:00:00Z
.
So, here is my current solution:
public static String getStartingDateAndTimeOfLastMonth() {
int dayOfCurrentMonth = ZonedDateTime.now().getDayOfMonth();
return ZonedDateTime.now()
.minusDays(dayOfCurrentMonth - 1)
.minusMonths(1)
.format(DateTimeFormatter.ISO_INSTANT);
}
When i call the moethod:
String startDate = CustomUtilsFunctions.getStartingDateAndTimeOfLastMonth();
System.out.println("startDate: " + startDate);
The output of current solution is:
startDate: 2021-05-01T07:22:10.389Z
As you can see, the time of the output is 07:22:10.389Z
but, I don't know the easiest way to turn it to 00:00:00:000Z
So the desired output for is:
startDate: 2021-05-01T00:00:00.000Z
Point:
I know, i can extract the hour, minutes and seconds and millis and then use the minus()
, but I believe there must be an easier solution.
truncatedTo(ChronoUnit.DAYS)
and maybe prependwithZoneSameInstant(ZoneId.of("UTC"))
.