【问题标题】:Converting Epoch time to date string将纪元时间转换为日期字符串
【发布时间】:2012-04-03 00:27:48
【问题描述】:

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

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

我想要做的是将它转换为字符串

例如,如果存储的纪元时间是今天,则最终字符串将显示为:

17/03/2012

我会怎么做?

【问题讨论】:

    标签: java android epoch


    【解决方案1】:

    查看SimpleDateFormat

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

    【讨论】:

    • 我已经得到了以正确格式打印出一个字符串,但它打印出的日期不正确,它给出的今天日期为 1970 年 16 月 00 日。使用的 Long 是 1332026487。
    • 这是因为你可能不得不玩弄你在构造函数中传递的参数。您可以在 Jav API 中找到所有显示可能性
    • 这个答案:打印的是 minute 而不是 month;忽略文化的选择(这很重要);忽略时区的选择(这非常重要)。您真的必须考虑这些事情 - 忽略它们是没有帮助的 (IMO)。
    • @nexus490 提供的 Long 似乎以秒为单位(即“真实”纪元格式),而 Date 类的 Java 实现需要以毫秒为单位的时间。尝试将您的 Long 乘以 1000,它应该可以工作。除此之外,Jon Skeet 的评论 +1000。
    【解决方案2】:

    您可以从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);
    

    【讨论】:

    • 我会为 Timezone 参数添加什么?
    • @nexus490:那么您想在哪个时区表示值?例如,在一小时内它将是 my 时区的 18/03/2012,但在美国,它仍然是 17/03/2012,自纪元以来的毫秒数相同。正确的答案是什么?这只有知道,因为您知道您的应用程序要求(希望如此)。也许你想要UTC。也许您想要系统本地时区。也许你想要用户的家乡时区。
    • 感谢您强调这一点:“如果您从纪元开始有 ,则乘以 1000。”
    • 这是获得毫秒数的方式:long now = Instant.now().toEpochMilli(); 另外,import java.time.Instant;import java.text.SimpleDateFormat; 我是 Java 新手,我不得不在别处寻找完整的图片。为了完整起见,我在这里添加。
    【解决方案3】:
    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

    【讨论】:

    • 只是一个小注释,您需要将 HH:MM:ss.SSS 更改为 HH:mm:ss.SSS -- MM 是月份,而不是分钟。
    • 完美!谢谢
    【解决方案4】:

    如果方法应该是可移植的,最好使用默认(本地时间)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));
    }
    

    【讨论】:

      【解决方案5】:

      试试这个

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

      【讨论】:

        【解决方案6】:

        乔达时间

        如果您所说的纪元时间是指自 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 被各种计算机系统使用。

        【讨论】:

          【解决方案7】:

          是时候有人提供现代答案了(自 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);
          

          19 年 1 月 25 日

          请注意 sn-p 如何让您指定时区、语言(区域设置)以及所需格式的长短。

          链接

          【讨论】:

            【解决方案8】:

            您需要注意,Java 中的纪元时间以毫秒为单位,而您要转换的可能以秒为单位。确保两边的转换都以毫秒为单位,然后就可以从 Date 对象中获取日期参数了。

            【讨论】:

              【解决方案9】:

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

              【讨论】:

                【解决方案10】:

                试试这个...

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

                【讨论】:

                • 这个答案有 3 个问题。这个答案忽略了时区的关键问题。此答案使用的格式与问题中要求的格式不同。这个答案基本上重复了几个月前发布的其他三个答案。我在这里看不到任何增值。
                猜你喜欢
                • 2018-02-18
                • 2012-11-22
                • 2014-11-18
                • 2017-11-10
                • 1970-01-01
                • 1970-01-01
                • 2012-06-14
                • 2023-03-18
                • 2019-08-21
                相关资源
                最近更新 更多