2

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.

9
  • Instead of getFormat(input), you should pass the pattern, something like getFormat("ddMMyy") Commented Jul 28, 2017 at 14:14
  • My problem is that I do not know how to get the right pattern for the localization? I want to use the pc`s localization ( someone is using Englisch, someone is using french ). Commented Jul 28, 2017 at 14:20
  • I don't use GWT very often, but it seems that the predefined formats uses the system's locale: gwtproject.org/doc/latest/… - "...use the default formats and let the localization mechanism in the DateTimeFormat do the work for you" Commented Jul 28, 2017 at 14:23
  • I tried it but is says the constructor Date(String) is deprecated and doing it the following way crashes: Date date = new Date(input); Date newDate = DateTimeFormat.getShortDateFormat().format(date); Commented Jul 28, 2017 at 14:41
  • You need another DateTimeFormat to parse the input. Probably date = DateTimeFormat.getFormat("ddMMyy").parse(input) (if the input is always in this format) or use the default format if the input is also localized. Commented Jul 28, 2017 at 14:42

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you very much for your support. I will try that on Monday, since I am not at work today and tomorrow. Thanks!
Hi, I tried it and it seems to be good! Now, they changed the request: some people are located in USA, UK, Germany, .., so everyone has a different style of input ( in Germany they write: ddMMyy, in USA they will use MM/dd/YY, and every input should now be transformed to the USA - standard input. Maybe you can help me again? I think it has to be done via localization but Locale is only available in Java, in GWt they use LocaleInfo, but I do not know how to get the user`s localization and then convert it to this pattern: mm/dd/YYYY - Maybe you can help me one again, Thank you very much.
I'm not sure how to configure locales in GWT (maybe this can help). Maybe you'll have to check each case and try to parse one by one (or check the string before parsing). But to change the output to USA format, just change the formatter above to use MM/dd/yyyy.
Could you edit your question, specifying all the date formats for the inputs? The inputs can be from anywhere in the world or is there a specific set of formats? If you have a well defined set of formats, just create lots of DateTimeFormatter's for each case (if you know the locale, or where the requests come from, it's better because you'll know which formatter to use - otherwise you'll have to try each one until the parsers succeeds)
Hi, thank you very much. I will talk through LocalInfo and now they came up with a pattern for our offices in different countries. So, I will implement a case locale: ... and for every locale I will define a different pattern. Thank you so much for helping me. Furthermore, I succeeded in converting everything in this way ( here I use a different package "shared" from GWT ): DefaultDateTimeFormatInfo info = new DefaultDateTimeFormatInfo();DateTimeFormat dtf = new DateTimeFormat(pattern, info) {}; String x = dtf.parse(input);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.