Pre-Requisite : Java : Date and Time Handling Tutorial
Most of the United States practice of advancing clock during summer month in order to leverage an additional hour of natural light(saving heating power, illumination power, enhancing the mood, and so on).
When clocks change time
In United States Daylight Saving Time begins at 2:00 a.m. on the second Sunday in March and reverts to standard time on the first Sunday in November. In United States each time zone switches clocks at different time.
Daylight Saving Time on time zone
In United States , When Daylight Saving Time are being followed in EST then it’s become EDT. These names just are abbreviation and having different meaning in different continents.
Ex:
- IST : Indian Standard Time
- IST : Irish Standard Time
- IST : Israel Standard Time
For United States (Follow Day Light Saving)
Standard Time | Daylight Savings Time |
CST : Central Standard Time (North America) | CDT : Central Daylight Time |
EST : Eastern Standard Time (North America) | EDT : Eastern Daylight Time |
PST : Pacific Standard Time (North America) | PDT : Pacific Daylight Time |
Complete List of timezone abbreviations
Note : The 3-letter abbreviations should be wholeheartedly avoided in favor of TZDB zone IDs. EST is not time zone such 3-4 letter codes are neither standardized nor unique, and further the confusion over Daylight saving time (DST). Whenever need to set time zone in code in the code always use proper time zone name in the “continent/region” format.
Complete List of time zone with continent/region
Example of Daylight Saving Time with Time Zone
In this example cover all the case for applying Daylight Saving Time on different time zone. Here I have try to apply with abbreviation, continent / region and show with standard time and Daylight saving time.
package datetime; import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.ZoneId; import java.util.Date; import java.util.TimeZone; public class DayLightSavingExample { public static void main(String[] args) { /** * DST = Daylight Saving Time (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) */ SimpleDateFormat sourceFormat = new SimpleDateFormat("MM/dd/yyyy"); SimpleDateFormat targetFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'+|-hh:mm"); String strWithDLS = "03/10/2019"; // MM/DD/YYYY String strWithOutDLS = "03/11/2019"; try { Date dateWithDLS = sourceFormat.parse(strWithDLS); Date dateWithOutDLS = sourceFormat.parse(strWithOutDLS); System.out.println("Default Timezone with Day Light Saving :" + targetFormat.format(dateWithDLS)); System.out.println("Default Timezone without Day Light Saving :" + targetFormat.format(dateWithOutDLS)); /** * Set time zone with EST (No difference in time) because 3 letter * time zone not consider DST (Day light saving time) */ sourceFormat.setTimeZone(TimeZone.getTimeZone("EST")); dateWithDLS = sourceFormat.parse(strWithDLS); dateWithOutDLS = sourceFormat.parse(strWithOutDLS); System.out.println("EST Timezone with Day Light Saving :" + targetFormat.format(dateWithDLS)); System.out.println("EST Timezone without Day Light Saving :" + targetFormat.format(dateWithOutDLS)); /** * Set time zone with America/New_York (1 hour difference in time) * when you need to use day light saving always use with * continent/region format. */ sourceFormat.setTimeZone(TimeZone.getTimeZone(ZoneId.of("America/New_York"))); dateWithDLS = sourceFormat.parse(strWithDLS); dateWithOutDLS = sourceFormat.parse(strWithOutDLS); System.out.println("America/New_York Timezone with Day Light Saving :" + targetFormat.format(dateWithDLS)); System.out.println( "America/New_York Timezone without Day Light Saving :" + targetFormat.format(dateWithOutDLS)); } catch (ParseException ex) { ex.printStackTrace(); } } }
Output :
Default Timezone with Day Light Saving :2019-03-10T00:00:00Z+|-12:00
Default Timezone without Day Light Saving :2019-03-11T00:00:00Z+|-12:00
EST Timezone with Day Light Saving :2019-03-10T10:30:00Z+|-10:30
EST Timezone without Day Light Saving :2019-03-11T10:30:00Z+|-10:30
America/New_York Timezone with Day Light Saving :2019-03-10T10:30:00Z+|-10:30
America/New_York Timezone without Day Light Saving :2019-03-11T09:30:00Z+|-09:30
Here i have tried to cover all the points as developer prospects. If you know more ways to handle it and any issues related to date and time please suggest in comments.
You must log in to post a comment.