How do I get localized date pattern string?

前端 未结 10 2212
死守一世寂寞
死守一世寂寞 2020-11-28 05:31

It is quite easy to format and parse Java Date (or Calendar) classes using instance of DateFormat, i.e. I could format current date into short localize date like this:

相关标签:
10条回答
  • 2020-11-28 05:52

    You can use DateTimeFormatterBuilder in Java 8. Following example returns localized date only pattern e.g. "d.M.yyyy".

    String datePattern = DateTimeFormatterBuilder.getLocalizedDateTimePattern(
      FormatStyle.SHORT, null, IsoChronology.INSTANCE, 
      Locale.GERMANY); // or whatever Locale
    
    0 讨论(0)
  • 2020-11-28 05:53

    For SimpleDateFormat, You call toLocalizedPattern()

    EDIT:

    For Java 8 users:

    The Java 8 Date Time API is similar to Joda-time. To gain a localized pattern we can use class DateTimeFormatter

    DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM);

    Note that when you call toString() on LocalDate, you will get date in format ISO-8601

    Note that Date Time API in Java 8 is inspired by Joda Time and most solution can be based on questions related to time.

    0 讨论(0)
  • 2020-11-28 05:55

    Java 8 provides some useful features out of the box for working with and formatting/parsing date and time, including handling locales. Here is a brief introduction.

    Basic Patterns

    In the simplest case to format/parse a date you would use the following code with a String pattern:

    DateTimeFormatter.ofPattern("MM/dd/yyyy")
    

    The standard is then to use this with the date object directly for formatting:

    return LocalDate.now().format(DateTimeFormatter.ofPattern("MM/dd/yyyy"));
    

    And then using the factory pattern to parse a date:

    return LocalDate.parse(dateString, DateTimeFormatter.ofPattern("MM/dd/yyyy"));
    

    The pattern itself has a large number of options that will cover the majority of usecases, a full rundown can be found at the javadoc location here.

    Locales

    Inclusion of a Locale is fairly simple, for the default locale you have the following options that can then be applied to the format/parse options demonstrated above:

    DateTimeFormatter.ofLocalizedDate(dateStyle);
    

    The 'dateStyle' above is a FormatStyle option Enum to represent the full, long, medium and short versions of the localized Date when working with the DateTimeFormatter. Using FormatStyle you also have the following options:

    DateTimeFormatter.ofLocalizedTime(timeStyle);
    DateTimeFormatter.ofLocalizedDateTime(dateTimeStyle);
    DateTimeFormatter.ofLocalizedDateTime(dateTimeStyle, timeStyle);
    

    The last option allows you to specify a different FormatStyle for the date and the time. If you are not working with the default Locale the return of each of the Localized methods can be adjusted using the .withLocale option e.g

    DateTimeFormatter.ofLocalizedTime(timeStyle).withLocale(Locale.ENGLISH);
    

    Alternatively the ofPattern has an overloaded version to specify the locale too

    DateTimeFormatter.ofPattern("MM/dd/yyyy",Locale.ENGLISH);
    

    I Need More!

    DateTimeFormatter will meet the majority of use cases, however it is built on the DateTimeFormatterBuilder which provides a massive range of options to the user of the builder. Use DateTimeFormatter to start with and if you need these extensive formatting features fall back to the builder.

    0 讨论(0)
  • 2020-11-28 06:00

    The following code will give you the pattern for the locale:

    final String pattern1 = ((SimpleDateFormat) DateFormat.getDateInstance(DateFormat.SHORT, locale)).toPattern();
    System.out.println(pattern1);
    
    0 讨论(0)
  • 2020-11-28 06:04

    You can try something like :

    LocalDate fromCustomPattern = LocalDate.parse("20.01.2014", DateTimeFormatter.ofPattern("MM/dd/yy"))
    
    0 讨论(0)
  • 2020-11-28 06:08

    Im not sure about what you want, but...

    SimpleDateFormat example:

    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yy");
    Date date = sdf.parse("12/31/10");
    String str = sdf.format(new Date());
    
    0 讨论(0)
提交回复
热议问题