Getting CST and EST time in java
I get the same time for EST and CST. Please find the code below.
Calendar.getInstance(TimeZone.getTimeZone("EST"));
and
Calendar.getInstance(TimeZone.getTimeZone("CST"));
both return the same time.
Please help me to solve this problem.
The reason you are getting the same time is because EST returns standard time and CST returns daytime.
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 result:
04:55:839 EDT
03:55:839 EST
03:55:839 CDT
The EST time does not match the correct time because it is actually 04:55 now, so it US/Eastern
will give you the correct time (EDT). As a general rule, I would recommend to always use formats US/Eastern
and US/Central
for security reasons.