91

指定されたロケールに基づいて、さまざまな方法でJavaで日付をフォーマットしようとしています。たとえば、英語のユーザーには「2009年11月1日」(「MMMd、yyyy」でフォーマット)を表示し、ノルウェーのユーザーには「2009年11月1日」(「d。MMM。yyyy」)を表示させます。

SimpleDateFormatコンストラクターにロケールを追加すると、月の部分は問題なく機能しますが、残りはどうでしょうか。

SimpleDateFormatにロケールとペアになったフォーマット文字列を追加できることを望んでいましたが、これを行う方法が見つかりません。コードでロケールをチェックし、対応するフォーマット文字列を追加することは可能ですか、それとも必要ですか?

4

10 に答える 10

93

SimpleDateFormat dateFormat = new SimpleDateFormat("EEEE dd MMM yyyy", Locale.ENGLISH);
String formatted = dateFormat.format(the_date_you_want_here);
于 2013-01-30T22:17:14.527 に答える
92

で独自のパターンを作成する代わりに、DateFormat.getDateInstance(int style、Locale locale)を使用しますSimpleDateFormat

于 2009-11-02T13:38:36.107 に答える
32

スタイル+ロケールを使用します:DateFormat.getDateInstance(int style、Locale locale)

http://java.sun.com/j2se/1.5.0/docs/api/java/text/DateFormat.htmlを確認 してください

次の例を実行して、違いを確認します。

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

public class DateFormatDemoSO {
  public static void main(String args[]) {
    int style = DateFormat.MEDIUM;
    //Also try with style = DateFormat.FULL and DateFormat.SHORT
    Date date = new Date();
    DateFormat df;
    df = DateFormat.getDateInstance(style, Locale.UK);
    System.out.println("United Kingdom: " + df.format(date));
    df = DateFormat.getDateInstance(style, Locale.US);
    System.out.println("USA: " + df.format(date));   
    df = DateFormat.getDateInstance(style, Locale.FRANCE);
    System.out.println("France: " + df.format(date));
    df = DateFormat.getDateInstance(style, Locale.ITALY);
    System.out.println("Italy: " + df.format(date));
    df = DateFormat.getDateInstance(style, Locale.JAPAN);
    System.out.println("Japan: " + df.format(date));
  }
}

出力:

United Kingdom: 25-Sep-2017
USA: Sep 25, 2017
France: 25 sept. 2017
Italy: 25-set-2017
Japan: 2017/09/25
于 2009-11-02T13:39:33.560 に答える
19
于 2016-12-01T20:54:06.537 に答える
8

日付文字列のローカリゼーション:

redsonicの投稿に基づく:

private String localizeDate(String inputdate, Locale locale) { 

    Date date = new Date();
    SimpleDateFormat dateFormatCN = new SimpleDateFormat("dd-MMM-yyyy", locale);       
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");


    try {
        date = dateFormat.parse(inputdate);
    } catch (ParseException e) {
        log.warn("Input date was not correct. Can not localize it.");
        return inputdate;
    }
    return dateFormatCN.format(date);
}

String localizedDate = localizeDate("05-Sep-2013", new Locale("zh","CN"));

05-九月-2013のようになります

于 2013-09-05T12:07:16.990 に答える
5

これにより、ユーザーの現在のロケールに従って日付が表示されます。

日時を返すには:

import java.text.DateFormat;    
import java.util.Date;

Date date = new Date();
DateFormat df = DateFormat.getDateTimeInstance();
String myDate = df.format(date);

1969年12月31日19:00:02

日付のみを返すには、次を使用します。

DateFormat.getDateInstance() 

1969年12月31日

于 2018-03-13T19:17:16.470 に答える
2

特定の日付のJava8スタイル

LocalDate today = LocalDate.of(1982, Month.AUGUST, 31);
System.out.println(today.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM).withLocale(Locale.ENGLISH)));
System.out.println(today.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM).withLocale(Locale.FRENCH)));
System.out.println(today.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM).withLocale(Locale.JAPANESE)));
于 2017-10-26T04:17:13.210 に答える
1

これが誰かを助けることを願っています。ロケールインスタンスを受け入れ、ロケール固有の日付形式/パターンを返す以下のコードを見つけてください。

public static String getLocaleDatePattern(Locale locale) {
    // Validating if Locale instance is null
    if (locale == null || locale.getLanguage() == null) {
        return "MM/dd/yyyy";
    }
    // Fetching the locale specific date pattern
    String localeDatePattern = ((SimpleDateFormat) DateFormat.getDateInstance(
            DateFormat.SHORT, locale)).toPattern();
    // Validating if locale type is having language code for Chinese and country
    // code for (Hong Kong) with Date Format as - yy'?'M'?'d'?'
    if (locale.toString().equalsIgnoreCase("zh_hk")) {
        // Expected application Date Format for Chinese (Hong Kong) locale type
        return "yyyy'MM'dd";
    }
    // Replacing all d|m|y OR Gy with dd|MM|yyyy as per the locale date pattern
    localeDatePattern = localeDatePattern.replaceAll("d{1,2}", "dd")
            .replaceAll("M{1,2}", "MM")
            .replaceAll("y{1,4}|Gy", "yyyy");
    // Replacing all blank spaces in the locale date pattern
    localeDatePattern = localeDatePattern.replace(" ", "");
    // Validating the date pattern length to remove any extract characters
    if (localeDatePattern.length() > 10) {
        // Keeping the standard length as expected by the application
        localeDatePattern = localeDatePattern.substring(0, 10);
    }
    return localeDatePattern;
}
于 2020-07-30T13:30:40.160 に答える
0
String text = new SimpleDateFormat("E, MMM d, yyyy").format(date);
于 2017-08-28T06:57:23.013 に答える
-2

Java8

 import java.time.format.DateTimeFormatter;         
 myDate.format(DateTimeFormatter.ofPattern("dd-MMM-YYYY",new Locale("ar")))
于 2014-11-25T10:41:55.273 に答える