2014-09-26 58 views
-5

我有像“21:00:00”那样的字符串,这是时间。我如何将此字符串转换为有时间使用的日期类型。相应的hibernate映射是日期类型,mysql字段是时间。在java中转换字符串时间日期类型

+0

即使是['java.sql.Time'](http://docs.oracle.com/javase/7/docs/api/java/sql/ Time.html)类有一个日期。 – 2014-09-26 12:26:45

+0

这就像在谷歌搜索5秒 – mlethys 2014-09-26 12:32:09

回答

0

java.sql.Time你从你的时间字段在MySQL中检索正确的值。

import java.io.IOException; 
import java.sql.Time; 
import java.text.ParseException; 
import java.text.SimpleDateFormat; 
import java.util.Date; 



public class teste { 

    /** 
    * @param args 
    * @throws IOException 
    */ 
    public static void main(final String[] args) throws IOException { 

     SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); 
     try { 
      Date data = sdf.parse("21:00:00"); 

      Time time = new Time(data.getTime()); 

      System.out.println(time); 
     } catch (ParseException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

    } 
} 

输出是

21:00:00 
相关问题