11

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.

Danielson
  • 2,605
  • 2
  • 28
  • 51
fzansari
  • 131
  • 1
  • 3
  • 12
  • http://stackoverflow.com/questions/11389877/jodatime-how-can-i-know-whether-a-daylight-saving-occurs-within-a-specified-pe – Linga May 11 '13 at 05:46
  • Just a note: California is GMT -7 in standard time and -8 in daylight saving time. (Daylight saving is "on" during the summer (March-ish to October-ish in the northern hemisphere) – Al Sweigart Mar 27 '15 at 06:33

1 Answers1

16

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;
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • Matt, I want to know "given the time or date" if daylight saving is on. Basically the key thing I am looking for is the value of the argument "double timezone" in my code above. I have a date being passed to it. Using the date and the location I want to know if daylight saving is on or off. For eg. After november 2013 my timezone value should be -8 – fzansari May 12 '13 at 03:00
  • Well, you would first need to resolve the time zone from the location. Joda time can't do that for you. You need to use one of the methods [described here](http://stackoverflow.com/q/16086962/634824). – Matt Johnson-Pint May 12 '13 at 04:45
  • I dont think you are getting my question. My location is california and it's static. I have to call an api "int Foo(double x)". The variable x should be (GMT -7) or (GMT -8) based on the date i.e depending on if daylight savings is on or off. I want to know given the date is daylight saving on or not. If it is on I should pass -7 to the api. I am not understanding how any of your answers is solving my problem. – fzansari May 12 '13 at 06:05
  • Please read "TimeZone != Offset" in the [timezone tag wiki](http://stackoverflow.com/tags/timezone/info). If all you have is an offset, then you can't determine if that offset is in DST or not. You need a *time zone*. Since you have coordinates, you can determine the time zone from those. If your location is always in california, then your time zone is always `America/Los_Angeles` and there is no need to look it up. There is also no need to tell Joda whether DST is on or off in that zone. If I somehow still missed the intent of your question, please clarify. – Matt Johnson-Pint May 12 '13 at 14:09
  • Matt, thanks for the update. Your second update kinds of answers my question. The only problem is I have a particular Date on which I want to know if DST is on or not. So based on your 2nd code sample // To just determine if it on Date "mm-dd-yy" DST in Pacific Time or not. DateTimeZone zone = DateTimeZone.forID("America/Los_Angeles"); //Assume date1 = "11-13-2013" boolean isStandardOffset = zone.isStandardOffset(date1); boolean isDaylightOfset = !isStandardOffset; – fzansari May 12 '13 at 18:01
  • Thanks Matt. That solved my problem. I passed date November 4 2013 and March 10 2014. And it correctly shows if daylight saving is on or off on that date. – fzansari May 12 '13 at 18:28
  • Does anyone have an answer for my question: http://stackoverflow.com/questions/26410962/joda-datetime-datetimezone ? – stefan Oct 25 '14 at 13:45
  • you're short an 'f' in that last line of code ;). I couldn't get this to work using `Instant.now()` but that is fairly trivial to overcome. probably just a difference in versions. – Kirby Nov 05 '19 at 17:32