Bernd
Bernd

Reputation: 657

Format String into Date considering localization in GWT

I have the problem that I cannot format a String, having in form of "270317" (German version), into a Date.

For accomplishing this, I use GWT. What I have so far is this:

String input = "270317";
LocaleInfo locale = null;
if (locale == null) {
    locale = LocaleInfo.getCurrentLocale();
}
date = DateTimeFormat.getFormat(input).parse(input);

The outcome is always the current date: 07/28/2017

What I want to achieve is to have the date as it is written in the country where the program is being executed. If that is not really possible then I would prefer to have it written in this way: 03/27/2017.

Upvotes: 2

Views: 1451

Answers (1)

user7605325
user7605325

Reputation:

To parse the input 270317 to a Date, you must provide the expected format (you're using input as the format, which is wrong):

String input = "270317";
Date date = DateTimeFormat.getFormat("ddMMyy").parse(input);

This will parse the date correctly, if the input format is always as day-month-year. If the inputs are produced in a localized format, then you can use DateTimeFormat.getFormat(PredefinedFormat.DATE_SHORT) or any other format - but this is locale-specific and it can vary a lot between different environments.

Check your inputs to know if you'll need to use a fixed or a localized format.

After you parsed the date, you can then format it to whatever format you want. If you want a locale-specific format, just use:

DateTimeFormat.getFormat(PredefinedFormat.DATE_SHORT).format(date);

This is locale specific, so the output can vary. In my system, I've got:

2017-03-27


Java new Date/Time API

Although you're using GWT, this specific code for date parsing/formatting could be handled by a better API. GWT uses java.util.Date, which has lots of problems and design issues.

If you're using Java 8, consider using the new java.time API. It's easier, less bugged and less error-prone than the old APIs.

If you're using Java <= 7, you can use the ThreeTen Backport, a great backport for Java 8's new date/time classes. And for Android, there's the ThreeTenABP (more on how to use it here).

The code below works for both. The only difference is the package names (in Java 8 is java.time and in ThreeTen Backport (or Android's ThreeTenABP) is org.threeten.bp), but the classes and methods names are the same.

To parse and format a date, you can use a DateTimeFormatter. As you're using only day, month and year, I'm using the LocalDate class (which has only the date fields):

String input = "270317";
// parse the date
DateTimeFormatter parser = DateTimeFormatter.ofPattern("ddMMyy");
LocalDate date = LocalDate.parse(input, parser);

// locale specific format
DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT);
System.out.println(formatter.format(date));

As this is locale specific, in my system I've got the output:

27/03/17

If you want to use exactly the same pattern produced by GWT, you can use:

// get the GWT format
String pattern = DateTimeFormat.getFormat(PredefinedFormat.DATE_SHORT).getPattern();
// use the same format in the formatter
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
System.out.println(formatter.format(date));

Based on the GWT docs, it seems to use patterns compatible with DateTimeFormatter (at least for date fields), so this should work for all cases.

If you want a fixed format (like 03/27/2017), just do:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");

Check the javadoc for more details about date patterns.

Upvotes: 2

Related Questions