有人可以详细解释以下内容:
我有一个long
代表 a 的值date
。
timezone
与价值相关联的是什么long
?如何将
long
值转换为正确的日期time zone
?有没有办法识别
timezone
与long date
价值相关的?
The long is milliseconds since Jan 1970, GMT. So, to that respect, it is GMT.
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));
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.
当时间为长格式时,TimeZone
不会与之关联。
您需要使用任一SimpleDateFormat
(或)Calendar
API 将时区应用于长值。