开发者

Converting a date to a time in Java

How can one convert a date type into a time type in Jav开发者_开发百科a?


Assuming based on your question history you want convert java.util.Date to java.sql.Time. You can just construct Time with the milliseconds as obtained from Date#getTime():

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

Also see the linked Javadocs. This is #1 source to look for answers to this kind of trivial questions.


The Date object stores date and time, you can get the time out of a date with accessors.


Use java.text.SimpleDateFormat

DateFormat dfm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
dfm.setTimeZone(TimeZone.getTimeZone("Europe/Zurich"));
Date a = dfm.parse("2007-02-26 20:15:00");
Date b = dfm.parse("2007-02-27 08:00:00");


If date is a Date object, then use:

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


Java Date and time classes can be a bit confusing. I wrote a tutorial covering the core classes. You can find it here:

http://tutorials.jenkov.com/java-date-time/index.html

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜