Yes, I see two major issues.
- Outmoded classes
The old .Date/.Calendar classes are notoriously troublesome. Avoid them.
- Time zone
Date varies depending on what time zone you are in.
Avoid java.util.Date/.Calendar
Avoid the old java.util.Date/.Calendar classes. The are notoriously confusing, troublesome, and flawed. Instead, use the successful Joda-Time library. In Java 8 and later use the new built-in java.time framework, inspired by Joda-Time.
Time Zone
If precision matters, apply time zones. The date varies by where you are on the globe. For example, a new day rises earlier in Paris than in Montréal.
Ideally your input string would include either offset-from-UTC and/or time zone. If not, you must know the zone. Tell the parser the zone to use.
DateTimeZone zone = DateTimeZone.forID( "America/Montreal" );
ISO 8601
Your string is nearly in standard format according to ISO 8601. Those standard formats are used by default in Joda-Time. So no need to define a format pattern if we replace the space in middle with a T
.
String input = "2015-09-18 12:24:16";
String inputStandard = input.replace( " " , "T" );
DateTime dateTime = new DateTime( inputStandard , zone );
To get our desired String output, specify a Locale
for the desired human language.
Locale locale = Locale.CANADA_FRENCH;
If you want the JVM’s current default Locale
, I suggest you call for it to make your code self-documenting and explicit.
Locale locale = Locale.getDefault();
But remember that the current default can change at any moment with a call to setDefault
by any code in any thread in any app in your JVM. I suggest specifying a Locale
rather than rely on default.
Define a formatter by specifying a pattern. Tell the formatter to use a specific Locale
rather than your JVM's current default.
DateTimeFormatter formatter = DateTimeFormat.forPattern( "d MMMM" );
formatter = formatter.withLocale( locale );
String output = formatter.print( dateTime );
Dump to console.
System.out.println( "input: " + input + " in zone: " + zone + " is: " + dateTime );
System.out.println( "In locale: " + locale + " = " + output );
When run.
input: 2015-09-18 12:24:16 in zone: America/Toronto is: 2015-09-18T12:24:16.000-04:00
In locale: fr_CA = 18 septembre
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.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes.
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.