firstpostcommenter
firstpostcommenter

Reputation: 2911

Convert sql date to string in Java8

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?

Upvotes: 3

Views: 1301

Answers (1)

Meno Hochschild
Meno Hochschild

Reputation: 44061

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.

Upvotes: 4

Related Questions