Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting CST and EST time same in java

Tags:

java

I am getting same time for EST and CST.Please find the code below.

Calendar.getInstance(TimeZone.getTimeZone("EST")); and Calendar.getInstance(TimeZone.getTimeZone("CST")); both returns the same time.

Please help me to resolve this issue.

like image 830
Deva Avatar asked Dec 17 '25 18:12

Deva


1 Answers

The reason you get the same time is because EST returns Standard time while CST the Daylight time.

Date today = new Date();
DateFormat df = new SimpleDateFormat("HH:mm:SS z");
df.setTimeZone(TimeZone.getTimeZone("US/Eastern"));
String time = df.format(today);
System.out.println(time);
df.setTimeZone(TimeZone.getTimeZone("EST"));
time = df.format(today);
System.out.println(time);
df.setTimeZone(TimeZone.getTimeZone("CST"));
time = df.format(today);
System.out.println(time);

and this is the output:

04:55:839 EDT
03:55:839 EST
03:55:839 CDT

EST time is not the correct time because it is actually 04:55 there right now, so US/Eastern will give you the correct (EDT) time. As a rule of thumb, I would recommend always using the US/Eastern and US/Central formats for safety.

like image 61
gpu3d Avatar answered Dec 19 '25 11:12

gpu3d



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!