Solved

Is there any chances date time increment by 5hr:30min  while converting string date to date

Posted on 2016-09-14
9
50 Views
Last Modified: 2016-09-22
Hi Experts,

I am facing a issue while converting string date to date.
The time incremented by 5hr 30 mins.

final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String scheduleTime = "2016-09-14 12:30:00";
System.out.println(sdf.parse(scheduleTime));
	    

Open in new window


o/p:Wed Sep 14 12:30:00 IST 2016

Is there any chance the above code moved to production server will it increment by 5hr 30 mins i mean like(Wed Sep 14 18:00:00 IST 2016)?
in my local machine i am getting the same date and time after converted to date format.

Thanks,
0
Comment
Question by:srikotesh
  • 5
  • 4
9 Comments
 
LVL 14

Expert Comment

by:CPColin
ID: 41798293
India Standard Time (IST) is UTC+0530, so it looks like the date is being parsed with the assumption that it's in UTC, then is being output as IST. You'll have to set the time zone on your SimpleDateFormat object or use a date format that includes the time zone.
0
 
LVL 2

Author Comment

by:srikotesh
ID: 41798359
HI,

I am not able to convert to date--> getting parse exception:
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss'Z'");
	    sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
	    String scheduleTime = "2016-09-14 12:30:00";
            System.out.println(sdf.parse(scheduleTime));

Open in new window

0
 
LVL 14

Expert Comment

by:CPColin
ID: 41798365
Take the Z back out of your format string. You might also need to set the time zone to IST, not UTC, for this to work right.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 2

Author Comment

by:srikotesh
ID: 41798377
YES IT IS WORKING NOW WITH BELOW CODE
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("IST"));
String scheduleTime = "2016-09-14 12:30:00";
System.out.println(sdf.parse(scheduleTime));


let suppose i am running this program on utc timezone based server.

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
String scheduleTime = "2016-09-14 12:30:00";
System.out.println(sdf.parse(scheduleTime));

WILL THIS PRINT SAME TIME LIKE (2016-09-14 12:30:00)?
0
 
LVL 2

Author Comment

by:srikotesh
ID: 41798394
In my local machine for utc timezone it is giving output as
Wed Sep 14 18:00:00 IST 2016
0
 
LVL 14

Accepted Solution

by:
CPColin earned 500 total points
ID: 41798416
In order to have maximum control over the time zone, you need to use your SimpleDateFormat objects both while parsing the date and while formatting it.

// This line parses the String into a Date object, then calls Date.toString() on that object.
// This means it won't necessarily use the time zone you set in sdf.
System.out.println(sdf.parse(scheduleTime));

// This would use the time zone you set:
Date date = sdf.parse(scheduleTime);
System.out.println(sdf.format(date));

Open in new window

0
 
LVL 2

Author Comment

by:srikotesh
ID: 41798434
That is fine

           SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	    sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
	    String scheduleTime = "2016-09-14 12:30:00";
        Date date =sdf.parse(scheduleTime);
        System.out.println(date);

Open in new window


will this code print same time as above(2016-09-14 12:30:00) when it is running in utc time zone based server?
0
 
LVL 2

Author Comment

by:srikotesh
ID: 41798438
why it is not printing same time in my local machine?
0
 
LVL 14

Expert Comment

by:CPColin
ID: 41798441
Don't know. It grabs the default time zone from somewhere. I don't know where. That's why you have to specify it, to be sure, both when parsing and when formatting.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Should localization be done inside spring controller 5 32
MySQL  on Tomcat 8 68
Eclipse Help Java EE 5,6,7 Documentation, why not Java EE 8 8 30
hashmap order 17 36
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to implement Singleton Design Pattern in Java.

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question