I need to change Calendar timezone offset
I tried many thing;
1-
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(TimeUnit.HOURS.convert(rawOffset, TimeUnit.MILLISECONDS) +
System.currentTimeMillis());
2-
TimeZone timezone = new SimpleTimeZone();
timezone.setRawOffset(rawOffset);
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(timezone);
Our API returning +1.0 timezone (Berlin) But I can't change Calender timezone offset. How can change?
Don’t mingle with raw offsets. Java knows the offset for Berlin better than you. And consider not using Calendar
. That class has a number of design problems and is considered long outdated.
ZonedDateTime zdtBerlin = ZonedDateTime.now(ZoneId.of("Europe/Berlin"));
System.out.println("Zeit in Berlin: " + zdtBerlin);
When I ran this snippet just now, I got this output:
Zeit in Berlin: 2019-02-08T19:23:00.275+01:00[Europe/Berlin]
As the name says, a ZonedDateTime
has a time zone in it. In this case Europe/Berlin. It’s the modern replacement for GregorianCalendar
(the most common concrete subclass of the abstract Calendar
class).
Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6.
org.threeten.bp
with subpackages.Snippet 1-: You are setting the Calendar
’s time to an hour into the future. You are not setting the time zone.
Snippet 2-: First SimpleTimeZone
hasn’t got a no-arg constructor, so new SimpleTimeZone()
won’t work. Next, its setRawOffset
expects an offset in milliseconds. If rawOffset
is 1, you are setting an offset of 1 millisecond.
java.time
.java.time
was first described.java.time
to Java 6 and 7 (ThreeTen for JSR-310).