Reputation: 1301
I have two java.util.Date
instances which is contain date value and time value. now I want to combine these values to create single java.util.Date
instance representing the date and time.
here some example to make clear what I'd want :
Date date = 2015-06-01;
Date time = 22:30;
combine into :
Date dateTime = 2015-06-01 22:30;
I do some search and I found this question Combining java.util.Dates to create a date-time which is similar with my current issue. But the chosen answer on that question is deprecated.
Upvotes: 4
Views: 4266
Reputation: 40056
You can do it without JODA, by using Calendar
However, as you asked about JODA, here is the way to do in JODA:
// you want the date part from it
Date d = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse("2013-01-02 03:04:05");
// you want to time part from it
Date t = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse("2014-02-03 04:05:06");
LocalDate datePart = new LocalDate(d);
LocalTime timePart = new LocalTime(t);
LocalDateTime dateTime = datePart.toLocalDateTime(timePart);
Date result = dateTime.toDate();
// Or shrink the above 4 lines into one, as follow
// Date result = new LocalDate(d).toLocalDateTime(new LocalTime(t)).toDate();
System.out.println("result " + result);
// print out result Wed Jan 02 04:05:06 CST 2013
Upvotes: 6
Reputation: 241
Use a Calendar instead?
In particular, set(int year,
int month,
int date,
int hourOfDay,
int minute) and
if you want a Date, use getTime() ?
or convert the Date object to a Calendar object using the setTime(Date ..
) function from the Calendar class, extract the values (day, hours, minute) using functions from the Calendar class?
Upvotes: 1