Android日历设置Am或Pm时间不是24小时

时间:2012-05-15 21:02:53

标签: java android

我现在有一个脚本,它始终以AM PM格式传递。但我不知道如何设置日历,因为它只接受24小时格式,就像我编写代码一样。如何将时间转换为24小时或输入12小时格式?

         calendar.set(Calendar.HOUR_OF_DAY, HourValue);
         calendar.set(Calendar.MINUTE, MinuteValue);
         calendar.set(Calendar.SECOND, 0);
         calendar.add(Calendar.MINUTE, -356);

4 个答案:

答案 0 :(得分:9)

http://developer.android.com/reference/java/util/Calendar.html#HOUR

calendar.set(calendar.HOUR,HourValue),而不是HOUR_OF_DAY所以如果您提供24小时输入,则将其转换为12小时格式

答案 1 :(得分:2)

0表示上午,1表示下午 Calendar c = Calendar.getInstance(); c.set(Calendar.YEAR, year); c.set(Calendar.MONTH, month); c.set(Calendar.DAY_OF_MONTH, day); c.set(Calendar.AM_PM, 1); c.set(Calendar.HOUR, hour); c.set(Calendar.MINUTE, minute); c.set(Calendar.SECOND, 00); c.set(Calendar.MILLISECOND, 00);

答案 2 :(得分:1)

int am_pm=c.get(Calendar.AM_PM);
String time=c.HOUR + ((am_pm==Calendar.AM)?"am":"pm"))

你可以试试上面的内容。

答案 3 :(得分:0)

TL;博士

ZonedDateTime.of( 
    LocalDate.now( ZoneId.of( "Pacific/Auckland" ) )  // Determine today’s date for a given region.
        .plusDays( 1 ) ,                              // Determine day-after, tomorrow.
    LocalTime.parse(                                  // Parse string with AM/PM into `LocalTime` object.
        "6:51:35 PM" , 
        DateTimeFormatter.ofPattern( "h:m:s a" ).withLocale( Locale.US ) 
    ) ,                                               // Combine date with time-of-day…
    ZoneId.of( "Pacific/Auckland" )                   // …and zone…
)                                                     // …to produce a `ZonedDateTime`, a point on the timeline.
.toString()                                           // Generate a String in standard ISO 8601 format, wisely extended by appending the name of the time zone in square brackets.
  

2018-02-02T18:51:35 + 13:00 [太平洋/奥克兰]

java.time

将12小时格式的传入字符串解析为LocalTime对象。

String input = "6:51:35 PM" ; 
DateTimeFormatter f = DateTimeFormatter.ofPattern( "h:m:s a" ).withLocale( Locale.US ) ;

LocalTime lt = LocalTime.parse( input , f ) ;

要设置闹钟,您需要一个与此时间相结合的日期以及一个时区来确定实际时刻。

LocalDate类表示没有时间且没有时区的仅限日期的值。

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

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

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

ZoneId z = ZoneId.of( "America/Montreal" ) ;  
LocalDate today = LocalDate.now( z ) ;

如果要使用JVM的当前默认时区,请求它并作为参数传递。如果省略,则隐式应用JVM的当前默认值。最好是明确的。

ZoneId z = ZoneId.systemDefault() ;  // Get JVM’s current default time zone.

通过添加一天确定明天。

LocalDate tomorrow = today.plusDays( 1 ) ;

应用时间和所需时区以生成ZonedDateTime。如果由于夏令时(DST)等异常,您的时间在该区域中的该日期无效,则课程会自动调整。阅读文档以确保您理解并同意这些调整的行为。

ZonedDateTime zdt = ZonedDateTime.of( tomorrow , lt , z ) ; // Get a specific moment in time given a particular date, time-of-day, and zone.

关于 java.time

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

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

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

从哪里获取java.time类?