Solved

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

Posted on 2016-09-14
9
39 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 1

Author Comment

by:srikotesh
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 1

Author Comment

by:srikotesh
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
why it is not printing same time in my local machine?
0
 
LVL 14

Expert Comment

by:CPColin
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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.

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now