91

我正在尝试根据给定的语言环境以不同的方式在 Java 中格式化日期。例如,我希望英语用户看到“Nov 1, 2009”(格式为“MMM d, yyyy”),挪威用户看到“1. nov. 2009”(“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-9-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 日晚上 7:00:02

要仅返回日期,请使用:

DateFormat.getDateInstance() 

1969 年 12 月 31 日

于 2018-03-13T19:17:16.470 回答
2

给定日期的 Java 8 样式

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 回答