Can you set AM/PM in a GregorianCalendar date or is it something you can only get
Asked Answered
I

3

8

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.

Are all hours in the 24 hour format in Gregorian and does it automatically determines the am and pm?

Improvvisatore answered 12/3, 2012 at 12:33 Comment(0)
P
43
Calendar.get(Calendar.HOUR);

Gives the hour (0-12) for AM/PM format.

Calendar.get(Calendar.HOUR_OF_DAY);

Gives the hour ranging from 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). This is true for GregorianCalendar (see comment to question by Peter Cetinski).

Btw/Hint imo you should use a DateFormat to ouput your preferred format.

Plutus answered 12/3, 2012 at 12:52 Comment(2)
Thanks MartinK,Much appreciated. I think I will use HOUR_OF_DAY as that sounds best for what I am doing. Thanks I will use the DateFormat. I have been using elsewhere in my code. Many Thanks. Thanks also Dilum and PeterImprovvisatore
If you set AM PM that will also change the time millis.Suppress
G
6
Calendar cal = new GreogorianCalendar();   
cal.set( Calendar.AM_PM, Calendar.AM );
Gresham answered 12/3, 2012 at 12:38 Comment(4)
@Jigar/@Peter, do you know whether this is overridden if cal.set(Calendar.HOUR_OF_DAY, ...) is called after setting AM/PM?Hochstetler
Calls to set are for the most part cumulative. However, setting a field may affect other fields depending on the field and implementation of the Calendar. See docs.oracle.com/javase/6/docs/api/java/util/Calendar.htmlGresham
Hi Dilum and Peter , I am not overriding the cal.set(Calendar.HOUR_OF_DAY,...). At the moment I am not setting AM/PM either. I was able to set gregorian calender date as 24 hour but was now wondering how best to get the AM/PM to display in my toString methods. In my toString I print my getmethod which returns the date/time of the date object. It doesn't at the moment return am or pm.Improvvisatore
Calendar.get(AM_PM) returns 0 or 1 for me, which is equal to Calendar.AM or Calendar.PM.Plutus
L
1

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);
Lifetime answered 7/1, 2019 at 18:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.