views:

265

answers:

7

hi every one, I want to force the print screen of time like => hh:mm it comes from database (my sql) like hh:mm:ss. I tryed the following but i didn't got want 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.

A: 

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);
}
John Uckele
A: 

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?

A: 

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

out.println("<h2>"+sdf.format(ts)+"</h2>"); 
goedson
+3  A: 

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.

Michael Krelin - hacker
+1 for most pragmatic solution
stacker
+3  A: 

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);
BalusC
the preffered solution and the easiest one.thanks
kawtousse
A: 
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();
}
Mayank Gupta
A: 

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.

CoolBeans