Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jodatime how to know if daylight savings is on

I have an API that needs the timezone. For eg. if I am in california, I need to pass -7 to it when daylight savings is on (California , PDT is GMT - 7) and -8 to it when daylight saving is off. But I am not able to figure out a way of knowing whether on the current date, daylight saving is on or off.

Date date1 = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(date1);
double[] coords = db.getCoords(id1);
double latitude = coords[0];
double longitude = coords[1];
double timezone = -7; /* For Pacific daylight time (GMT - 7)*/

ArrayList<String> Times = Class.foo(cal, latitude,
longitude, timezone);

I have installed JodaTime and even there I cannot find a way. Please suggest if native java or jodatime, either have a way of doing this.

like image 337
fzansari Avatar asked May 11 '13 05:05

fzansari


People also ask

How do you know if daylight savings is on or off?

Most of the United States begins Daylight Saving Time at 2:00 a.m. on the second Sunday in March and reverts to standard time on the first Sunday in November. In the U.S., each time zone switches at a different time. In the European Union, Summer Time begins and ends at 1:00 a.m. Universal Time (Greenwich Mean Time).

How do you show daylight savings time?

“Spring forward, fall back” is one of the little sayings used to remember which way to set your watch. You set your clock forward one hour in the spring when DST starts (= lose 1 hour), and back one hour when DST ends in the fall (= regain 1 hour).

What is a DST indicator on clock?

Daylight Saving Time (DST) is the practice of turning the clock ahead as warmer weather approaches and back as it becomes colder again.

How does daylight Savibgs time work?

Daylight Saving Time begins on Sunday, March 13, 2022 at 2:00 A.M. On Saturday night, clocks are set forward one hour (i.e., losing one hour) to “spring forward.” Daylight Saving Time ends on Sunday, November 6, 2022, at 2:00 A.M. On Saturday night, clocks are set back one hour (i.e., gaining one hour) to “fall back.”


1 Answers

When you create a DateTime with JodaTime, you don't need to pass an offset. Instead, pass the time zone. It will take care of determining the correct offset, including consideration for DST.

// First get a DateTimeZone using the zone name
DateTimeZone zone = DateTimeZone.forID("America/Los_Angeles");

// Then get the current time in that zone.
DateTime dt = new DateTime(zone);

// Or if you prefer to be more explicit, this syntax is equivalent.
DateTime dt = DateTime.now(zone);

UPDATE

I'm still not sure exactly what you are asking, but perhaps you are looking for one of these:

// To get the current Pacific Time offset
DateTimeZone zone = DateTimeZone.forID("America/Los_Angeles");
int currentOffsetMilliseconds = zone.getOffset(Instant.now());
int currentOffsetHours = currentOffsetMilliseconds / (60 * 60 * 1000);


// To just determine if it is currently DST in Pacific Time or not.
DateTimeZone zone = DateTimeZone.forID("America/Los_Angeles");
boolean isStandardOffset = zone.isStandardOffset(Instant.now());
boolean isDaylightOffset = !isStandardOffset;
like image 106
Matt Johnson-Pint Avatar answered Oct 17 '22 08:10

Matt Johnson-Pint