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
It's not clear what you mean by a "Java SQL Date". If you mean as in java.sql.Date
, then it doesn't really have a string format... it's just a number. To format it in a particular way, use something like java.text.SimpleDateFormat.
Alternatively, convert it to a Joda Time DateTime
; Joda Time is a much better date and time API than the built-in one. For example, SimpleDateFormat
isn't thread-safe.
(Note that a java.sql.Date
has more precision than a normal java.util.Date
, but it looks like you don't need that here.)
If it is for presentation you can use SimpleDateFormat straight away:
package org.experiment;
import java.sql.Date;
import java.text.SimpleDateFormat;
public class Dates {
private static SimpleDateFormat df = new SimpleDateFormat("MMMM yyyy");
public static void main(String[] args){
Date oneDate = new Date(new java.util.Date().getTime());
System.out.println(df.format(oneDate));
}
}
myJavaSqlDate.toLocalDate()
.format(
DateTimeFormatter.ofLocalizedDate ( FormatStyle.LONG )
.withLocale ( Locale.UK )
)
11 May 2017
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.
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.
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.
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:
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
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?
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.
Object such as java.sql.Date
and java.util.Date
(of which java.sql.Date
is a subclass) don't have a format of themselves. You use a java.text.DateFormat
object to display these objects in a specific format, and it's the DateFormat
(not the Date
itself) that determines the format.
For example:
Date date = ...; // wherever you get this
DateFormat df = new SimpleDateFormat("dd MMMM yyyy");
String text = df.format(date);
System.out.println(text);
Note: When you print a Date
object without using a DateFormat
object, like this:
Date date = ...;
System.out.println(date);
then it will be formatted using some default format. That default format is however not a property of the Date
object that you can change.