Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to convert LocalDate and LocalTime to java.util.date?

Tags:

threetenbp

I have a org.threeten.bp.LocalDate and a org.threeten.bp.LocalTime and I do need a java.util.date instance. Whats the best way to archieve this. I looked through DateTimeUtils but found no proper solution.

like image 513
Lukas Lechner Avatar asked Oct 23 '25 14:10

Lukas Lechner


1 Answers

Here is the better solution without using deprecated stuff but using the helper class DateTimeUtils:

// your input (example)
LocalDate date = LocalDate.of(2015, 4, 3);
LocalTime time = LocalTime.of(17, 45);

// the conversion based on your system timezone
Instant instant = date.atTime(time).atZone(ZoneId.systemDefault()).toInstant();
Date d = DateTimeUtils.toDate(instant);
System.out.println(d); // Fri Apr 03 17:45:00 CEST 2015

You need a timezone to make this conversion working. I have chosen the system timezone in the example given above but you are free to adjust the timezone to your needs.

like image 199
Meno Hochschild Avatar answered Oct 27 '25 04:10

Meno Hochschild



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!