3

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?

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.