I would like to set java.util.Date object to java.util.Calendar object to change Date object's year as below:
TimezoneId is equal to "Europe/Istanbul".And year is 2018.
Date object is 1969-12-31 22:00:00.0 (1969-12-31T22:00:00.000Z) and zoneinfo is below:
"sun.util.calendar.ZoneInfo[id="UTC",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null]"
Europe/Istanbul timezone is UTC+3 but when I run code with this date object new GregorianCalendar changes time of date object as -1 hour.Something is working wrongly and I could not find why this not convert UTC to UTC+3.Returned Date object is equals with Sun Dec 31 21:00:00 UTC 2017.UTC-1 is not correct for Europe/Istanbul.How can I fix this ?
public static Date setYearOfDate(Date date, int year, String timeZoneId)
{
Calendar customCalendar = new GregorianCalendar(TimeZone.getTimeZone(timeZoneId));
customCalendar.setTime(date);
customCalendar.set(Calendar.YEAR, year);
return customCalendar.getTime();
}
Note:customCalendar zoneinfo is below: "sun.util.calendar.ZoneInfo[id="Europe/Istanbul",offset=10800000,dstSavings=0,useDaylight=false,transitions=130,lastRule=null]"
Date
andCalendar
. Those classes are poorly designed and long outdated. If you really want a date without time of day, use useLocalDate
from java.time, the modern Java date and time API. If you want to include time and time zone, useZonedDateTime
from the same API. – Ole V.V.