Java 您可以在 GregorianCalendar 日期中设置 AM/PM 还是只能得到
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9667080/
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
Can you set AM/PM in a GregorianCalendar date or is it something you can only get
提问by daveb
I had a look at the java api about GregorianCalendar and did not see any way of setting the am/pm in its constructor. Can you set AM/PM in a GregorianCalendar date or is it something you can only get using a get method on the calendar. Does it handle all of this automatically. I am looking to take the am/pm and output it in my toString for a class which has a date object. I was going to use the get method on the calendar to achieve this. I understand the am/pm is an int that is 0 or 1.
我查看了有关 GregorianCalendar 的 java api,但没有看到任何在其构造函数中设置 am/pm 的方法。您可以在 GregorianCalendar 日期中设置 AM/PM 还是只能使用日历上的 get 方法来获取。它是否自动处理所有这些。我正在寻找 am/pm 并将其输出到我的 toString 中,用于具有日期对象的类。我打算使用日历上的 get 方法来实现这一点。我知道 am/pm 是一个 0 或 1 的整数。
Are all hours in the 24 hour format in Gregorian and does it automatically determines the am and pm?
公历中的所有小时都是 24 小时格式吗,它会自动确定上午和下午吗?
采纳答案by MartinK
Calendar.get(Calendar.HOUR);
Gives the hour (0-12) for AM/PM format.
为 AM/PM 格式提供小时 (0-12)。
Calendar.get(Calendar.HOUR_OF_DAY);
Gives the hour ranging from 0-24.
给出从 0 到 24 的小时。
It does the conversion on its own. You don't need to tell it.
它自己进行转换。你不需要告诉它。
cal.set( Calendar.AM_PM, Calendar.AM )
Will/Could changethe point time this calendar object represents. (If it's 1:00PM you will have 1:00AM afterwards). This is true for GregorianCalendar
(see comment to question by Peter Cetinski).
将/可能更改此日历对象所代表的时间点。(如果是下午 1:00,则之后是凌晨 1:00)。这适用于GregorianCalendar
(见 Peter Cetinski 对问题的评论)。
Btw/Hint imo you should use a DateFormat
to ouput yourpreferred format.
顺便说一句/提示imo您应该使用aDateFormat
来输出您喜欢的格式。
回答by Peter Cetinski
Calendar cal = new GreogorianCalendar();
cal.set( Calendar.AM_PM, Calendar.AM );
回答by MartinK
Simply I did this :
只是我这样做了:
Calendar calendar = new GregorianCalendar();
int seconds = calendar.get(Calendar.SECOND);
int minutes = calendar.get(Calendar.MINUTE);
int hour = calendar.get(Calendar.HOUR);
int am_pm = calendar.get(Calendar.AM_PM);
String ampm = "ampm";
if (am_pm == 0) {
ampm = "AM";
} else {
ampm = "PM";
}
jLabel1.setText(hour + ":" + minutes + ":" + seconds + " " + ampm);