【问题标题】:Convert milliSeconds To Gregorian Calendar将毫秒转换为公历
【发布时间】:2014-11-07 05:18:04
【问题描述】:

我想将长格式的毫秒转换为公历。 通过在网上搜索,我使用以下代码:

public static String getStringDate(int julianDate){
    GregorianCalendar gCal = new GregorianCalendar();
    Time gTime = new Time();
    gTime.setJulianDay(julianDate);
    gCal.setTimeInMillis(gTime.toMillis(false));
    String gString = Utils.getdf().format(gCal.getTime());
    return gString;
}

public static SimpleDateFormat getdf(){
    return new SimpleDateFormat("yyyy-MM-dd, HH:MM",Locale.US);
}

是的,代码有效,但我发现只有日期和小时是正确的,但分钟有错误。假设这件事发生在 2014-11-06, 14:00,它会给我 2014-11-06, 14:11。我想知道是否有任何解决方案可以修改它,或者不建议将时间转换为公历。非常感谢!

【问题讨论】:

    标签: android time calendar gregorian-calendar


    【解决方案1】:

    问题其实很简单, 修改 SimpleDateFormat("yyyy-MM-dd, HH:MM",Locale.US) 与

    SimpleDateFormat("yyyy-MM-dd, HH:mm",Locale.getDefault());

    解决问题

    【讨论】:

    • 仅供参考,java.util.Datejava.util.Calendarjava.text.SimpleDateFormat 等麻烦的旧日期时间类现在已被 java.time 类所取代。 ThreeTen-Backport 项目中的大部分 java.time 功能都向后移植到 Java 6 和 Java 7。在ThreeTenABP 项目中进一步适用于早期的Android。见How to use ThreeTenABP…
    【解决方案2】:

    tl;博士

    Instant.ofEpochMilli( millis )                  // Convert count-from-epoch into a `Instant` object for a moment in UTC.
        .atZone( ZoneId.of( "Pacific/Auckland" ) )  // Adjust from UTC to a particular time zone. Same moment, different wall-clock time. Renders a `ZonedDateTime` object.
        .format(                                    // Generate a String in a particular format to represent the value of our `ZonedDateTime` object.
            DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuu-MM-dd, HH:mm" )
        )
    

    java.time

    现代方法使用 java.time 类而不是那些麻烦的遗留类。

    将自 1970 年第一时刻 (1970-01-01T00:00Z) 的纪元引用以来的毫秒数转换为 Instant 对象。请注意,Instant 能够实现更精细的纳秒粒度。

    Instant instant = Instant.ofEpochMilli( millis ) ;
    

    那一刻在UTC。要调整到另一个时区,请应用ZoneId 以获取ZonedDateTime

    时区对于确定日期至关重要。对于任何给定的时刻,日期在全球范围内因区域而异。例如,Paris France 中午夜后几分钟是新的一天,而 Montréal Québec 中仍然是“昨天”。

    如果没有指定时区,JVM 会隐式应用其当前的默认时区。该默认值可能随时更改,因此您的结果可能会有所不同。最好将您想要/预期的时区明确指定为参数。

    continent/region 的格式指定proper time zone name,例如America/MontrealAfrica/CasablancaPacific/Auckland。切勿使用 3-4 个字母的缩写,例如 ESTIST,因为它们不是真正的时区,没有标准化,甚至不是唯一的 (!)。

    ZoneId z = ZoneId.of( "Africa/Tunis" ) ;
    ZonedDateTime zdt = instant.atZone( z ) ;
    

    使用DateTimeFormatter 对象生成所需格式的字符串。

    DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuu-MM-dd, HH:mm" , Locale.US ) ;
    String output = zdt.format( f ) ;
    

    关于java.time

    java.time 框架内置于 Java 8 及更高版本中。这些类取代了麻烦的旧 legacy 日期时间类,例如 java.util.DateCalendarSimpleDateFormat

    Joda-Time 项目现在位于maintenance mode,建议迁移到java.time 类。

    要了解更多信息,请参阅Oracle Tutorial。并在 Stack Overflow 上搜索许多示例和解释。规格为JSR 310

    从哪里获得 java.time 类?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-17
      • 2013-06-23
      • 2011-02-13
      • 2011-02-14
      相关资源
      最近更新 更多