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