Java 将字符串转换为 GregorianCalendar

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/240510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 11:55:01  来源:igfitidea点击:

Convert a string to GregorianCalendar

javacalendartimezone

提问by Marcin

I have a string from an email header, like Date: Mon, 27 Oct 2008 08:33:29 -0700. What I need is an instance of GregorianCalendar, that will represent the same moment. As easy as that -- how do I do it?

我有一个来自电子邮件标题的字符串,例如Date: Mon, 27 Oct 2008 08:33:29 -0700. 我需要的是 GregorianCalendar 的一个实例,它将代表同一时刻。就这么简单——我该怎么做?

And for the fastest ones -- this is notgoing to work properly:

对于最快的 - 这将无法正常工作:

SimpleDateFormat format = ... // whatever you want
Date date = format.parse(myString)
GregorianCalendar calendar = new GregorianCalendar();
calendar.setTime(date)

because it will normalize the timezone to UTC (or your local machine time, depending on Java version). What I need is calendar.getTimeZone().getRawOffset() to return -7 * milisInAnHour.

因为它会将时区标准化为 UTC(或您的本地机器时间,取决于 Java 版本)。我需要的是 calendar.getTimeZone().getRawOffset() 返回-7 * milisInAnHour

采纳答案by Hyman Leow

I'd recommend looking into the Joda Time library, if that's an option. I'm normally against using a third-party library when the core platform provides similar functionality, but I made this an exception because the author of Joda Time is also behind JSR310, and Joda Time is basically going to be rolled into Java 7 eventually.

如果可以的话,我建议您查看 Joda Time 库。当核心平台提供类似功能时,我通常反对使用第三方库,但我将其作为一个例外,因为 Joda Time 的作者也落后于 JSR310,而 Joda Time 基本上最终会被纳入 Java 7。

http://joda-time.sourceforge.net/

http://joda-time.sourceforge.net/

So anyway, if Joda Time is an option, something like this shouldwork:

所以无论如何,如果 Joda Time 是一个选项,这样的事情应该可行:

DateTimeFormatter formatter =
    DateTimeFormat.forPattern("your pattern").withOffsetParsed();
DateTime dateTime = formatter.parseDateTime("your input");
GregorianCalendar cal = dateTime.toGregorianCalendar();

I hope this helps.

我希望这有帮助。

回答by matt b

And for the fastest ones -- this is not going to work properly ... because it will normalize the timezone to UTC (or your local machine time, depending on Java version). What I need is calendar.getTimeZone().getRawOffset() to return -7 * milisInAnHour.

对于最快的 - 这将无法正常工作......因为它会将时区标准化为UTC(或您的本地机器时间,取决于Java版本)。我需要的是 calendar.getTimeZone().getRawOffset() 返回 -7 * milisInAnHour。

Well technically this does work, because while it will return an object with TimeZone equal to the current system TimeZone, the time will be modified to account for the offset.

从技术上讲,这确实有效,因为虽然它会返回一个 TimeZone 等于当前系统 TimeZone 的对象,但时间将被修改以说明偏移量。

This code:

这段代码:

String dateString = "Mon, 27 Oct 2008 08:33:29 -0700";
DateFormat df = new SimpleDateFormat("E, dd MMM yyyy hh:mm:ss Z");
Date parsed = df.parse(dateString);
System.out.println("parsed date: " + parsed);

Calendar newCalendar = Calendar.getInstance();
newCalendar.setTime(parsed);

outputs:

输出:

parsed date: Mon Oct 27 11:33:29 EDT 2008

解析日期:2008 年 10 月 27 日星期一 11:33:29 EDT

which technically is correct, since my system timezone is EDT / UTC minus four hours (which is three hours ahead of yours). If you express time as the number of milliseconds since January 1, 1970, 00:00:00 GMT (which is how the Dateobject stores it's date/time), then these date/times are equal, it's just the TimeZone that is different.

这在技术上是正确的,因为我的系统时区是 EDT / UTC 减去四小时(比你的时区早三小时)。如果您将时间表示为自格林威治标准时间 1970 年 1 月 1 日 00:00:00(Date对象存储日期/时间的方式)以来的毫秒数,那么这些日期/时间是相等的,只是时区不同。

Your issue is really How do I convert a Date/Calendar into my timezone?For that, take a look at my response to the previous question How to handle calendar TimeZones using Java?

您的问题真的是如何将日期/日历转换为我的时区?为此,请查看我对上一个问题如何使用 Java 处理日历时区的回答