Java如何把+0800改成+0000?
How to change +0800 to +0000 in Java?
如何在Java中将2018-12-24 12:00:00 +0800
更改为2018-12-23 16:00:00 +0000
?
private String currentDateandTime = new Date();
final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
final DateFormat fullFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss XX", Locale.CHINA);
//dateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
//fullFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
Date dateTest = dateFormat.parse(currentDateandTime);
currentDateandTime = fullFormat.format(dateTest);
currentDateanTime 结果
2018-12-24 12:00:00 +0800
需要为全格式设置 "GMT" 时区 (+0000),以便将默认时区转换为使用 GMT 时区:
Date currentDateandTime = new Date();
final DateFormat fullFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss XX", Locale.CHINA);
TimeZone gmtTime = TimeZone.getTimeZone("GMT");
fullFormat.setTimeZone(gmtTime);
String currentDateandTimeInGMTFullFormat = fullFormat.format(currentDateandTime);
嗯,你的代码开始有问题。
private String currentDateandTime = new Date();
假设 Date()
是从这里导入的 java.util.Date
这一行应该显示编译器错误
无论如何,我假设您想将 LocalTimezone DateTime 转换为 GMT 日期时间
Date currentDate = new Date();
System.out.println(currentDate);
final DateFormat gmtFormatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
gmtFormatter.setTimeZone(TimeZone.getTimeZone("GMT"));
String convertedDate = gmtFormatter.format(currentDate);
System.out.println(convertedDate);
OUTPUT:
Mon Dec 24 09:52:14 IST 2018
2018-12-24 04:22:14
如果只想获取不同时区的同一个实例,可以这样:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss Z");
OffsetDateTime offsetDateTime = OffsetDateTime.parse("2018-12-24 12:00:00 +0800", formatter);
ZonedDateTime dateTimeInDesiredZoned = offsetDateTime.atZoneSameInstant(ZoneId.of("UTC"));
// 2018-12-24 04:00:00 +0000
System.out.println(formatter.format(dateTimeInDesiredZoned));
但是,2018-12-24 12:00:00 +0800
和2018-12-23 16:00:00 +0000
不是同一个瞬间。他们之间有12个小时的间隔,你需要减去这12个小时。
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss Z");
OffsetDateTime offsetDateTime = OffsetDateTime.parse("2018-12-24 12:00:00 +0800", formatter);
offsetDateTime = offsetDateTime.minusHours(12);
ZonedDateTime dateTimeInDesiredZoned = offsetDateTime.atZoneSameInstant(ZoneId.of("UTC"));
// 2018-12-23 16:00:00 +0000
System.out.println(formatter.format(dateTimeInDesiredZoned));
如何在Java中将2018-12-24 12:00:00 +0800
更改为2018-12-23 16:00:00 +0000
?
private String currentDateandTime = new Date();
final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
final DateFormat fullFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss XX", Locale.CHINA);
//dateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
//fullFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
Date dateTest = dateFormat.parse(currentDateandTime);
currentDateandTime = fullFormat.format(dateTest);
currentDateanTime 结果
2018-12-24 12:00:00 +0800
需要为全格式设置 "GMT" 时区 (+0000),以便将默认时区转换为使用 GMT 时区:
Date currentDateandTime = new Date();
final DateFormat fullFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss XX", Locale.CHINA);
TimeZone gmtTime = TimeZone.getTimeZone("GMT");
fullFormat.setTimeZone(gmtTime);
String currentDateandTimeInGMTFullFormat = fullFormat.format(currentDateandTime);
嗯,你的代码开始有问题。
private String currentDateandTime = new Date();
假设 Date()
是从这里导入的 java.util.Date
这一行应该显示编译器错误
无论如何,我假设您想将 LocalTimezone DateTime 转换为 GMT 日期时间
Date currentDate = new Date();
System.out.println(currentDate);
final DateFormat gmtFormatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
gmtFormatter.setTimeZone(TimeZone.getTimeZone("GMT"));
String convertedDate = gmtFormatter.format(currentDate);
System.out.println(convertedDate);
OUTPUT:
Mon Dec 24 09:52:14 IST 2018
2018-12-24 04:22:14
如果只想获取不同时区的同一个实例,可以这样:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss Z");
OffsetDateTime offsetDateTime = OffsetDateTime.parse("2018-12-24 12:00:00 +0800", formatter);
ZonedDateTime dateTimeInDesiredZoned = offsetDateTime.atZoneSameInstant(ZoneId.of("UTC"));
// 2018-12-24 04:00:00 +0000
System.out.println(formatter.format(dateTimeInDesiredZoned));
但是,2018-12-24 12:00:00 +0800
和2018-12-23 16:00:00 +0000
不是同一个瞬间。他们之间有12个小时的间隔,你需要减去这12个小时。
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss Z");
OffsetDateTime offsetDateTime = OffsetDateTime.parse("2018-12-24 12:00:00 +0800", formatter);
offsetDateTime = offsetDateTime.minusHours(12);
ZonedDateTime dateTimeInDesiredZoned = offsetDateTime.atZoneSameInstant(ZoneId.of("UTC"));
// 2018-12-23 16:00:00 +0000
System.out.println(formatter.format(dateTimeInDesiredZoned));