Formatting a time (not current time)

B

B. Schad

Hi,

to format a certain time (say 14:15) what would you have to do? I found
the following way:

Calendar cal = Calendar.getInstance();
cal.set (Calendar.HOUR_OF_DAY, 14);
cal.set (Calendar.MINUTE, 15);
Date date = cal.getTime();
DateFormat df = DateFormat.getTimeInstance();

StringBuffer msg = new StringBuffer();
msg.append (df.format (date));

The problem seemed to be that it is not possible to directly format a
Calendar time. Do I really have to create a (somewhat incomplete) Date
from the Calender time first?

Kind regards,
B. Schad
 
T

Tim

why not use Date directly?

Date d = new Date();

d.setHours(14);

d.setMinutes(15);

DateFormat df = DateFormat.getTimeInstance();

StringBuffer msg = new StringBuffer();

msg.append(df.format(d));
 
T

Thomas Weidenfeller

B. Schad said:
to format a certain time (say 14:15) what would you have to do?
I found
the following way:

Yes, it is a mess. Sadly that is how it is supposed to be done. You can
play a little bit and e.g. create an own SimpleDateFormat instead of
using DateFormat.getTimeInstance(), but that doesn't change much
(DateFormat.getTimeInstance() also returns a SimpleDateFormat).

The really annoying thing is that SimpleDateFormat uses a Calendar
internally for separating e.g. hours and minutes. But there is no
format method that allows to provide an own Calendar for formating.
And no, setCalendar() does not what one would hope.

/Thomas
 
D

David Zimmerman

B. Schad said:
Hi,

to format a certain time (say 14:15) what would you have to do? I found
the following way:

Calendar cal = Calendar.getInstance();
cal.set (Calendar.HOUR_OF_DAY, 14);
cal.set (Calendar.MINUTE, 15);
Date date = cal.getTime();
DateFormat df = DateFormat.getTimeInstance();

StringBuffer msg = new StringBuffer();
msg.append (df.format (date));

Calendar.getTime() returns a Date, use that in the format
 
R

Roedy Green

The problem seemed to be that it is not possible to directly format a
Calendar time. Do I really have to create a (somewhat incomplete) Date
from the Calender time first?

It is a bit odd they left out a convenience format( Calendar )
method, but what do you expect from goofs who create code like this:

now.getTime().getTime();

where one getTime gets a Date object and the other a long.

They are SO muddled about vocabulary in those classes! I fulminate
about it in http://mindprod.com/jgloss/gotchas.html#DATE
 
B

B. Schad

The problem seemed to be that it is not possible to directly format a
It is a bit odd they left out a convenience format( Calendar )

When calling Calendar.getTime() it is tried to find a time instance that
matches the Calendar (which is different depending on the Locale). The
information I provide is incomplete so getTime() probably guesses the
missing information (year, month, day and so on).

The problem is that all this is not needed to format the time I provide
in the code. Hopefully finding a time instance does not change hour and
minute. So this is not just convenience missing. :-(
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top