如何正确格式化java时间

时间:2014-12-12 08:42:26

标签: java date

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

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 个答案:

答案 0 :(得分:3)

我不认为这是不一致的。当您使用HH评估时间时,它将忽略aa位,因为它将输入评估为24小时,而aa位没有意义。但是,当您使用hh运行它时,它会将05:30:15 PM读为“下午五点半”并写入它将给出2014-12-01 17:30:15。以24小时的时间阅读05:30:15 PM,将其视为“早上五点半”,将PM位扔掉。

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

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

答案 1 :(得分:-1)

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。切勿使用诸如ESTIST之类的3-4字母缩写,因为它们不是真正的时区,不是标准化的,甚至不是唯一的(!)。

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.US& FormatStyle.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.DateCalendar和& SimpleDateFormat

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

要了解详情,请参阅Oracle Tutorial。并搜索Stack Overflow以获取许多示例和解释。规范是JSR 310

使用符合JDBC driver或更高版本的JDBC 4.2,您可以直接与数据库交换 java.time 对象。不需要字符串也不需要java.sql。* classes。

从哪里获取java.time类?

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