Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert sql date to string in Java8

Tags:

java-8

I am new to Java8

If I want to convert sqldate to string then what should I use?

1) Simpledateformat (which is easier) or
2) Convert the sql date to LocalDate and then use DateTimeFormatter

I want to know is it good practice to use Simpledateformat in Java8 when we have DateTimeFormatter?

like image 671
firstpostcommenter Avatar asked Apr 10 '26 17:04

firstpostcommenter


1 Answers

I would go with the second way, because the newer format engine DateTimeFormatter is immutable in contrast to SimpleDateFormat and also offers a more modern API in general. It is even more performant than the old stuff, especially in multi-threading-environment (although there are still quicker libraries outside according to my observations).

It is also very easy to convert java.sql.Date to LocalDate thanks to a built-in conversion method.

In addition: Since your SQL-date represents a plain calendar date, the type LocalDate is semantically a much better match than the type java.util.Date (used by SimpleDateFormat) because latter one represents a global instant which is quite different from a calendar date. So going the second way you don't need to bother with any time component which makes the problem/task easier to understand regarding your input.

like image 68
Meno Hochschild Avatar answered Apr 19 '26 11:04

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!