tags:

views:

108

answers:

6

hi,

how to convert a date type in java to a time type.

thanks for help.

A: 

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

plor
A: 

A java.util.Date already contains both date information (day, month, year, etc.) and time-of-day information (hour, minute, second, and millisecond).

What are you trying to do, that you can't?

Matt Ball
A: 

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");
BioXhazard
+1  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.

BalusC
A: 

If date is a Date object, then use:

Time time = new Time(date.getTime());
kiamlaluno
A: 

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

Jakob Jenkov