简体   繁体   English

您可以在 GregorianCalendar 日期中设置 AM/PM 还是只能得到

[英]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.

相关问题 您可以格式化西班牙时间字符串 (Java) 以强制使用大写的 AM/PM 吗? - Can you format a Spanish time string (Java) to force AM/PM in caps? 您可以等于忽略案例集吗? - Can you equalsIgnoreCase a Set? GregorianCalendar:无法获得当地时间 - GregorianCalendar : can't get the local time hours 你能在Java Bitset中获得一组连续的位吗? - Can you get a set of consecutive bits set in Java Bitset? 无法设置公历日期和日期 - Unable to set GregorianCalendar Month and Date 当您尝试迭代的数组列表太大时,您可以获得 IndexOutOfBoundException 吗? 还是我的循环有问题? - Can you get IndexOutOfBoundException when the array list you are trying to iterate over is too large? Or is there something wrong with my loops? 在对象列表中,只有对象的唯一名称时如何获取对象 - In a list of objects, how can you get the object when you only have the unique name of the object 为什么GregorianCalendar根据系统时间(AM / PM)设置HOUR - Why GregorianCalendar sets HOUR depending on System time(AM/PM) java获取日期标记字段(上午/下午) - java get date marker field(am/pm) 使用 GregorianCalendar 操作和比较日期,无法使代码正常工作 - Manipulating and comparing dates with GregorianCalendar and can't get the code to work properly
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM