[英]Can you set AM/PM in a GregorianCalendar date or is it something you can only get
I had a look at the java api about GregorianCalendar and did not see any way of setting the am/pm in its constructor.我查看了有关 GregorianCalendar 的 java api,但没有看到任何在其构造函数中设置 am/pm 的方法。 Can you set AM/PM in a GregorianCalendar date or is it something you can only get using a get method on the calendar.您可以在 GregorianCalendar 日期中设置 AM/PM 还是只能使用日历上的 get 方法来获取。 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.我正在寻找 am/pm 并将其输出到我的 toString 中,用于具有日期对象的类。 I was going to use the get method on the calendar to achieve this.我打算使用日历上的 get 方法来实现这一点。 I understand the am/pm is an int that is 0 or 1.我知道 am/pm 是一个 0 或 1 的整数。
Are all hours in the 24 hour format in Gregorian and does it automatically determines the am and pm?公历中的所有小时都是 24 小时格式吗,它会自动确定上午和下午吗?
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 change the point time this calendar object represents.将/可能更改此日历对象所代表的时间点。 (If it's 1:00PM you will have 1:00AM afterwards). (如果是下午 1:00,则之后是凌晨 1:00)。 This is true for GregorianCalendar
(see comment to question by Peter Cetinski).这对于GregorianCalendar
是正确的(请参阅 Peter Cetinski 对问题的评论)。
Btw/Hint imo you should use a DateFormat
to ouput your preferred format.顺便说一句/提示imo您应该使用DateFormat
来输出您喜欢的格式。
Calendar cal = new GreogorianCalendar();
cal.set( Calendar.AM_PM, Calendar.AM );
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.