Solved

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

Posted on 2016-09-14
9
49 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 1

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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 1

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 1

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 1

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 1

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
V.simple question about Android packages and libraries 1 21
GUI builder for Eclipse? 8 36
Java Loop 6 62
xampp tool 12 50
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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…
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.

809 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