Best way to convert Java SQL Date from yyyy-MM-dd to dd MMMM yyyy format

后端 未结 4 1338
有刺的猬
有刺的猬 2020-11-27 23:25

Is there a straightforward way of converting a Java SQL Date from format yyyy-MM-dd to dd MMMM yyyy format?

I could convert the date to a string and then manipulate

4条回答
  •  有刺的猬
    2020-11-27 23:48

    tl;dr

    myJavaSqlDate.toLocalDate()
                 .format(
                     DateTimeFormatter.ofLocalizedDate ( FormatStyle.LONG )
                                      .withLocale ( Locale.UK )
                 )
    

    11 May 2017

    Do not conflate date-time values with their textual representation

    As others said, a date-time object has no format. Only strings generated from the object or parsed by the object have a format. But such strings are always separate and distinct from the date-time object.

    Use objects, not strings

    Avoid using strings to communicate date-time values to/from your database. For date-time values, use date-time classes to instantiate date-time objects.

    The very purpose of JDBC is to mediate the differences in types between your database and Java.

    Using java.time

    The other Answers are outdated as they use the troublesome old legacy date-time classes or the venerable Joda-Time library. Both have been supplanted by the java.time classes.

    If you have a java.sql.Date object in hand, convert to java.time.LocalDate by calling the new method toLocalDate added to the old class.

    LocalDate ld = myJavaSqlDate.toLocalDate() ;
    

    For JDBC drivers that comply with JDBC 4.2 and later, you can work directly with java.time types.

    You seem to be interested in the date-only values. So use LocalDate. The LocalDate class represents a date-only value without time-of-day and without time zone.

    • PreparedStatement::setObject
      myPStmt.setObject( … , myLocalDate )
    • ResultSet::getObject
      myResultSet.getObject( … , LocalDate.class )

    To generate a string in your desired format, you could specify a custom formatting pattern. But I suggest letting java.time automatically localize.

    To localize, specify:

    • FormatStyle to determine how long or abbreviated should the string be.
    • Locale to determine (a) the human language for translation of name of day, name of month, and such, and (b) the cultural norms deciding issues of abbreviation, capitalization, punctuation, separators, and such.

    Example…

    LocalDate ld = LocalDate.now ( ZoneId.of ( "America/Montreal" ) );  // Today's date at this moment in that zone.
    DateTimeFormatter f = DateTimeFormatter.ofLocalizedDate ( FormatStyle.LONG ).withLocale ( Locale.UK );
    String output = ld.format ( f );
    

    output: 11 May 2017


    About java.time

    The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

    The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

    To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

    Where to obtain the java.time classes?

    • Java SE 8, Java SE 9, and later
      • Built-in.
      • Part of the standard Java API with a bundled implementation.
      • Java 9 adds some minor features and fixes.
    • Java SE 6 and Java SE 7
      • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
    • Android
      • The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
      • See How to use ThreeTenABP….

    The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

提交回复
热议问题