Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Joda: Convert Date and Time to DateTime

Is there a way to convert a Time and Date variable to a DateTime?

I have a period between two DateTime variables, for each Date in that period I want to store a period IN that Date with a begin DateTime and end DateTime, so a day can have multiple periods defined by a DateTime.

Can't seem to figure out how to combine Date and Time to a DateTime.

Thanks in advance!

like image 397
Luc Avatar asked Nov 30 '22 13:11

Luc


1 Answers

Plain java Date and Joda-Time DateTime should serve the purpose.

Date date = new Date(); // java.util.Date; - This date has both the date and time in it already.
DateTime dateTime = new DateTime(date);

For more info about Joda-Time.

If you have two String objects, where 1 holds the Date and the other Time, you can combine the 2 Strings and use a SDF to parse it and get the Date object, which you can then convert to DateTime.

like image 109
Rahul Avatar answered Dec 02 '22 03:12

Rahul