4

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 SimpleDateFormat and joda.DateTime without any success.

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

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.

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.

Any help would be really appreciated Cheers.

4

5 回答 5

9

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

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

于 2015-11-10T04:13:04.837 回答
4

CalendarsDates没有TimeZone. _

没有时区,日历和日期就无法存在。

你不能ignore completely any timezone

您可以像这样为格林威治标准时间(偏移量为零)创建一个日历:

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

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

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

于 2012-07-10T17:22:41.197 回答
1

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

protected Calendar(TimeZone zone, Locale aLocale)
于 2012-07-10T15:46:35.397 回答
1

正如其他人所指出的,没有时区CalendarDate对象就无法存在。

我相信您可能希望将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);
于 2019-06-12T19:37:57.417 回答
0

旧的,但仍然不正确。

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

那是一种误解。getTime() 将仅获得毫秒。计数为 GMT。

只有在格式化输出时,时区才会变得相关。Sind 原贴没有显示代码,无法确定错误发生在哪里。

于 2014-04-11T05:24:39.510 回答