aspose file tools*
The moose likes Java in General and the fly likes Java Date and  time classes - Format AM_PM field Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java Date and  time classes - Format AM_PM field" Watch "Java Date and  time classes - Format AM_PM field" New topic
Author

Java Date and time classes - Format AM_PM field

Sudhir Srinivasan
Ranch Hand

Joined: Jun 08, 2011
Posts: 90
Hi,

The program uses the java date / time classes to display dates in default
format and the formats specified.

and its output


I have the following questions:

1. As per the Date javadoc, even
though the Date object represents an "epoch" (i.e start of a period, being, January 1,1970, 00:00:00 GMT)
with millisecond precision, in my program the toString() converts it to the local time (i.e IST,
being the time-zone of my country )and displays it in the default format (line no.7 in the output).
Given that 'epoch' is absolute (& not locale-specific) and the default locale of my JVM (as determined by
Locale.getDefault) shows en_US, how does the toString() override this to display date/time in my local time-zone?


2. While using the Calendar class & its subclass (GregorianCalendar), Is there a way to convert the internal integer
representation of AM_PM field to display am or pm accordingly in a date depending on the hour before noon or afternoon:

a) Calendar class: the instance - cal - of the class invokes the get() method to return the respective time fields. While Calendar.HOUR
field displays the hour (afternoon in this case in line no.17 in output) correctly, the Calendar.AM_PM field displays the int value 1 instead
of pm. How can this be implemented?

b) GregorianCalendar: I'm able to retrieve and display individual date / time fields like year, month, day , hour, minutes, second etc. with
the exception of AM_PM field (again!). The code snippet

returns the default epoch (line no.21 in output) which means either the field passed to the get() method is insufficient or wrong.

As a workaround,
i) I declared the variable am_pm as int
ii) Changed the above code snippet to
am_pm = date.get(Calendar.AM_PM);
where the instance 'date' of the GregorianCalendar returns the int value 1 assigned to variable in point i)
iii) Use if condition to display am or pm marker

The modified code

and its output

Returning to point b), Should I pass all the individual time fields to get() method to obtain the am / pm marker? (Note:
The Calendar javadoc has the set(), add() and
roll() methods for field manipulation. Can they be used for resolving the AM_PM field and if yes, how?.......the
javadoc is pretty confusing in this regard)
.

3. How to display am / pm marker using DateFormat class (as the javadoc
encourages use of the same for formatting and parsing date/time strings even though the SimpleDateFormat
class, being a subclass of DateFormat, returns am/pm marker internally as shown in line no.12 of output)?

Please excuse me for the one too many questions (just thought would get the answers at one go ).

Would much appreciate a response from the forum experts or members.

Thanks,
Sudhir
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2318
    
  49
1. As per the Date javadoc, even
though the Date object represents an "epoch" (i.e start of a period, being, January 1,1970, 00:00:00 GMT)
with millisecond precision, in my program the toString() converts it to the local time (i.e IST,
being the time-zone of my country )and displays it in the default format (line no.7 in the output).
Given that 'epoch' is absolute (& not locale-specific) and the default locale of my JVM (as determined by

There is a separate class used ie TimeZone to determine the time zone.

2. While using the Calendar class & its subclass (GregorianCalendar), Is there a way to convert the internal integer
representation of AM_PM field to display am or pm accordingly in a date depending on the hour before noon or afternoon:

Use a DateFormat class.

a) Calendar class: the instance - cal - of the class invokes the get() method to return the respective time fields. While Calendar.HOUR
field displays the hour (afternoon in this case in line no.17 in output) correctly, the Calendar.AM_PM field displays the int value 1 instead
of pm. How can this be implemented?

Internally the value is stored as an int with two possible values Calendar.AM and Calendar.PM

b) GregorianCalendar: I'm able to retrieve and display individual date / time fields like year, month, day , hour, minutes, second etc. with
the exception of AM_PM field (again!).

Be careful. Not all the values returned are as you would think, for instance months are 0 based ie January is 0, December is 11
You should not use these values to display times or dates, use a DateFormat class instead
 
 
subject: Java Date and time classes - Format AM_PM field