-2

I used the below code where I've printed the modified GMT date in String & in Date format, it's giving me two different values.

Date initial = new Date();  
DateFormat dateFormatter = DateFormat.getInstance();  
dateFormatter.setTimeZone (TimeZone.getTimeZone("UTC"));  
String gmtS = dateFormatter.format(initial);             
Date gmt = dateFormatter.parse(gmtS);              
System.out.println("Data type is Date    = " + gmt);
System.out.println("Data type is String "+gmtS);

Output

gtm where value id of type Date = Thu Jul 03 23:15:00 EDT 2014 gmtS where value id of type String = 7/4/14 3:15 AM

But I want to see the value (7/4/14 3:15 AM) as a Date type. Any help is really appreciated.

Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156
Sourav Bag
  • 339
  • 2
  • 4
  • 10
  • possible duplicate of [How can I get the current date and time in UTC or GMT in Java?](http://stackoverflow.com/questions/308683/how-can-i-get-the-current-date-and-time-in-utc-or-gmt-in-java) and [this](http://stackoverflow.com/q/5236052/642706). – Basil Bourque Jul 04 '14 at 06:45

2 Answers2

0

When you output a Date by calling toString() (which is what System.out.println("Data type is Date = " + gmt); does) you will get that Date according to the system time zone, because that is what Date.toString() returns.

Converts this Date object to a String of the form:

dow mon dd hh:mm:ss zzz yyyy

where:

 ...
 zzz is the time zone (and may reflect daylight saving time). Standard time 
     zone abbreviations include those recognized by the method parse. If time 
     zone information is not available, then zzz is empty - that is, it 
     consists of no characters at all.

So, to get the output you expect use your dateFormatter to format it again.

String gmtS = dateFormatter.format(initial);             
Date gmt = dateFormatter.parse(gmtS);              
System.out.println("Data type is Date    = " + dateFormatter.format(gmt));
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • But that'll be again of type String. I need the final date value should be of type Date with value 7/4/14 3:15 AM – Sourav Bag Jul 04 '14 at 03:33
  • @SouravBag Then you will need to change your computer's timezone to UTC. This is how `Date` works (in Java). As an example, what is the date and time where you are right now? Here it is 11:36 PM on July 3, 2014. You'll notice that where you are right now (although you record the time differently) is still contemporaneous with my instant of time. – Elliott Frisch Jul 04 '14 at 03:34
0

tl;dr

Instant.now().toString()

2019-02-07T19:15:29.123456Z

Avoid legacy date-time classes

You are using date-time classes that are terribly troublesome, with many flaws in design.

First, you should know that java.util.Date represents a moment in UTC, always in UTC by definition. But its toString method tells a lie, dynamically applying the JVM’s current default time zone while generating the text representing the moment in the Date object.

java.time

The modern approach uses the java.time classes.

Instant

For a moment in UTC, use Instant. Like java.time.Date it represents a moment always in UTC (but with a finer resolution of nanoseconds versus milliseconds). Indeed, you can convert easily back-and-forth between Date and Instant by using new methods added to the old class.

Unlike toString on Date, the toString method on Instant always tells the truth. The method generates text in standard ISO 8601 format. The T in the middle separates the date portion from the time portion. The Z on the end is short for UTC and is pronounced “Zulu”.

Instant.now().toString(): 2019-01-23T12:34:56.123456789Z

OffsetDateTime

The Instant class is a basic building-block class in java.time, with limited functionality. If you want more flexible formatting, use the OffsetDateTime class with the offset set to UTC.

OffsetDateTime odt = instant.atOffset( ZoneOffset.UTC ) ;

Or skip the Instant class.

OffsetDateTime odt = OffsetDateTime.now( ZoneOffset.UTC ) ;

To generate text representing the value of the OffsetDateTime object, use the DateTimeFormatter class. Search Stack Overflow as this has been covered many many times already.


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.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for 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