I would like to convert a date given in the UTC
format to a date in the CET
format.
The problem is that I need to add or subtract hours accordingly.
Example:
Date = "2015-07-31 01:14:05"
I would like to convert it to German date (adding two 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