首页 文章

如何正确格式化java时间

提问于
浏览
1

下面是我的格式化时间代码

public class FormatTime {

    public static void main(String[] args) throws Exception {
        FormatTime ft = new FormatTime();
        System.out.println(ft.evaluate("12/01/2014 05:30:15 PM","MM/dd/yyyy hh:mm:ss aa", "yyyy-MM-dd HH:mm:ss"));  
    }

    public String evaluate(String time,String iFormat ,String f) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat(f);
        SimpleDateFormat inFormat = new SimpleDateFormat(iFormat);

        Date date=inFormat.parse(time);
        String fechaNueva = format.format(date);
        return fechaNueva;
    }

}

这个程序的输出正如预期的那样给出 2014-12-01 17:30:15 .
但是当我在iFormat中将hh替换为HH(与outputformat中相同)时,它以12格式输出 2014-12-01 05:30:15
如果我将两者都转换为hh,也会发生同样的情况 . 为什么会出现这种类型的不一致?

2 回答

  • -1

    我不相容't think it' . 当您使用 HH 评估时间时,它将忽略 aa 位,因为它将输入评估为24小时,而 aa 位没有意义 . 但是,当您使用 hh 运行它时,它将 05:30:15 PM 读为"half five in the afternoon"并写入它将给出 2014-12-01 17:30:15 . 读取 05:30:15 PM 作为24小时时间,将其读作"half five in the morning",将 PM 位扔掉 .

    当两种格式都使用 hh 时,您将以12小时格式阅读和书写 . 为了理解,您还需要将 aa 位添加到输出格式 .

    我希望以有道理的方式回答你的问题:)

  • 3

    tl;博士

    LocalDateTime.parse(            // Parse as a date-time lacking time zone or offset-from-UTC.
        "12/01/2014 05:30:15 PM" ,  // Define a formatting pattern to match input. Case-sensitive formatting code. Use `h` lowercase for 12-hour clock, 0-12. Use uppercase `H` for 24-hour clock, 0-23.
        DateTimeFormatter.ofPattern( "MM/dd/uuuu hh:mm:ss a" , Locale.US )
    ).format(                       // Generate a String in specific format. Generally better to let java.time localize automatically.
        DateTimeFormatter.ofPattern( "MM/dd/uuuu hh:mm:ss a" , Locale.US )
    )
    

    java.time

    现代方法使用java.time类来取代麻烦的旧日期时间类 .

    以UTC格式获取当前时刻 . Instant类表示UTC中时间轴上的一个时刻,分辨率为nanoseconds(小数部分的最多九(9)位数) .

    Instant instant = Instant.now() ;
    

    要通过特定区域(时区)的人使用的挂钟时间镜头看到同一时刻,请应用 ZoneId 以获得 ZonedDateTime .

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

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

    要在其中任何一个上生成标准ISO 8601格式的字符串,请调用 toString . 在配对/生成字符串时,java.time类默认使用标准格式 . 因此无需指定格式化模式 .

    如果您需要其他格式,请使用 DateTimeFormatterDateTimeFormatterBuilder 类 . 您可以指定格式化模式,但更容易让java.time自动本地化 .

    要进行本地化,请指定:

    • FormatStyle确定字符串应该多长或缩写 .

    • Locale确定(a)翻译日期名称,月份名称等的人类语言,以及(b)决定缩写,大写,标点符号,分隔符等问题的文化规范 .

    例:

    Locale l = Locale.FRENCH ; 
    DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.FULL ).withLocale( l );
    String output = zdt.format( f );
    

    mercredi14février2018à00:59:07 heure normale d'Europe centrale

    或者, Locale.USFormatStyle.SHORT .

    2/14 / 18,1:01 AM

    您输入的自定义格式为:

    String input = "12/01/2014 05:30:15 PM" ;
    DateTimeFormatter f = DateTimeFormatter.ofPattern( "MM/dd/uuuu hh:mm:ss a" , Locale.US ) ;
    

    该输入字符串缺少任何时区指示符或与UTC的偏移量 . 所以这不是时刻,也不是时间轴上的特定点 . 它代表了对大约26-27小时范围内潜在时刻的模糊概念 . 因此,我们将此输入解析为缺少任何区域/偏移概念的 LocalDateTime .

    LocalDateTime ldt = LocalDateTime.parse( input , f ) ;
    

    ldt.toString():2014-12-01T17:30:15

    以该格式生成字符串 .

    String output = ldt.format( f ) ;
    

    12/01/2014 05:30:15 PM

    正如其他人所解释的那样,格式代码区分大小写 . 如果您想要24小时制,请使用大写 H . 对于12小时的时间,请使用小写 h .

    请注意,java.time中的格式代码与旧版 SimpleDateFormat 的格式代码相近,但不完全相同 . 研究文档并搜索Stack Overflow以获取许多示例 .

    将日期时间值作为文本交换时,请遵循标准ISO 8601格式 .


    关于java.time

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

    现在位于maintenance modeJoda-Time项目建议迁移到java.time类 .

    要了解更多信息,请参阅Oracle Tutorial . 并搜索Stack Overflow以获取许多示例和解释 . 规格是JSR 310 .

    使用JDBC driverJDBC 4.2或更高版本兼容,您可以直接与数据库交换java.time对象 . 不需要字符串也不需要java.sql . *类 .

    从哪里获取java.time类?

    ThreeTen-Extra项目使用其他类扩展了java.time . 该项目是未来可能添加到java.time的试验场 . 您可以在这里找到一些有用的类,例如IntervalYearWeekYearQuartermore .

相关问题