我收到的日期表示utc中的日期时间。假设:21-6月-2019 10:00
我想把这个日期转换成时区“欧洲/维也纳”,期望:21-6月-2019 12:00。
我不明白,为什么下面的代码显示的时间是相同的?
Date utcFinish = new Date(new Date().getYear(), Calendar.JUNE, 21);
TimeZone europeVienna = TimeZone.getTimeZone("Europe/Vienna");
Calendar finishInViennaTime = Calendar.getInstance(europeVienna);
finishInViennaTime.setTime(utcFinish);
System.out.println(format.format(utcFinish));
System.out.println(format.format(finishInViennaTime.getTime()));输出:
2019-06-21 00:00
2019-06-21 00:00什么是最好的java7只(没有joda,本地化请)解决方案!?谢谢
编辑:我也尝试过:
SimpleDateFormat formatWithTimezone = new SimpleDateFormat("yyyy-MM-dd HH:mm");
formatWithTimezone.setTimeZone(TimeZone.getTimeZone("Europe/Vienna"));
SimpleDateFormat formatonly = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date utcDate = new Date(new Date().getYear(), Calendar.JUNE, 21);
System.out.println(formatonly.format(utcDate));
System.out.println(formatWithTimezone.format(utcDate));输出:
2019-06-21 00:00
2019-06-21 00:00溶液
谢谢你所有的解决方案。最后,问题是默认时区。以下是我目前的解决方案(欢迎进一步反馈!):
// Unfortunately this date has the wrong time zone (Local Time Zone),
// because Date assumes Local Time Zone the database stores timestamps
// in utc that's why I now convert to a datestring and reparse
Date finishTimeWrongTimeZone = new Date(new Date().getYear(), Calendar.JUNE, 21);
// in reality i call the db here like getFinishTime();
// get the plain date string without time shifting
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MMM-dd HH:mm");
String dateWithoutTimeZone = formatter.format(finishTimeWrongTimeZone);
// add the timezone to the formatter and reinterpret the datestring
// effectively adding the correct time zone the date should be in
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
String finishTime = null;
try {
Date dateWithCorrectTimeZone = formatter.parse(dateWithoutTimeZone);
// Convert to expected local time zone (europe/vienna)
formatter.setTimeZone(TimeZone.getTimeZone("Europe/Vienna"));
finishTime = formatter.format(dateWithCorrectTimeZone);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(finishTime);发布于 2019-04-17 12:10:34
我使用一个格式化程序对象并更改其上的时区。
format.setTimeZone(TimeZone.getTimeZone("UTC"));格式=新的SimpleDateFormat (“dd:mm”)
Date dUtc = format.parse("21-06-2019 10:00");
System.out.println(dUtc);
TimeZone europeVienna = TimeZone.getTimeZone("europe/vienna");
format.setTimeZone(europeVienna);
String sVienna = format.format(dUtc);
System.out.println(sVienna);发布于 2019-04-17 12:33:37
现代(java.time)溶液
按照您的要求,下面有一个旧API的版本,但为了完整起见,我还将提供更现代的解决方案。如果更新Java不是一种选择,我建议您查看:
ZonedDateTime zdt = LocalDateTime.of(
Year.now().getValue(), Month.JUNE, 21, 10, 0, 0
).atZone(ZoneOffset.UTC);
System.out.println(
zdt.withZoneSameInstant(ZoneId.of("Europe/Vienna"))
.format(DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm"))
);旧(java.util)溶液
不建议使用new Date(...),您不应该使用它。如果您确实需要坚持旧的API,则需要使用Calendar
Calendar utcFinish = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
utcFinish.set(Calendar.MONTH, Calendar.JUNE);
utcFinish.set(Calendar.DATE, 21);
utcFinish.set(Calendar.HOUR_OF_DAY, 10);
utcFinish.set(Calendar.MINUTE, 0);然后使用一个带有您希望使用的时区的DateFormat:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
TimeZone europeVienna = TimeZone.getTimeZone("Europe/Vienna");
format.setTimeZone(europeVienna);
System.out.println(format.format(utcFinish.getTime()));输出
这两种解决方案都应输出(在编写本报告时,在2019年):
2019-06-21 12:00发布于 2019-04-17 11:48:00
请使用SimpleDateFormat转换时区
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date utcFinish = new Date(new Date().getYear(), Calendar.JUNE, 21);
TimeZone europeVienna = TimeZone.getTimeZone("Europe/Vienna");
format.setTimeZone(europeVienna);
System.out.println(format.format(utcFinish));https://stackoverflow.com/questions/55726699
复制相似问题