0

I tried searching answer for different formats of ISO 8601 date only but not able to identify answer or material which contains these details

I am trying to receive ISO 8601 date format(date only as its date of birth) string and convert it into date object and later persist in dB. Is yyyy-MM-dd only date format that is considered as ISO 8601 date format. If so I can handle this accordingly but is there multiple date ISO 8601 date formats, if so can you please guide what is the best way to parse given string and convert to date object in Java

2
  • 2
    According to the standard if you only require date (without time), then the format is only yyyy-MM-dd, e.g. 2020-04-05 Commented Apr 5, 2020 at 18:51
  • 1
    Have a look here (especially the "Predefined Formatters" section) : docs.oracle.com/javase/8/docs/api/java/time/format/…, you have some explanations and examples of format SO 8601 Commented Apr 5, 2020 at 18:55

1 Answer 1

2

Yes, yyyy-MM-dd is the ISO 8601 format for a date without time of day and without time zone.

To store into an SQL database using either a JDBC 4.2 or later driver or a modern JPA implementation such as Hibernate 5, you have got nothing to use a Date object for (no matter if you meant java.util.Date or java.sql.Date). I recommend that instead you use java.time, the modern Java date and time API. The class to use for a date without time of day is LocalDate. And you can store one into your database without any conversion.

LocalDate parses ISO 8601 format as its default, that is, without any explicit formatter:

    String receivedString = "1962-11-27";
    LocalDate dateOfBirth = LocalDate.parse(receivedString);
    System.out.println("Date of birth is " + dateOfBirth);

LocalDate also gives ISO 8601 format back when we print it:

Date of birth is 1962-11-27

I haven’t given you the full story yet, though. yyyy-MM-dd is the extended format that is by far most often used. There is also a basic format that is compacter in that it leaves out the hyphens: yyyyMMdd. I suggest that you insist on getting your date string in the extended format. If you cannot do that, you will have to specify a formatter for parsing:

    String receivedString = "19621127";
    LocalDate dateOfBirth = LocalDate.parse(
            receivedString, DateTimeFormatter.BASIC_ISO_DATE);

This will give you a LocalDate equal to and indistinguishable from the one we had before.

To save into your database using JDBC:

    PreparedStatement pStmt
            = yourDatabaseConnection.prepareStatement(
                    "insert into your_table (date_of_birth) values (?);");
    pStmt.setObject(1, dateOfBirth);
    int insertedRows = pStmt.executeUpdate();

Note the use of setObject(), not setDate().

Links

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.