我对 Java 还很陌生,并且对使用 SimpleDateFormat
和 Calendar
有点困惑。我有一个日期对象,想要提取像 yyyy-MM-dd HH:mm:ss 这样的 GMT 日期字符串。我住在德国,现在是 GMT +0200。例如,我的日期对象的时间是 2011-07-18 13:00:00
。我现在需要的是2011-07-18 11:00:00
。我的时区的偏移量应该自动计算。
我尝试了类似的方法,但我猜想某处存在错误:
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);
}
在某些设备上,日期字符串按照我想要的方式返回。在其他设备上,偏移量计算不正确,我从输入日期对象接收日期和时间。你能给我一些提示或建议吗?我想我的默认时区方法不起作用?
I am pretty new to Java and I am a little stuck with using SimpleDateFormat
and Calendar
. I have a Date-Object and want to extract a GMT datestring like yyyy-MM-dd HH:mm:ss
. I live in Germany and at the moment we are GMT +0200. My Date-Object's time is for example 2011-07-18 13:00:00
. What I need now is 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?
发布评论
评论(3)
您不需要创建新的
SimpleTimeZone
,因为您并不是在发明一个新时区 - 您的程序中有 2 个现有时区,即 GMT 和默认时区。您也不需要修改现有的日期对象,因为您不想表示不同的时间点 - 您只想以不同的方式显示相同的时间点。
您所需要做的就是告诉
SimpleDateFormat
在格式化时使用哪个时区。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.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.
All you need to do is tell the
SimpleDateFormat
which timezone to use in formatting.java.time
使用现代日期时间 API
java.time
,有多种方法可以实现:LocalDateTime
➡️ 组合它与您的时区一起获取ZonedDateTime
➡️ 转换为Instant
➡️ 使用 ZonedDateTime href="https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html#atZone-java.time.ZoneId-" rel="nofollow noreferrer">即时# atZone
和 UTC 时区。LocalDateTime
➡️ 将其与您的时区结合起来以获得ZonedDateTime
➡️ 转换为Instant
➡️ 使用 < 转换为ZonedDateTime
一个href="https://docs.oracle.com/javase/8/docs/api/java/time/ZonedDateTime.html#ofInstant-java.time.Instant-java.time.ZoneId-" rel="nofollow noreferrer" >ZonedDateTime#ofInstant
和 UTC 时区。ZonedDateTime#withZoneSameInstant
:ZonedDateTime# withZoneSameInstant
:了解有关现代日期时间 API*< /sup> 来自跟踪:日期时间。
* 无论出于何种原因,如果您必须坚持使用 Java 6 或 Java 7,您可以使用 ThreeTen-Backport 将大部分 java.time 功能向后移植到 Java 6 和 Java 6 7. 如果您正在处理 Android 项目,并且您的 Android API 级别仍然不符合 Java-8,请检查 通过脱糖提供 Java 8+ API 和 如何在Android项目中使用ThreeTenABP。
java.time
Using
java.time
, the modern date-time API, there are many ways to do it:LocalDateTime
➡️ Combine it with your timezone to getZonedDateTime
➡️ Convert toInstant
➡️ Convert toZonedDateTime
usingInstant#atZone
and UTC timezone.LocalDateTime
➡️ Combine it with your timezone to getZonedDateTime
➡️ Convert toInstant
➡️ Convert toZonedDateTime
usingZonedDateTime#ofInstant
and UTC timezone.ZonedDateTime#withZoneSameInstant
:DateTimeFormatter#withZone
andZonedDateTime#withZoneSameInstant
:Learn more about the modern date-time API* from Trail: Date Time.
* 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.