• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java Date and time classes - Format AM_PM field

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
We must storm this mad man's lab and destroy his villanous bomb! Are you with me tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic