将纪元时间转换为日期字符串

问题描述 投票:21回答:10

我已经多次看到这个问题了,没有一个答案似乎是我需要的。

我有一个长型变量,其中存储了一个纪元时间。

我想要做的是将其转换为String

例如,如果存储的纪元时间是今天的最终字符串将读取:

17/03/2012

我怎么会这样?

java android epoch
10个回答
32
投票

看看SimpleDateFormat

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
sdf.format(new Date(myTimeAsLong));

0
投票

ArLiteDTMConv实用程序帮助转换EPOUCH-UNIX日期时间值,表单EPOUCH-UNIX-To-Date-format和Vise-Versa。您可以将结果设置为变量,然后在脚本中使用该变量,或者在作为参数传递时引入,或者在Window和Linux的任何DB条件中引入。 (在此链接下载zip文件)


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);

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


2
投票

Joda-Time

如果按照纪元时间你的意思是自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 );

Other Epochs

epoch的定义很常见,因为它在Unix中使用。但请注意,各种计算机系统至少使用couple dozen epoch definitions


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);

0
投票

你需要知道java中的纪元时间是以毫秒为单位,而你转换的时间可能是几秒钟。确保转换的两侧都以毫秒为单位,然后您可以从Date对象中获取日期参数。


0
投票

试试这个...

示例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 pm

快乐的编码


0
投票

基于以前的答案,同时传递系统默认的LocaleTimeZone ...

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));
}

0
投票

有人提供现代答案的时间(自2014年起有效和推荐)。

java.time

    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

请注意该代码段如何允许您指定时区,语言(区域设置)以及所需格式的长短。

链接

© www.soinside.com 2019 - 2024. All rights reserved.