3

I am trying to read the system date in CST time zone using Java. I tried the below code but whenever I use formatter.parse() it is returning time in EST time zone.

private Date getTodayInCST() {
    Calendar currentdate = Calendar.getInstance();
    DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
    TimeZone obj = TimeZone.getTimeZone("CST");
    formatter.setTimeZone(obj);

    String today = formatter.format(currentdate.getTime());
    try {
        return formatter.parse(today);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return null;
}
wjl
  • 7,143
  • 1
  • 30
  • 49
  • 3
    A Date does not have time zone information so what you are trying to do is not possible. You can *print* a Date with a given time zone setting though. – assylias Sep 10 '14 at 14:20
  • possible duplicate of [How to set time zone of a java.util.Date?](http://stackoverflow.com/questions/2891361/how-to-set-time-zone-of-a-java-util-date) – Basil Bourque Sep 10 '14 at 15:46
  • It may not be returning the date in EST. We are now currently in C***D***T, in which times are 1 hour later than they are in CST. If the time you are reading is in CDT, the output would look like EST to you. – Jim Garrison Sep 10 '14 at 15:59
  • FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Jun 02 '18 at 02:23

3 Answers3

7

tl;dr

ZonedDateTime.now( 
    ZoneId.of( "America/Chicago" )
)

Details

I am trying to read the system date in CST time zone

By “CST”, do you mean Central Standard Time in North America, or China Standard Time?

Specify a proper time zone name in the format of continent/region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. Never use the 3-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique as seen above with CST.

ZoneId z = ZoneId.of( "America/Chicago" ) ; 

java.time

You are using troublesome old date-time classes that are now legacy. Supplanted by the java.time classes.

Get the current moment in UTC. The Instant class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction).

Instant instant = Instant.now() ;

2018-02-26T05:45:24.213610Z

Adjust into another time zone. Same moment, same point on the timeline, different wall-clock time.

ZonedDateTime zdt = instant.atZone( z ) ;

zdt.toString(): 2018-02-25T23:45:24.213610-06:00[America/Chicago]

The above strings are in standard ISO 8601 format. The ZonedDateTime class extends that standard wisely to append the name of the zone in square brackets.

If you want to generate String objects in other formats, use DateTimeFormatter.

DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd-MM-uuuu HH:mm:ss" ) ;
String output = zdt.format( f ) ;

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Using a JDBC driver compliant with JDBC 4.2 or later, you may exchange java.time objects directly with your database. No need for strings nor java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
4

java.util.Date objects do not contain any timezone information by themselves - you cannot set the timezone on a Date object. The only thing that a Date object contains is a number of milliseconds since the "epoch" - 1 January 1970, 00:00:00 UTC.

If you want to set timezone try it this way

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
format.setTimeZone(TimeZone.getTimeZone("CST"));
System.out.println(format.format(new Date()));
SparkOn
  • 8,806
  • 4
  • 29
  • 34
1

If want to the code to provide the current time considering the daylight saving adjustment from CST to CDT or vice versa ,you can use the "CST6CDT" timezone. in place of "CST" in SimpleDateFormat.

SimpleDateFormat cstCdtFormat=new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
cstCdtFormat.setTimeZone(TimeZone.getTimeZone("CST6CDT"));
System.out.println(cstCdtFormat.format(new Date()));
udyan
  • 141
  • 5