[英]Convert GMT DateTime String
I am pretty new to Java and I am a little stuck with using SimpleDateFormat
and Calendar
.我对 Java 很陌生,我对使用SimpleDateFormat
和Calendar
有点坚持。 I have a Date-Object and want to extract a GMT datestring like yyyy-MM-dd HH:mm:ss
.我有一个 Date-Object 并想提取一个像yyyy-MM-dd HH:mm:ss
这样的 GMT 日期字符串。 I live in Germany and at the moment we are GMT +0200.我住在德国,目前我们是 GMT +0200。 My Date-Object's time is for example 2011-07-18 13:00:00
.我的日期对象的时间是例如2011-07-18 13:00:00
。 What I need now is 2011-07-18 11:00:00
.我现在需要的是2011-07-18 11:00:00
。 The offset for my timezone should be calculated automatically.我的时区的偏移量应该自动计算。
I tried something like this, but I guess there is a fault somewhere:我尝试过这样的事情,但我想某处有问题:
private String toGmtString(Date date){
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
TimeZone timeZone = TimeZone.getDefault();
Calendar cal = Calendar.getInstance(new SimpleTimeZone(timeZone.getOffset(date.getTime()), "GMT"));
sd.setCalendar(cal);
return sd.format(date);
}
On some devices the datestring is returned like I want it to.在某些设备上,日期字符串会像我想要的那样返回。 On other devices the offset isn't calculated right and I receive the date and time from the input date-object.在其他设备上,偏移量计算不正确,我从输入日期对象接收日期和时间。 Can you give me some tips or advices?你能给我一些提示或建议吗? I guess my way off getting the default timezone does not work?我想我获得默认时区的方法不起作用?
private String toGmtString(Date date){
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sd.setTimeZone(TimeZone.getTimeZone("GMT"));
return sd.format(date);
}
You don't need to create a new SimpleTimeZone
, because you aren't inventing a new timezone - there are 2 existing timezones that come into play in your program, GMT and your default one.您不需要创建新的SimpleTimeZone
,因为您没有发明新的时区 - 您的程序中有 2 个现有时区,GMT 和您的默认时区。
You also don't need to modify your existing date object, because you don't want to represent a different point in time - you only want a different way to display the same point in time.您也不需要修改您现有的日期 object,因为您不想表示不同的时间点 - 您只需要不同的方式来显示相同的时间点。
All you need to do is tell the SimpleDateFormat
which timezone to use in formatting.您需要做的就是告诉SimpleDateFormat
在格式化中使用哪个时区。
private String toGmtString(Date date){
//date formatter
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//getting default timeZone
TimeZone timeZone = TimeZone.getDefault();
//getting current time
Calendar cal = Calendar.getInstance()
cal.setTime(date) ;
//adding / substracting curren't timezone's offset
cal.add(Calendar.MILLISECOND, -1 * timeZone.getRawOffset());
//formatting and returning string of date
return sd.format(cal.getTime());
}
Using java.time
, the modern date-time API, there are many ways to do it:使用java.time
,现代日期时间 API ,有很多方法可以做到:
LocalDateTime
➡️ Combine it with your timezone to get ZonedDateTime
➡️ Convert to Instant
➡️ Convert to ZonedDateTime
using Instant#atZone
and UTC timezone.解析为LocalDateTime
➡️ 将其与您的时区结合以获得ZonedDateTime
➡️ 转换为Instant
➡️ 使用Instant#atZone
和 UTC 时区转换为ZonedDateTime
。import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
String strDateTime = "2011-07-18 13:00:00";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss", Locale.ENGLISH);
LocalDateTime ldt = LocalDateTime.parse(strDateTime, dtf);
// Using ZoneId.of("Europe/Berlin") for the demo. Change it to
// ZoneId.systemDefault()
Instant instant = ldt.atZone(ZoneId.of("Europe/Berlin")).toInstant();
ZonedDateTime zdtUtc = instant.atZone(ZoneId.of("Etc/UTC"));
System.out.println(zdtUtc.format(dtf)); // 2011-07-18 11:00:00
}
}
LocalDateTime
➡️ Combine it with your timezone to get ZonedDateTime
➡️ Convert to Instant
➡️ Convert to ZonedDateTime
using ZonedDateTime#ofInstant
and UTC timezone.解析为LocalDateTime
➡️ 将其与您的时区结合以获得ZonedDateTime
➡️ 转换为Instant
➡️ 使用ZonedDateTime#ofInstant
和 UTC 时区转换为ZonedDateTime
。import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
String strDateTime = "2011-07-18 13:00:00";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss", Locale.ENGLISH);
LocalDateTime ldt = LocalDateTime.parse(strDateTime, dtf);
// Using ZoneId.of("Europe/Berlin") for the demo. Change it to
// ZoneId.systemDefault()
Instant instant = ldt.atZone(ZoneId.of("Europe/Berlin")).toInstant();
ZonedDateTime zdtUtc = ZonedDateTime.ofInstant(instant, ZoneId.of("Etc/UTC"));
System.out.println(zdtUtc.format(dtf)); // 2011-07-18 11:00:00
}
}
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
String strDateTime = "2011-07-18 13:00:00";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss", Locale.ENGLISH);
LocalDateTime ldt = LocalDateTime.parse(strDateTime, dtf);
// Using ZoneId.of("Europe/Berlin") for the demo. Change it to
// ZoneId.systemDefault()
ZonedDateTime zdtPak = ldt.atZone(ZoneId.of("Europe/Berlin"));
ZonedDateTime zdtUtc = zdtPak.withZoneSameInstant(ZoneId.of("Etc/UTC"));
System.out.println(zdtUtc.format(dtf)); // 2011-07-18 11:00:00
}
}
DateTimeFormatter#withZone
and ZonedDateTime#withZoneSameInstant
:使用DateTimeFormatter#withZone
和ZonedDateTime#withZoneSameInstant
:import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
String strDateTime = "2011-07-18 13:00:00";
// Using ZoneId.of("Europe/Berlin") for the demo. Change it to
// ZoneId.systemDefault()
DateTimeFormatter dtfInput = DateTimeFormatter.ofPattern("u-M-d H:m:s", Locale.ENGLISH)
.withZone(ZoneId.of("Europe/Berlin"));
ZonedDateTime zdtPak = ZonedDateTime.parse(strDateTime, dtfInput);
ZonedDateTime zdtUtc = zdtPak.withZoneSameInstant(ZoneId.of("Etc/UTC"));
DateTimeFormatter dtfOutput = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss", Locale.ENGLISH);
System.out.println(zdtUtc.format(dtfOutput)); // 2011-07-18 11:00:00
}
}
Learn more about the modern date-time API * from Trail: Date Time .从Trail: Date Time了解有关现代日期时间 API *的更多信息。
* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project . * For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API级别仍然不符合 Java-8,请检查Java 8+ API 可通过脱糖和如何在 Android 项目中使用 ThreeTenABP 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.