Avatar of srikotesh
srikotesh
 asked on

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

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,
Java

Avatar of undefined
Last Comment
CPColin

8/22/2022 - Mon
CPColin

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.
srikotesh

ASKER
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

CPColin

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.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
srikotesh

ASKER
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)?
srikotesh

ASKER
In my local machine for utc timezone it is giving output as
Wed Sep 14 18:00:00 IST 2016
ASKER CERTIFIED SOLUTION
CPColin

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
srikotesh

ASKER
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?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
srikotesh

ASKER
why it is not printing same time in my local machine?
CPColin

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.