开发者

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 Dai开发者_开发问答lytimesheet 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.


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);


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.


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);
}


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?


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

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


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();
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜