How to display time ?

V

vertigo

Hello
i tried:
Calendar c = Calendar.getInstance();
Date d = c.getTime();
text.append(c.HOUR+":"+c.MINUTE+":"+c.SECOND); //1
text.append(d.getHours()+":"+d.getMinutes()+":"+d.getSeconds());//2
text.append(d.getTime());//3

in 1 i receive always: 10:12:13
in 2 functions are deprecated.
in 3 i do not know how to change long to String(data).

COuld anybody help ? How to simply dipslay time ?

Thanx
Michal
 
M

Michael Borgwardt

vertigo said:
i tried:
Calendar c = Calendar.getInstance();
Date d = c.getTime();
text.append(c.HOUR+":"+c.MINUTE+":"+c.SECOND); //1
text.append(d.getHours()+":"+d.getMinutes()+":"+d.getSeconds());//2
text.append(d.getTime());//3

in 1 i receive always: 10:12:13

Those are numerical constants to be used as parameters for methods of the
Calendar class.
in 2 functions are deprecated.

And the API tells you what methods replace them, though it can't
tell you that you shouldn't even be trying to use those methods.
in 3 i do not know how to change long to String(data).

COuld anybody help ? How to simply dipslay time ?

Use java.text.SimpleDateFormat
 
S

Stefan Waldmann

vertigo said:
Hello
i tried:
Calendar c = Calendar.getInstance();
Date d = c.getTime();
text.append(c.HOUR+":"+c.MINUTE+":"+c.SECOND); //1
text.append(d.getHours()+":"+d.getMinutes()+":"+d.getSeconds());//2
text.append(d.getTime());//3

in 1 i receive always: 10:12:13
in 2 functions are deprecated.
in 3 i do not know how to change long to String(data).

COuld anybody help ? How to simply dipslay time ?

Thanx
Michal

1: you are using the Constants of the Calendar interface, which are
always the same, because they are final. (java styleguide: constants
which can't be changed are always in UPPERCASE)

2: if you would have read the Date javadoc, you would now know, which
methods you should use instead of the deprecated ones:

text.append(c.get(Calendar.HOUR));
text.append(":");
text.append(c.get(Calendar.MINUTE));
text.append(":");
text.append(c.get(Calendar.SECOND));

3: a long is not what you need, so forget d.getTime()


You could also use a SimpleDateFormat from the java.text package
which would result in the shortest code:

DateFormat df = new SimpleDateFormat("HH:mm:ss");
String date = df.format(c.getTime());

(not tested, but should work)

HTH

Regards, Stefan
 
T

Thomas Kellerer

1: you are using the Constants of the Calendar interface, which are
always the same, because they are final. (java styleguide: constants
which can't be changed are always in UPPERCASE)
Wait. The constants have nothing to do with the fact that he will see 10:12:13
each time he outputs the value of d.

Michal assumes that an instance of Date updates itself automatically so that
each time you call getTime() you'll get the current time (but actually you'll
get the time provided when creating the instance)

Thomas
 
S

Stefan Waldmann

Thomas said:
Michal assumes that an instance of Date updates itself automatically so
that each time you call getTime() you'll get the current time (but
actually you'll get the time provided when creating the instance)

Thomas

Then someone should tell him that Date and Calendar objects DON'T update
themselves ;-)

Regards,
Stefan
 
M

Michael Borgwardt

Thomas said:
Wait. The constants have nothing to do with the fact that he will see
10:12:13 each time he outputs the value of d.

Sure they have; that's exactly the reason for the unexpected behaviour.
Michal assumes that an instance of Date updates itself automatically so
that each time you call getTime() you'll get the current time

I don't see any indication for this.
 
G

Glenn

vertigo said:
Hello
i tried:
Calendar c = Calendar.getInstance();
Date d = c.getTime();
text.append(c.HOUR+":"+c.MINUTE+":"+c.SECOND); //1
text.append(d.getHours()+":"+d.getMinutes()+":"+d.getSeconds());//2
text.append(d.getTime());//3

in 1 i receive always: 10:12:13
in 2 functions are deprecated.
in 3 i do not know how to change long to String(data).

COuld anybody help ? How to simply dipslay time ?

Thanx
Michal


Date date = new Date();
SimpleDateFormat sdf = new
SimpleDateFormat("dd-MMM-yyyy_HH.mm.ss.SSS_zzz");
String dateString = sdf.format(date);

and use the String however you wish. The time format syntax can be
changed to what you want as specified in the API.

The reason behind the date being in the above example is that I use
this syntax for creating unique names for flat files. Just remove
dd-MMM-yyyy_ and you have the time down to the millisecond.


- Glenn
 

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

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top