0

I want this date to be displayed in GMT "01/01/2100"

Expecting result - "Fri Jan 01 00:00:00 GMT 2100"

Result i am seeing - "Thu Dec 31 19:00:00 EST 2099"

  SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
  sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
  try{
       Date myDate = sdf.parse("01/01/2100");
           System.out.println(myDate);

  }
  catch(Exception e){

  }

I want the result myDate in GMT time not EST

Example 2

  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  sdf.setTimeZone(TimeZone.getTimeZone("gmt"));
  try{
       Date myDate = sdf.parse("2010-05-23 09:01:02");

       SimpleDateFormat df = new SimpleDateFormat("EEE, d MMM yyyy, HH:mm");
      System.out.println("MYDATE: " +df.format(myDate));

  }
  catch(Exception e){

  }

Output: Sun, 23 May 2010, 05:01

Expecting: Sun, 23 May 2010, 09:01

http://goo.gl/uIy9RQ - URL for my project code, run and check the result

Cintu
  • 913
  • 2
  • 16
  • 32
  • 3
    You need to print it through the `SimpleDateFormatter`, not just use `Date.toString()`, which will always use the JVM's default timezone. – Andy Turner Feb 08 '16 at 10:58
  • After this code i need to check if the hours and minutes are 00:00:00 But from the above i got 19:00:00 and the logic failing – Cintu Feb 08 '16 at 11:02
  • Try the accepted answer [here](http://stackoverflow.com/questions/18122608/simpledateformat-parse-loses-timezone) – Raghavendra Feb 08 '16 at 11:03
  • I tried everything check and run my java code here - http://goo.gl/uIy9RQ – Cintu Feb 08 '16 at 11:54

3 Answers3

1

As already stated in the comments you have to apply the date format to the Date. I modified your code to display the myDate

try{
     SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
     sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
     Date myDate = sdf.parse("01/01/2100");
     System.out.println(sdf.format(myDate));
     sdf.setTimeZone(TimeZone.getTimeZone("EST"));
     System.out.println(sdf.format(myDate));
}
catch(Exception e){
     // error handling
}

Output is

01/01/2100
31/12/2099

But do not forget the date still contains information about hours, minutes, seconds etc. In respect to the applied date formatter this information is not shown when running

System.out.println(sdf.format(myDate));

If you need to set some fields of the date, you should make use of the Calendar.

Stefan Freitag
  • 3,578
  • 3
  • 26
  • 33
0

For customizing date format use below

DateFormat df = new SimpleDateFormat("EEE, d MMM yyyy, HH:mm");
String date = df.format(Calendar.getInstance().getTime());


Date format parameter can be any one of the following

"yyMMddHHmmssZ"-------------------- 010704120856-0700
"K:mm a, z" ----------------------- 0:08 PM, PDT
"yyyy.MM.dd G 'at' HH:mm:ss z" ---- 2001.07.04 AD at 12:08:56 PDT
"EEE, d MMM yyyy HH:mm:ss Z"------- Wed, 4 Jul 2001 12:08:56 -0700
"h:mm a" -------------------------- 12:08 PM
"EEE, MMM d, ''yy" ---------------- Wed, Jul 4, '01
"yyyy-MM-dd'T'HH:mm:ss.SSSZ"------- 2001-07-04T12:08:56.235-0700
"hh 'o''clock' a, zzzz" ----------- 12 o'clock PM, Pacific Daylight Time
Sreehari
  • 5,621
  • 2
  • 25
  • 59
0

toString on java.util.Date Is Misleading

As has been stated many times before on Stack Overflow, the toString method of java.util.Date silently applies the JVM’s current default time zone when generating the textual representation of the date-time value. An unfortunate design choice.

The old java.util.Date/.Calendar classes are notoriously troublesome. Avoid them. In Java 8 and later, use the built-in java.time framework. Defined by JSR 310, extended by the ThreeTen-Extra project. See Tutorial.

UTC

Basically UTC is the same as GMT in this context.

java.time

The java.time framework includes the class LocalDate for a date-only value without time-of-day nor time zone.

Note that getting the current date requires a time zone even though no time zone is stored internally. For any given moment the date varies around the world by time zone, with a new day dawning earlier in the east.

Use a proper time zone name. Never use 3-4 letter codes such as EST as they are neither standardized nor unique.

ZoneId zoneId = ZoneId.of( "America/Montreal" );
LocalDate today = LocalDate.now( zoneId );

The LocalDate::toString method generates a string in standard ISO 8601 format: 2016-01-23.

Joda-Time

If Java 8 technology is not available to you, add the very successful Joda-Time library. Joda-Time inspired the java.time.

In this case the code is similar that seen above.

DateTimeZone zone = DateTimeZone.forID( "America/Montreal" );
LocalDate localDate = new LocalDate( zone );
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154