Java - 创建没有时区的日历

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11416914/
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-10-31 05:01:57  来源:igfitidea点击:

Java - Creating a Calendar without a TimeZone

javadatetimetimezone

提问by MaLLinok

I would like to start by saying that I've read several threads similar to this one, but none of them really solved my problem. I would also like to state that I've tried to use SimpleDateFormatand joda.DateTimewithout any success.

我想首先说我已经阅读了几个与此类似的主题,但没有一个真正解决了我的问题。我也想指出,我试着使用SimpleDateFormatjoda.DateTime没有任何成功。

The problem is the following:

问题如下:

I have a Calendar object that holds the information about a specific date: 2008-04-30T00:00:00Z When using the calendar.getTime()method I can get different results because I know that that method is looking for the local value

我有一个 Calendar 对象,其中包含有关特定日期的信息:2008-04-30T00:00:00Z 使用该calendar.getTime()方法时,我可以获得不同的结果,因为我知道该方法正在寻找本地值

Thus:

因此:

UK: 2008-04-30T01:00:00.000+0100

US: 2008-04-30T20:00:00.000-0400

But I would like to get a Date object that holds just the Date and Time values "2008-04-30T00:00:00" ignoring completely any timezone.

但我想得到一个 Date 对象,它只包含日期和时间值“2008-04-30T00:00:00”而完全忽略任何时区。

How can I do that?

我怎样才能做到这一点?

As I mentioned before I tried to use SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss")but I always end up with the same results.

正如我之前提到的,我尝试使用 SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss")但我总是得到相同的结果。

Any help would be really appreciated Cheers.

任何帮助将不胜感激干杯。

回答by GuestOnly

Found out that you can clear the Timezone by using code below:

发现您可以使用以下代码清除时区:

Calendar cal = Calendar.getInstance(); cal.clear(Calendar.ZONE_OFFSET);

Calendar cal = Calendar.getInstance(); cal.clear(Calendar.ZONE_OFFSET);

回答by jahroy

Calendarsand Datesmean nothing without a TimeZone.

CalendarsDates毫无意义没有TimeZone

Calendars and dates cannotexist without a timezone.

没有时区,日历和日期就不能存在。

You can't ignore completely any timezone.

你不能ignore completely any timezone

You can create a Calendar for Greenwich Mean Time (offset zero) like this:

您可以为格林威治标准时间(零偏移)创建一个日历,如下所示:

    TimeZone zone = TimeZone.getTimeZone("Etc/GMT");
    Calendar cal = Calendar.getInstance(zone);

This represents a Date/Calendar that is only meaningful in the GMT timezone.

这表示仅在 GMT 时区有意义的日期/日历。

It sounds like you want a timestamp, which represents an instant in time.

听起来你想要一个时间戳,它代表一个瞬间。

回答by dgl

Do you use a standard constructor for initializing Calendar? What if you used the constructor which allows to specify the time zone and locale?

您是否使用标准构造函数来初始化 Calendar?如果您使用允许指定时区和语言环境的构造函数怎么办?

protected Calendar(TimeZone zone, Locale aLocale)

回答by JJ Brown

As others have pointed out, Calendarand Dateobjects cannot exist without a time zone.

正如其他人指出的那样,没有时区CalendarDate对象就不能存在。

I believe you may want to use the LocalDateTimeclass introduced in Java 8 with the new time API:

我相信您可能希望将Java 8 中引入的LocalDateTime类与新的时间 API 一起使用:

LocalDateTime literal = LocalDateTime.of(2008, 4, 30, 0, 0, 0);
LocalDateTime parsed = LocalDateTime.parse("2008-04-30T00:00:00"); // ISO-8601 by default
Assert.assertEquals(literal, parsed);

回答by Gunnar Bernstein

Old, but still incorrect.

旧的,但仍然不正确。

"When using the calendar.getTime() method I can get different results because I know that that method is looking for the local value"

“使用 calendar.getTime() 方法时,我可以获得不同的结果,因为我知道该方法正在寻找本地值”

That is a misconception. getTime() will get the Milliseconds only. Countet as GMT.

那是一种误解。getTime() 将仅获取毫秒。以格林威治标准时间计数。

ONLY during formatting of the Output the time zone becomes relevant. Sind the original poster did not show the code, it can not be decided, where the error occurs.

只有在格式化输出期间,时区才相关。Sind原始海报没有显示代码,无法确定错误发生在哪里。