java Android 日历设置上午或下午时间不是 24 小时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10608845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 01:48:41  来源:igfitidea点击:

Android Calendar Set Am or Pm time not 24 Hour

javaandroid

提问by Somk

I have a script at the moment that is always being passed the time in an AM PM format. But I do not know how to set the calendar with this time as it only accepts 24 hour format the way I have the code written. How would I convert the time to 24 hour or input a 12 hour format?

我现在有一个脚本,它总是以 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);

回答by Shrikanth

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

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

calendar.set(calendar.HOUR,HourValue) , instead of HOUR_OF_DAY so if you give a 24 hour input it converts it into 12 hour format

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

回答by deepan

0 for am and 1 for pm 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);

上午 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);

回答by j2emanue

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

you can try the above.

你可以试试上面的。

回答by Basil Bourque

tl;dr

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[Pacific/Auckland]

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

java.time

时间

Parse that incoming string in 12-hour format into a LocalTimeobject.

将 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 ) ;

To set your alarm, you'll need a date combined with this time-of-day along with a time zone to determine an actual moment.

要设置闹钟,您需要将日期与当前时间以及时区相结合来确定实际时刻。

The LocalDateclass represents a date-only value without time-of-day and without time zone.

LocalDate级表示没有时间一天和不同时区的日期,唯一的价值。

A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris Franceis a new day while still “yesterday” in Montréal Québec.

时区对于确定日期至关重要。对于任何给定时刻,日期因地区而异。例如,在法国巴黎午夜过后几分钟是新的一天,而在魁北克蒙特利尔仍然是“昨天” 。

If no time zone is specified, the JVM implicitly applies its current default time zone. That default may change at any moment, so your results may vary. Better to specify your desired/expected time zone explicitly as an argument.

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

Specify a proper time zone namein the format of continent/region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. Never use the 3-4 letter abbreviation such as ESTor ISTas they are nottrue time zones, not standardized, and not even unique(!).

以、、 或等格式指定正确的时区名称。永远不要使用 3-4 个字母的缩写,例如或因为它们不是真正的时区,不是标准化的,甚至不是唯一的(!)。continent/regionAmerica/MontrealAfrica/CasablancaPacific/AucklandESTIST

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

If you want to use the JVM's current default time zone, ask for it and pass as an argument. If omitted, the JVM's current default is applied implicitly. Better to be explicit.

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

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

Determine tomorrow by adding a day.

通过添加一天来确定明天。

LocalDate tomorrow = today.plusDays( 1 ) ;

Apply the time-of-day and desired time zone to produce a ZonedDateTime. If your time-of-day is not valid for that date in that zone because of anomalies such as Daylight Saving Time (DST), the class automatically adjusts. Read the doc to be sure you understand and agree to the behavior of those adjustments.

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

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


About java.time

关于java.time

The java.timeframework is built into Java 8 and later. These classes supplant the troublesome old legacydate-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

java.time框架是建立在Java 8和更高版本。这些类取代了麻烦的旧的遗留日期时间类,例如java.util.Date, Calendar, & SimpleDateFormat

The Joda-Timeproject, now in maintenance mode, advises migration to the java.timeclasses.

现在处于维护模式Joda-Time项目建议迁移到java.time类。

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

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

Where to obtain the java.time classes?

从哪里获得 java.time 类?