Java使用Joda time将军用小时转换为民用小时

Java使用Joda time将军用小时转换为民用小时,java,android,date,datetime,jodatime,Java,Android,Date,Datetime,Jodatime,我试图转换一个int(表示军事时间小时)并将其转换为民用时间。例如,如果我有“13”小时,我想将其转换为“下午1:00”。以下是我一直在尝试的: private DateTimeFormatter hourFormat = DateTimeFormat.forPattern("hh:mm a"); private DateTimeFormatter dateFormat = DateTimeFormat.forPattern("YYYY-MM-dd"); int hour = 8; DateT

我试图转换一个int(表示军事时间小时)并将其转换为民用时间。例如,如果我有“13”小时,我想将其转换为“下午1:00”。以下是我一直在尝试的:

private DateTimeFormatter hourFormat = DateTimeFormat.forPattern("hh:mm a");
private DateTimeFormatter dateFormat = DateTimeFormat.forPattern("YYYY-MM-dd");

int hour = 8;
DateTime fullDate = new DateTime();
 String date = dateFormat.print(fullDate) + " " + Integer.toString(hour) + ":00";
String civilianTime = hourFormat.parseDateTime(date).toString();
日志:

java.lang.IllegalArgumentException: Invalid format: "2016-01-27 8:00" is malformed at "16-01-27 8:00"
at org.joda.time.format.DateTimeFormatter.parseDateTime(DateTimeFormatter.java:945)
Joda不允许我只是重新格式化小时,我得到了一个“8:00太短”类型的错误,所以我包括了日期,并将日期和时间组合成一个字符串,即使我不需要日期。我也尝试过在那里使用时区组件,但我得到了相同的错误。仅作为背景:int“hour”来自一个数据库查询的时间戳(UTC w/时区偏移),该查询会给出我需要的小时列表。所以我回来的时间已经考虑到了时区


本质上,我试图从一天中的“HH”小时(0-23)变为半天中的“HH:mm”小时(1-12)。我假设有时“HH”只是“H”,因为从数据库查询到应用程序时前导零会被删除。我用小时=8和小时=10尝试了这一点,结果出现了相同的错误。

由于要使用的注释之一,这就完成了:

private DateTimeFormatter hourFormat = DateTimeFormat.forPattern("h:mm a");   
int hour = 8;
String ltime = new LocalTime(hour,0).toString(hourFormat);
在格式模式中,Joda time的类表示要使用:

  • h
    (小写)表示半天的时钟小时数(1~12)
  • H
    (大写)表示一天中的小时数(0~23)

如果要为一位数值填充前导的
0
(零),请使用双精度
hh
。例如,获取
08:00
而不是
8:00
。解析时,可以接受任意数量的数字。

多亏了要使用的注释之一,这就完成了:

private DateTimeFormatter hourFormat = DateTimeFormat.forPattern("h:mm a");   
int hour = 8;
String ltime = new LocalTime(hour,0).toString(hourFormat);
在格式模式中,Joda time的类表示要使用:

  • h
    (小写)表示半天的时钟小时数(1~12)
  • H
    (大写)表示一天中的小时数(0~23)
如果要为一位数值填充前导的
0
(零),请使用双精度
hh
。例如,获取
08:00
而不是
8:00
。解析时,接受任意数量的数字。

tl;博士 使用java.time而不是Joda-time

LocalTime.of( 13 , 0 )
         .format( 
             DateTimeFormatter.ofLocalizedTime( FormatStyle.SHORT )
                              .withLocale( Locale.US ) 
         )
下午1:00

看这个

……或者

ZonedDateTime.of(
    LocalDate.now( ZoneId.of( "Pacific/Auckland" ) ) ,  // Get today’s date at this moment in this time zone.
    LocalTime.of( 13 , 0 ) ,                            // Specify the time-of-day given to us. In this example `13` for 1 PM is given to us.
    ZoneId.of( "Pacific/Auckland" )                     // Specify the time zone giving the context for this date-time. 
).format(                                               // Generate a string representing the value of this date-time object.
     DateTimeFormatter.ofLocalizedDateTime( FormatStyle.SHORT )
                      .withLocale( Locale.US )          // Localize using the language and cultural norms of the United States.
 )
2017年5月4日下午1:00

更新 该项目现在正在进行中,团队建议迁移到类。看

使用java.time 和类表示日期时间的每个部分,没有任何nor时区

int hour = 13 ;  // 24-hour clock, 0-23. So `13` is 1 PM. 
LocalTime lt = LocalTime.of( hour , 0 ); // ( hours , minutes )
lt.toString():13:00

让java.time为您本地化

DateTimeFormatter f2 = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.SHORT ).withLocale( Locale.US ) ;
String output2 = zdt.format( f2 );
要本地化,请指定:

  • 确定字符串的长度或缩写
  • 确定(a)用于翻译日名、月名等的人类语言,以及(b)决定缩写、大写、标点符号、分隔符等问题的文化规范

    DateTimeFormatter f=CalizedTime(FormatStyle.SHORT)的DateTimeFormatter.withLocale(Locale.US); 字符串输出=lt.format(f)

输出:下午1:00

时区对于确定日期至关重要。在任何一个特定的时刻,世界各地的日期都因地区而异。例如,中午夜后几分钟是新的一天,而中仍然是“昨天”

大陆/地区
的格式指定,例如,或
太平洋/奥克兰
。切勿使用3-4个字母的缩写,如
EST
IST
,因为它们不是真正的时区,也不是标准化的,甚至不是唯一的(!)

ld.toString():2017-01-23

为了到达时间轴上的特定点,我们应用时区(
ZoneId
对象)来获取
zoneDateTime

ZonedDateTime zdt = ZonedDateTime.of( ld , lt , z ) ;  // ( LocalDate , LocalTime , ZoneId )
zdt.toString():2017-01-23T13:00:00-05:00[美国/蒙特利尔]

生成所需格式的字符串。如上所述,让java.time为您本地化

DateTimeFormatter f2 = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.SHORT ).withLocale( Locale.US ) ;
String output2 = zdt.format( f2 );
2017年5月4日下午1:00

看这个

不是真正的“军事时间” 顺便说一句,这里的“12小时制”主要是美国的一种现象,因为美国是少数几个12小时制占主导地位的国家之一

世界各地的其他人通常都使用这两种方式,在临时事务中使用12小时制,在火车时刻表等关键事务中使用24小时制。根据我的经验,这是一种更明智的做法


关于java.time 该框架内置于Java8及更高版本中。这些类取代了麻烦的旧日期时间类,例如,&

该项目现已启动,建议迁移到类

要了解更多信息,请参阅。并搜索堆栈溢出以获得许多示例和解释。规格是

从哪里获得java.time类

  • ,及以后
    • 内置的
    • 标准JavaAPI的一部分,带有捆绑实现
    • Java9添加了一些次要功能和修复
    • 大部分java.time功能都在中向后移植到Java6和Java7
    • 该项目专门为Android采用了ThreeTen Backport(如上所述)
tl;博士 使用java.time而不是Joda-time

LocalTime.of( 13 , 0 )
         .format( 
             DateTimeFormatter.ofLocalizedTime( FormatStyle.SHORT )
                              .withLocale( Locale.US ) 
         )
下午1:00

看这个

……或者

ZonedDateTime.of(
    LocalDate.now( ZoneId.of( "Pacific/Auckland" ) ) ,  // Get today’s date at this moment in this time zone.
    LocalTime.of( 13 , 0 ) ,                            // Specify the time-of-day given to us. In this example `13` for 1 PM is given to us.
    ZoneId.of( "Pacific/Auckland" )                     // Specify the time zone giving the context for this date-time. 
).format(                                               // Generate a string representing the value of this date-time object.
     DateTimeFormatter.ofLocalizedDateTime( FormatStyle.SHORT )
                      .withLocale( Locale.US )          // Localize using the language and cultural norms of the United States.
 )
2017年5月4日下午1:00

更新 该项目现在正在进行中,团队建议迁移到类。看

使用java.time 和类表示日期时间的每个部分,没有任何nor时区

int hour = 13 ;  // 24-hour clock, 0-23. So `13` is 1 PM. 
LocalTime lt = LocalTime.of( hour , 0 ); // ( hours , minutes )
lt.toString():13:00

让java.time为您本地化

DateTimeFormatter f2 = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.SHORT ).withLocale( Locale.US ) ;
String output2 = zdt.format( f2 );
要本地化,请指定:

  • 确定字符串的长度或缩写
  • 确定(a)用于翻译日名、月名等的人类语言,以及(b)决定缩写、大写、标点符号、分隔符等问题的文化规范

    DateTimeFormatter f=CalizedTime(FormatStyle.SHORT)的DateTimeFormatter.withLocale(Locale.US); 字符串输出=lt.format(f)

输出:下午1:00

时区对于确定日期至关重要。对于任何g