1

I've this:

2015-09-18 12:24:16

And I need this:

18 settembre (localized month string)

I've tried this way:

private String formatDate(String dateString) {
        String formattedDate = "";
        Date date;
        final SimpleDateFormat sdf =
        new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());

        try {
            date = sdf.parse(dateString);
            sdf.applyPattern("dd MMMM");
            formattedDate = sdf.format(date);
        } catch (ParseException pe) {
            pe.printStackTrace();
        }

        return formattedDate;
    }

Any pitfall with this code? Can it be simplified? Locale.getDefault will always get the right translation? Why applyPattern doc says:

"Changes the pattern of this simple date format to the specified pattern which uses non-localized pattern characters.

Jumpa
  • 4,319
  • 11
  • 52
  • 100

4 Answers4

3
SimpleDateFormat parser = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d1 = parser.parse("2015-09-18 12:24:16");
SimpleDateFormat parser1 = new SimpleDateFormat("dd MMMM",new Locale ("it"));
System.out.println(parser1.format(d1));

Output:

 18 settembre
SatyaTNV
  • 4,137
  • 3
  • 15
  • 31
  • I can't use an hardcoded Locale, I should get it from the "OS" somehow. – Jumpa Sep 29 '15 at 09:40
  • `SimpleDateFormat parser1 = new SimpleDateFormat("dd MMMM",new Locale ("it")); ` instead that , Use this : `SimpleDateFormat parser1 = new SimpleDateFormat("dd MMMM", ( Locale.getDefault()));` – User Learning Sep 29 '15 at 09:51
0

for default locale use this : locale.getDefault().

SimpleDateFormat parser = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d1 = null;
        try {
            d1 = parser.parse("2015-09-18 12:24:16");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        SimpleDateFormat parser1 = new SimpleDateFormat("dd MMMM",Locale.getDefault());
        System.out.println(parser1.format(d1));
User Learning
  • 3,165
  • 5
  • 30
  • 51
0

You can try this:-

Calendar calendar = Calendar.getInstance();
Date currDate = Util.getCurrentDate();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String date = formatter.format(calendar.getTime());
Chetan chadha
  • 558
  • 1
  • 4
  • 19
0

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.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154