【发布时间】:2012-04-03 00:27:48
【问题描述】:
我多次看到这个问题被问到,但似乎没有一个答案是我需要的。
我有一个长类型变量,其中存储了一个纪元时间。
我想要做的是将它转换为字符串
例如,如果存储的纪元时间是今天,则最终字符串将显示为:
17/03/2012
我会怎么做?
【问题讨论】:
我多次看到这个问题被问到,但似乎没有一个答案是我需要的。
我有一个长类型变量,其中存储了一个纪元时间。
我想要做的是将它转换为字符串
例如,如果存储的纪元时间是今天,则最终字符串将显示为:
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);
【讨论】:
long now = Instant.now().toEpochMilli(); 另外,import java.time.Instant; 和 import java.text.SimpleDateFormat; 我是 Java 新手,我不得不在别处寻找完整的图片。为了完整起见,我在这里添加。
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);
【讨论】:
如果您所说的纪元时间是指自 1970 年 UTC 第一刻以来的毫秒数,那么这里有一些使用 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 中使用。但请注意,至少有一个couple dozen epoch definitions 被各种计算机系统使用。
【讨论】:
是时候有人提供现代答案了(自 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 日
请注意 sn-p 如何让您指定时区、语言(区域设置)以及所需格式的长短。
【讨论】:
您需要注意,Java 中的纪元时间以毫秒为单位,而您要转换的可能以秒为单位。确保两边的转换都以毫秒为单位,然后就可以从 Date 对象中获取日期参数了。
【讨论】:
ArLiteDTMConv 实用程序帮助转换 EPOUCH-UNIX 日期时间值、表格 EPOUCH-UNIX-To-Date-format 和 Vise-Versa。您可以将结果设置为变量,然后在脚本中使用该变量,或者在作为参数传递时使用该变量,或者在 Window 和 Linux 的任何 DB 标准中引入该变量。 (在此链接上下载 zip 文件)
【讨论】:
试试这个...
示例 Epoch 时间戳为 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
【讨论】: