java - Converting date from UTC to CET -
this question has answer here:
- convert date/time given timezone - java 14 answers
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
Post a Comment