I'm doing this
import java.time.LocalDateTime
import java.time.temporal.ChronoUnit
LocalDateTime doy = LocalDateTime.now();
LocalDateTime d = LocalDateTime.of(doy.getYear(), doy.getMonth(), doy.getDayOfMonth(),0,0,0,0);
d.plus(-1,ChronoUnit.MONTHS);
return d.atZone(ZoneOffset.systemDefault()).withZoneSameInstant(ZoneOffset.UTC).format(DateTimeFormatter.ofPattern("YYYY-MM-dd'T'HH:mm:ss.SSSX"))
d
contains 2016-12-28T08:00:00.000Z
I want to get 2015-12-28T00:00:00.000Z
I'm on mac OSX java SE 8 1.8.0_45
also tried java SE 8 1.8.0_72
What am I doing wrong ?
Adding more explanation here
Apparently part of my problem is related to formatting
ZonedDateTime doy = ZonedDateTime.now(ZoneOffset.UTC);
ZonedDateTime d = ZonedDateTime.of(LocalDateTime.of(doy.getYear(), doy.getMonth(), doy.getDayOfMonth(),0,0,0,0),ZoneOffset.UTC);
System.out.println(d);
System.out.println(d.plus(-1,ChronoUnit.MONTHS));
System.out.println(d.minusMonths(1));
System.out.println(d.plus(-1,ChronoUnit.MONTHS).format(DateTimeFormatter.ofPattern("YYYY-MM-dd'T'HH:mm:ss.SSSX")));
output
2016-01-28T00:00Z
2015-12-28T00:00Z
2015-12-28T00:00Z
2016-12-28T00:00:00.000Z
The last one should be 2015-12-28T00:00:00.000Z
System.out.println(d.minusMonths(1).format(DateTimeFormatter.ofPattern("YYYY-MM-dd'T'HH:mm:ss.SSSX")));
Gives the same result
You can do something like this:
LocalDateTime doy = LocalDateTime.now();
LocalDateTime d = doy.minusYears(1);
and then
LocalDateTime d2 = d.minusMonth(1);
return d2;
from API java 8 ( LINK ):
minusYears(long years)
Returns a copy of this LocalDateTime with the specified number of years subtracted.
and d.minusMonth
Returns a copy of this LocalDateTime with the specified number of months subtracted.
YYYY is year value for "year-week" style dates, as in 2006W52. It may be off the year-of-era value by +1 or -1 if the week in question straddles year boundary. Looks like you have chosen the last week of the year.
See http://en.wikipedia.org/wiki/ISO_8601#Week_dates
See also: java.time.LocalDateTime (also ZonedDateTime). Fails to return one year backward when minus 1 month with formatting
I found a solution to my problem
ZonedDateTime doy = ZonedDateTime.now(ZoneOffset.UTC);
ZonedDateTime d = ZonedDateTime.of(LocalDateTime.of(doy.getYear(), doy.getMonth(), doy.getDayOfMonth(),0,0,0,0),ZoneOffset.UTC);
DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
builder.appendValue(ChronoField.YEAR).append(DateTimeFormatter.ofPattern("-MM-dd'T'HH:mm:ss.SSSX"));
DateTimeFormatter formatter = builder.toFormatter();
System.out.println(d.plus(-1,ChronoUnit.MONTHS).format(formatter));
output
2015-12-28T00:00:00.000Z
So I guess Something is wrong with the pattern DateTimeFormatter.ofPattern("YYYY-MM-dd'T'HH:mm:ss.SSSX")
Actually this formatting DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSX")
works
Damn it !!!!
Anyone can explain why my tests were passing yesterday and not anymore today, what's so special about January 28th ??????
Since you want to backward one month , then you should do this for example:
LocalDateTime d = LocalDateTime.of(2016, Month.JANUARY, 28, 0, 0, 0);
System.out.println(d);
System.out.println(d.minusMonths(1));
I think you are looking for d.minusMonths(...). I've never used the "plus" method.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.