Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
504 views
in Technique[技术] by (71.8m points)

android - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...