hi,
how to convert a date type in java to a time type.
thanks for help.
hi,
how to convert a date type in java to a time type.
thanks for help.
The Date object stores date and time, you can get the time out of a date with accessors.
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?
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");
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.
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: