[英]Converting Epoch time to date string
我已经多次看到这个问题,但似乎没有一个答案是我需要的。
我有一个长型变量,其中存储了一个纪元时间。
我想要做的是将其转换为字符串
例如,如果存储的纪元时间是今天,则最终字符串将显示为:
17/03/2012
我该怎么办?
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
sdf.format(new Date(myTimeAsLong));
你会从long
创建一个Date
- 这很容易:
Date date = new Date(epochTime);
请注意,这里的epochTime
应该是自纪元以来的毫秒数 - 如果自纪元以来有秒数,则乘以 1000。
然后您将创建一个SimpleDateFormat
指定相关的模式、文化和时区。 例如:
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy", Locale.US);
format.setTimeZone(...);
然后使用它将日期格式化为字符串:
String text = format.format(date);
Date date = new Date(String);
这已被弃用。
解决方案
Date date = new Date(1406178443 * 1000L);
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
format.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
String formatted = format.format(date);
确保乘以 1000L
如果该方法应该是可移植的,最好使用默认(本地时间) TimeZone.getDefault()
:
String epochToIso8601(long time) {
String format = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.getDefault());
sdf.setTimeZone(TimeZone.getDefault());
return sdf.format(new Date(time * 1000));
}
尝试这个
Date date = new Date(1476126532838L);
DateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
format.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
String formatted = format.format(date);
format.setTimeZone(TimeZone.getTimeZone("Asia/Colombo"));//your zone
formatted = format.format(date);
System.out.println(formatted);
如果纪元时间是指自 UTC 时间 1970 年第一时刻以来的毫秒数,那么这里是一些使用 Joda-Time 库的示例代码……
DateTimeZone timeZone = DateTimeZone.forID( "Europe/Paris" );
DateTime dateTime = new DateTime( yourMilliseconds, timeZone );
String output = DateTimeFormat.forStyle( "S-" ).withLocale( Locale.CANADA_FRENCH ).print( dateTime );
epoch 的定义很常见,因为它在 Unix 中使用。 但请注意,各种计算机系统至少使用了几十个纪元定义。
是时候提供现代答案了(自 2014 年起有效并推荐)。
DateTimeFormatter formatter = DateTimeFormatter
.ofLocalizedDateTime(FormatStyle.LONG).withLocale(Locale.US);
String facebookTime = "1548410106047";
long fbt = Long.parseLong(facebookTime);
ZonedDateTime dateTime = Instant.ofEpochMilli(fbt).atZone(ZoneId.of("America/Indiana/Knox"));
System.out.println(dateTime.format(formatter));
输出是:
2019 年 1 月 25 日美国中部标准时间上午 3:55:06
如果您只想要日期和较短的格式,请使用例如
DateTimeFormatter formatter = DateTimeFormatter
.ofLocalizedDate(FormatStyle.SHORT).withLocale(Locale.US);
19 年 1 月 25 日
请注意该代码段如何允许您指定时区、语言(区域设置)以及所需格式的长度或长度。
您需要注意 java 中的纪元时间以毫秒为单位,而您正在转换的时间可能以秒为单位。 确保双方的转换都以毫秒为单位,然后您就可以从 Date 对象中获取日期参数。
尝试这个...
样本纪元时间戳为 1414492391238
方法:
public static String GetHumanReadableDate(long epochSec, String dateFormatStr) {
Date date = new Date(epochSec * 1000);
SimpleDateFormat format = new SimpleDateFormat(dateFormatStr,
Locale.getDefault());
return format.format(date);
}
可用性:
long timestamp = Long.parseLong(engTime) / 1000;
String engTime_ = GetHumanReadableDate(timestamp, "dd-MM-yyyy HH:mm:ss aa");
结果:
28-10-2014 下午 16:03:11
快乐编码
ArLiteDTMConv 实用程序帮助转换 EPOUCH-UNIX 日期时间值、表格 EPOUCH-UNIX-To-Date-format 和 Vise-Versa。 您可以将结果设置为一个变量,然后在脚本中使用该变量,或者在作为参数传递或在 Window 和 Linux 的任何数据库标准中引入时使用该变量。 (在此链接上下载 zip 文件)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.