Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SimpleDateFormat pattern based on locale, but forcing a 4-digit year

I need to build a date format like dd/MM/yyyy. It's almost like DateFormat.SHORT, but contains 4 year digits.

I try to implement it with

new SimpleDateFormat("dd//MM/yyyy", locale).format(date);

However for US locale the format is wrong.

Is there a common way to format date that changes pattern based on locale?

Thank you

like image 396
lili Avatar asked Oct 17 '11 15:10

lili


People also ask

How will you format a date based on a locale after you have obtained a DateFormat object?

To format a date for the current Locale, use one of the static factory methods: myString = DateFormat. getDateInstance(). format(myDate);

What is locale date format?

Java DateFormat The locale is used for specifying the region and language for making the code more locale to the user. The way of writing date is different in different regions of the world.

What is the format of SimpleDateFormat?

SimpleDateFormat is a concrete class for formatting and parsing dates in a locale-sensitive manner. It allows for formatting (date -> text), parsing (text -> date), and normalization. SimpleDateFormat allows you to start by choosing any user-defined patterns for date-time formatting.

How do you format a date in Java like in the Ddmmyyyy format?

//Instantiating the SimpleDateFormat class SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy"); Parse/convert the required String to Date object using the parse() method by passing it as a parameter.


2 Answers

I would do it like this:

    StringBuffer buffer = new StringBuffer();

    Calendar date = Calendar.getInstance();
    DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.US);
    FieldPosition yearPosition = new FieldPosition(DateFormat.YEAR_FIELD);

    StringBuffer format = dateFormat.format(date.getTime(), buffer, yearPosition);
    format.replace(yearPosition.getBeginIndex(), yearPosition.getEndIndex(), String.valueOf(date.get(Calendar.YEAR)));

    System.out.println(format);

Using a FieldPosition you don't really have to care about wheter the format of the date includes the year as "yy" or "yyyy", where the year ends up or even which kind of separators are used.

You just use the begin and end index of the year field and always replace it with the 4 digit year value and that's it.

like image 82
Francisco Paulo Avatar answered Oct 07 '22 00:10

Francisco Paulo


java.time

Here’s the modern answer. IMHO these days no one should struggle with the long outdated DateFormat and SimpleDateFormat classes. Their replacement came out in the modern Java date & time API early in 2014, the java.time classes.

I am just applying the idea from Happier’s answer to the modern classes.

The DateTimeFormatterBuilder.getLocalizedDateTimePattern method generates a formatting pattern for date and time styles for a Locale. We manipulate the resulting pattern string to force the 4-digit year.

LocalDate date = LocalDate.of( 2017, Month.JULY, 18 );

String formatPattern =
    DateTimeFormatterBuilder.getLocalizedDateTimePattern(
        FormatStyle.SHORT, 
        null, 
        IsoChronology.INSTANCE, 
        userLocale);
formatPattern = formatPattern.replaceAll("\\byy\\b", "yyyy");
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(formatPattern, userLocale);

String output = date.format(formatter);

Example output:

  • For Locale.US: 7/18/2017.
  • For each of UK, FRANCE, GERMANY and ITALY: 18/07/2017.

DateTimeFormatterBuilder allows us to get the localized format pattern string directly, without getting a formatter first, that’s convenient here. The first argument to getLocalizedDateTimePattern() is the date format style. null as second argument indicates that we don’t want any time format included. In my test I used a LocalDate for date, but the code should work for the other modern date types too (LocalDateTime, OffsetDateTime and ZonedDateTime).

like image 20
Ole V.V. Avatar answered Oct 06 '22 22:10

Ole V.V.