6

有人可以详细解释以下内容:

我有一个long代表 a 的值date

  1. timezone与价值相关联的是什么long

  2. 如何将long值转换为正确的日期time zone

  3. 有没有办法识别timezonelong date价值相关的?

4

4 回答 4

7

The long is milliseconds since Jan 1970, GMT. So, to that respect, it is GMT.

于 2012-10-08T04:27:13.553 回答
4

Date (作为 long 或java.util.Date)表示时间的某个时刻。

除非您处理日历,否则不涉及时区。

您可以为给定的 TimeZone 和 Locale 创建一个日历,如下所示:

long rightNow = System.currentTimeMillis();
Locale exampleLocale = Locale.GERMANY;
TimeZone zone = TimeZone.getTimeZone("EST");

Calendar theCalendar = Calendar.getInstance(zone, exampleLocale);
theCaledar.setTime(new Date(rightNow));
于 2012-10-08T04:14:48.407 回答
4

The long value which represents the java.util.Date is the number of milliseconds elapsed from epoch. (Jan 1, 1970)

/**
 * Allocates a <code>Date</code> object and initializes it to 
 * represent the specified number of milliseconds since the 
 * standard base time known as "the epoch", namely January 1, 
 * 1970, 00:00:00 GMT. 
 *
 * @param   date   the milliseconds since January 1, 1970, 00:00:00 GMT.
 * @see     java.lang.System#currentTimeMillis()
 */
public Date(long date) {
    fastTime = date;
}
  • What will be the timezone associated with the long value?
    Can you attach a unit to a long value.? No.
    This is akin to saying given an int 2 what does it represent? . It could be 2 miles or 2 pounds.

  • How to convert the long value to a date with proper time zone?
    You can't because of above.

  • Is there is way to identify the timezone associated with the long date value?
    Nope.

于 2012-10-08T04:26:52.170 回答
2

当时间为长格式时,TimeZone不会与之关联。

您需要使用任一SimpleDateFormat(或)CalendarAPI 将时区应用于长值。

于 2012-10-08T03:49:46.397 回答