-1

I have a class called Lezione that has an attribute time of type Time. This attributes is filled with the value of the attribute time of the table Lezione in my database (mysql). Also, the attribute time in the database is of type TIME.

Now: I do a query on the db:

 select * from lezione as l

and then I create an object of type Lezione and I set all the attributes. For the time I do this:

l.settime(rs2.getTime(3));

And this Works.

When I try to print the value with l.gettime() I get the value (f.e.) 13:00:00

What I would like to have instead is 13:00

I tried with the expression

l.gettime().getHours()+":"+l.gettime().getMinutes()

(that uses depreciated methods) but it prints 13:0 (and I guess that if I would like to print 09:02 it would print 9:2)

So What can I do?

5 Answers 5

5

Use a simpledate formatter.

DateFormat formatter = new SimpleDateFormat("H:mm");
formatter.format( time ) ); // 13:00
0
2

I would also go with DateFormat.

But you can add leading zeros with

String.format("%02d", yournumber);

So in your case it would like:

l.gettime().getHours()+":"+String.format("%02d", l.gettime().getMinutes());
1

There are many ways to do this as you can see in the answers. Here is another way to display hours and minutes (and other details from Calendar):

GregorianCalendar date = new GregorianCalendar();
int  minutes = date.get(Calendar.MINUTE);
int  hours = date.get(Calendar.HOUR_OF_DAY); //HOUR_OF_DAY for the 24-hour clock
System.out.println("The time is: "+ hours+ ":" +minutes);
1

To format a Date/Time you should use a (Simple)DateFormat, for example:

DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
dateFormat.format(new Date());

This prints the current time in 24-hour format (HH) with seconds. To not print seconds, just change the format to "HH:mm".

4
  • I thought I leave something to the reader's imagination. Also, DateFormatter is an equally viable answer as opposed to DateFormat or SimpleDateFormat for that matter. Who knows what kind of application he/she is developing, could be a Java Swing app, who knows? Feb 9, 2013 at 10:20
  • 1
    A DateFormatter uses a DateFormat. There's no point in usign a DateFormatter outside of a Swing program. And you need a DateFormat to use it anyway.
    – JB Nizet
    Feb 9, 2013 at 10:23
  • I know, I know, I made a typo... So much for trying to talk you out of downvoting my answer ;) Feb 9, 2013 at 10:24
  • You'd better fix your answer if you want me to remove my downvote.
    – JB Nizet
    Feb 9, 2013 at 10:26
1

If you are only interested in printing the string from getTime, then you could do like this:

String str = rs2.getTime(3);
str = str.substring(0, str.length() - 3);
System.out.println(str);

Although, that might be a little ugly.

1
  • 1
    You probably mean .length()
    – Maroun
    Feb 9, 2013 at 10:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.