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
To format a date for the current Locale, use one of the static factory methods: myString = DateFormat. getDateInstance(). format(myDate);
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.
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.
//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.
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.
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:
Locale.US
: 7/18/2017
.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
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With