Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading system time in CST time zone using Java

Tags:

java

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;
}
like image 231
manjunatha citragar Avatar asked May 01 '26 13:05

manjunatha citragar


2 Answers

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?

  • Java SE 8, Java SE 9, and later
    • Built-in.
    • Part of the standard Java API with a bundled implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and Java SE 7
    • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android
    • Later versions of Android bundle implementations of the java.time classes.
    • For earlier Android, the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….

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.

like image 78
Basil Bourque Avatar answered May 03 '26 02:05

Basil Bourque


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()));
like image 44
SparkOn Avatar answered May 03 '26 03:05

SparkOn



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!