java - Converting date from UTC to CET -


this question has answer here:

i convert date given in utc format date in cet format.

the problem need add or subtract hours accordingly.

example:

date = "2015-07-31 01:14:05" 

i convert german date (adding 2 hours):

2015-07-31 03:14:05"  

my code:

private static long convertdatefromutctocet(string publicationdate) {     //"2015-07-31 01:14:05"      simpledateformat simpledateformat = new simpledateformat("yyyy-mm-dd hh:mm:ss");     //simpledateformat simpledateformat = new simpledateformat("yyyy-mm-dd");     date date = null;     try {         date = simpledateformat.parse(publicationdate);     } catch (parseexception e) {         e.printstacktrace();     }     calendar calendar = calendar.getinstance(timezone.gettimezone("utc"));     calendar.settime(date);     date givendate = calendar.gettime();     system.out.println("original utc date is: " + givendate.tostring());      timezone timezone = timezone.gettimezone("cet");     calendar.settimezone(timezone);     date currentdate = calendar.gettime();     system.out.println("cet date is: " + currentdate.tostring());      long milliseconds = calendar.gettimeinmillis();      return milliseconds; } 

this prints:

original utc date is: sat jan 31 01:14:05 ist 2015 cet date is: sat jan 31 01:14:05 ist 2015 

first of all, pattern string wrong. it's yyyy-mm-dd, not yyyy-mm-dd.

simpledateformat simpledateformat = new simpledateformat("yyyy-mm-dd hh:mm:ss"); 

to parse given time zone, set with:

simpledateformat.settimezone(timezone.gettimezone("utc")); 

Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -