我已经多次询问过这个问题,但没有一个答案似乎是我需要的。
我有一个长型变量,其中存储了一个纪元时间。
我想要做的是将其转换为字符串
例如,如果存储的纪元时间是今天,则最终字符串将显示为:
17/03/2012
我怎么会这样?
答案 0 :(得分:30)
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
sdf.format(new Date(myTimeAsLong));
答案 1 :(得分:22)
您可以从Date
创建long
- 这很简单:
Date date = new Date(epochTime);
请注意epochTime
此处应该是自纪元以来的毫秒数 - 如果您从纪元开始已经秒,则乘以1000。
然后你创建一个SimpleDateFormat
来指定相关的模式,文化和时区。例如:
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy", Locale.US);
format.setTimeZone(...);
然后使用它将日期格式化为字符串:
String text = format.format(date);
答案 2 :(得分:16)
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
答案 3 :(得分:2)
如果按照纪元时间你的意思是自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中使用。但要注意,各种计算机系统至少使用couple dozen epoch definitions。
答案 4 :(得分:1)
试试这个
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);
答案 5 :(得分:0)
你需要知道java中的纪元时间是以毫秒为单位,而你转换的时间可能是几秒钟。确保转换的两侧都是以毫秒为单位,然后您可以从Date对象中获取日期参数。
答案 6 :(得分:0)
试试这个......
示例纪元时间戳是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 pm
快乐编码
答案 7 :(得分:0)
基于之前的答案,同时传递系统默认Locale
和TimeZone
...
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));
}
答案 8 :(得分:0)
某人提供现代答案的时间(自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);
1/25/19
请注意,该代码段如何允许您指定时区,语言(语言环境)以及所需格式的长短。
答案 9 :(得分:0)
ArLiteDTMConv实用工具可帮助转换EPOUCH-UNIX日期时间值,EPOUCH-UNIX-To-Date格式和Vise-Versa格式。您可以将结果设置为变量,然后在脚本中或在将其作为参数传递时使用该变量,或者为Window和Linux引入任何数据库条件。 (在此链接上下载一个zip文件)