Solved

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

Posted on 2016-09-14
9
54 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This video teaches viewers about errors in exception handling.

717 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