我有时间来自 1352437114052 格式的 gpslocation 服务。有人可以告诉我如何在 Java、Matlab 或 Excel 中将其转换为当地时间。
6 回答
Date
从纪元以来的毫秒数创建一个新的。然后使用 aDateFormat
将其格式化为您想要的时区。
Date date = new Date(1352437114052L);
DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
format.setTimeZone(TimeZone.getTimeZone("PST"));
System.out.println(format.format(date));
这是一个纪元时间,它代表格林威治标准时间 2012 年 11 月 9 日星期五 04:58:34。这个数值是一个绝对时间点,与时区无关。
如果您想在不同时区查看该时间点,请使用GregorianCalendar
:
Calendar c = new GregorianCalendar(TimeZone.getTimeZone("EST"));
c.setTimeInMillis(1352437114052L);
c.get(Calendar.HOUR_OF_DAY); //20:58 the day before
使用 JVM 的时区设置(通常与您的计算机的时区相同)的现代 Java 答案:
long time = 1_352_437_114_052L;
ZonedDateTime dateTime = Instant.ofEpochMilli(time).atZone(ZoneId.systemDefault());
System.out.println(dateTime);
在我的电脑上运行我得到
2012-11-09T05:58:34.052+01:00[欧洲/哥本哈根]
要指定时区:
ZonedDateTime dateTime = Instant.ofEpochMilli(time).atZone(ZoneId.of("Asia/Almaty"));
2012-11-09T10:58:34.052+06:00[亚洲/阿拉木图]
问题:这也适用于 Android 吗?
在这里回答修补匠的评论:是的。我正在使用java.time
现代 Java 日期和时间 API,它在较旧和较新的 Android 设备上运行良好。它只需要至少 Java 6。
- In Java 8 and later and on newer Android devices (from API level 26, I’m told) the modern API comes built-in.
- In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310; see the links at the bottom).
- On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from
org.threeten.bp
with subpackages.
Links
- Oracle tutorial: Date Time explaining how to use
java.time
. - Java Specification Request (JSR) 310, where
java.time
was first described. - ThreeTen Backport project, the backport of
java.time
to Java 6 and 7 (ThreeTen for JSR-310). - ThreeTenABP, Android edition of ThreeTen Backport
- Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
@Steve Kuo 几乎直接回答了这个问题。这是机器本地时间的更通用解决方案,包括夏时制时间,其中a
的类型BasicFileAttributes
是从 Windows 目录条目中报告的public FileVisitResult visitFile(Path f, BasicFileAttributes a)
during Files.walkFileTree
:
String modifyDate;
Date date = new Date(a.lastModifiedTime().to(TimeUnit.MILLISECONDS));
DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
format.setTimeZone(TimeZone.getDefault());
modifyDate = (format.format(date)).substring(0,10);
long timeStamp = System.currentTimeMillis();
System.out.println(timeStamp+"");
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getDefault());
calendar.setTimeInMillis(timeStamp);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a z");
String dateString = sdf.format(calendar.getTime());
System.out.println(dateString);
输出 :
timestamp : 1528860439258
dateformat from sdf : 2018-06-12 08:27:19 PM PDT
Since new Date(String string) is deprecated now(which is the accepted answer), we can use DateTimeZone.getDefault()
to get the system time zone
public String getZonedDate(String dateStr) {
DateTime utcDateTime = new DateTime(dateStr).toDateTime(DateTimeZone.UTC);
return utcDateTime
.toDateTime(DateTimeZone.getDefault()).toString("yyyy-MM-dd'T'HH:mm:ss");
}