Tuesday, February 17, 2015

Is in Daylight Saving Time (DST)? for given date and timezone

Daylight Saving Time (DST) is the practice of turning the clock ahead as warmer weather approaches and back as it becomes colder again so that people will have one more hour of daylight in the afternoon and evening during the warmer season of the year. i.e In the spring when DST starts (= lose one hour), and back one hour when DST ends in the fall (= regain one hour) In java we can find the given date and timezone is in DST. sample code
import java.util.Calendar;
import java.util.Date;
import java.util.SimpleTimeZone;
import java.util.TimeZone;
/**
*
* @author Rivet Systems
*/
public class FindDST {
public static void main(String[] args) {
Calendar time = Calendar.getInstance();
time.set(2014, 11, 1, 6, 00);
if (isDayLightSaving("CET", time.getTime())) {
System.out.println("Its in DST");
} else {
System.out.println("Its NOT in DST");
}
}
public static boolean isDayLightSaving(String timezone, Date dateStr) {
boolean isDST = false;
TimeZone iTimezone = TimeZone.getTimeZone(timezone);
System.out.println("Timezone : " + iTimezone.getDisplayName());
System.out.println("dateStr : " + dateStr);
SimpleTimeZone stz = new SimpleTimeZone(iTimezone.getRawOffset(),
timezone,
Calendar.MARCH, 8, -Calendar.SUNDAY,
2 * 60 * 60 * 1000,
Calendar.NOVEMBER, 1, -Calendar.SUNDAY,
2 * 60 * 60 * 1000, 60 * 60 * 1000);
if (stz.inDaylightTime(dateStr)) {
return true;
}
return false;
}
}
view raw FindDST hosted with ❤ by GitHub

Related Posts:

0 comments:

Post a Comment