gpt4 book ai didi

java - 将日期从 UTC 转换为 CET

转载 作者:行者123 更新时间:2023-11-29 04:46:04 25 4
gpt4 key购买 nike

<分区>

我想将 UTC 格式的日期转换为 CET 格式的日期。

问题是我需要相应地增加或减少小时数。

例子:

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

我想将其转换为德语日期(增加两个小时):

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com