kawtousse
kawtousse

Reputation: 4141

converting time from hh:mm:ss to hh:mm in java

I want to convert time to hh:mm from hh:mm:ss it comes from database (my sql) in the form of hh:mm:ss. I tried the following code but i didn't get what i want.

 try {

        s= HibernateUtil.currentSession();
        tx=s.beginTransaction();
        Query query = s.createQuery("select from Dailytimesheet dailytimesheet where dailytimesheet.IdDailyTimeSheet=6083 " );         

             for(Iterator it=query.iterate();it.hasNext();)
             {                                                                           
                               if(it.hasNext())
                               {

                                   Dailytimesheet object=(Dailytimesheet)it.next();
                                   String dt=object.getTimeFrom().toString();
                                   SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");  
                                   long ms=0;
                            try {
                                ms = sdf.parse(dt).getTime();
                                } 
                            catch (ParseException e) {e.printStackTrace();}
                                Time ts = new Time(ms);


         out.println("<h2>"+ts+"</h2>");                       

thanks for your help.

Upvotes: 1

Views: 17044

Answers (7)

CoolBeans
CoolBeans

Reputation: 20800

I would just do the conversion in the SQL in this case. Here is the MySql reference on date conversion. In your case, it should be something like this -

Blockquote DATE_FORMAT('2007-10-04 22:23:00', '%H:%i');

It looks like you are needing to do the conversion for each row you get from your query and it might be more cost effective to have it done by the database engine if you are retrieving many records.

Upvotes: 0

Mayank Gupta
Mayank Gupta

Reputation: 1

try {
    String strDate = object.getTimeFrom().toString(); 
    SimpleDateFormat sdf1 = new SimpleDateFormat("HH:mm:ss");
    SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm");
    Date date = sdf1.parse(strDate);
    System.out.println(sdf2.format(date));
} catch (ParseException e) {
    e.printStackTrace();
}

Upvotes: 0

BalusC
BalusC

Reputation: 1108632

You need to convert a java.util.Date object to a java.lang.String object representing the human readable time in the desired format using DateFormat#format(). You cannot convert it back to Date again (or any of its subclasses like java.sql.Time and so on) without losing the format. The format is namely not stored in Date. The Date only knows about the amount of milliseconds since the Epoch.

SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
java.util.Date date = object.getTimeFrom();
String string = sdf.format(date);
System.out.println(string);

Upvotes: 7

Michael Krelin - hacker
Michael Krelin - hacker

Reputation: 143051

Converting HH:MM:SS to HH:MM is best done by hhmmss.substring(0,5) ;-) Alternatively, you may prefer to format the data right in the SELECT statement.

Upvotes: 4

goedson
goedson

Reputation: 643

You should use the DateFormat's format method. Something like:

out.println("<h2>"+sdf.format(ts)+"</h2>"); 

Upvotes: 0

Matschie
Matschie

Reputation: 1247

What about trying the java.util.Calendar Class? You could do something like this:

Calendar c = new Calendar();
Calendar.setDate(new Date(object.getTimeFrom().toString()));
String date = Calendar.HOUR + ":" + Calendar.MINUTE

Or an I not getting your question?

Upvotes: 0

John Uckele
John Uckele

Reputation: 36

Perhaps you could add a new method to Dailytimesheet called getTimeWithoutSecondsFrom() which copies the date object and then sets the copy's seconds to zero before returning the date.

As an added benefit you can eschew the SimpleDateFormat sdf bit altogether then and the code will be easier to read.

The code might look like this:

public Date getTimeWithoutSecondsFrom()
{
    Date result = this.getTimeFrom();
    result.setSeconds(0);
    return(result);
}

Upvotes: 0

Related Questions