将日期从UTC转换为CET [英] Converting date from UTC to CET
本文介绍了将日期从UTC转换为CET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将以 UTC
格式给出的日期转换为以 CET
格式给出的日期.
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.
示例:
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"
我的代码:
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;
}
此打印:
Original UTC date is: Sat Jan 31 01:14:05 IST 2015
CET date is: Sat Jan 31 01:14:05 IST 2015
推荐答案
首先,您的模式字符串错误.是 yyyy-MM-dd
,而不是 yyyy-mm-dd
.
First of all, your pattern string is wrong. It's yyyy-MM-dd
, not yyyy-mm-dd
.
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
要解析给定的时区,请设置为:
To parse with a given time zone, set it with:
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
这篇关于将日期从UTC转换为CET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文