基于android上的用户区域设置的日期格式

问题描述 投票:46回答:5

我想根据用户区域设置显示出生日期。在我的申请中,我的一个字段是出生日期,目前的格式为dd/mm/yyyy。因此,如果用户更改其区域设置,则日期格式也应相应更改。任何指针或代码示例都会帮助我克服这个问题。

android internationalization date-format
5个回答
78
投票

您可以使用DateFormat类根据用户区域设置格式化日期。

例:

String dateOfBirth = "26/02/1974";
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Date date = null;
try {
    date = sdf.parse(dateOfBirth);
} catch (ParseException e) {
    // handle exception here !
}
java.text.DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(context);
String s = dateFormat.format(date);

您可以使用不同的方法getLongDateFormatgetMediumDateFormat,具体取决于您希望拥有的详细程度。


12
投票

要根据区域设置更改日期格式,以下代码对我有用:

    String dateOfBirth = "26/02/1974";
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
    Date date = null;
    try {
        date = sdf.parse(dateOfBirth);
    } catch (ParseException e) {
        // handle exception here !
    }

    String myString = DateFormat.getDateInstance(DateFormat.SHORT).format(date);

然后,当您更改区域设置时,日期格式将根据它进行更改。有关日期模式的更多信息:http://docs.oracle.com/javase/tutorial/i18n/format/dateFormat.html


10
投票

Android api提供了显示本地化日期格式的简便方法。以下代码有效。

public class FileDateUtil {
    public static String getModifiedDate(long modified) {
        return getModifiedDate(Locale.getDefault(), modified);
    }

    public static String getModifiedDate(Locale locale, long modified) {
        SimpleDateFormat dateFormat = null;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            dateFormat = new SimpleDateFormat(getDateFormat(locale));
        } else {
            dateFormat = new SimpleDateFormat("MMM/dd/yyyy hh:mm:ss aa");
        }

        return dateFormat.format(new Date(modified));
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
    public static String getDateFormat(Locale locale) {
        return DateFormat.getBestDateTimePattern(locale, "MM/dd/yyyy hh:mm:ss aa");
    }
}

您可以查看以下代码。

String koreaData = FileDateUtil.getModifiedDate(Locale.KOREA, System.currentTimeMillis());
String franceData = FileDateUtil.getModifiedDate(Locale.FRENCH, System.currentTimeMillis());
String defaultData = FileDateUtil.getModifiedDate(Locale.getDefault(), System.currentTimeMillis());

String result = "Korea : " + koreaData + System.getProperty("line.separator");
result += "France : " + franceData + System.getProperty("line.separator");
result += "Default : " + defaultData + System.getProperty("line.separator"); 
tv.setText(result);

8
投票

很简单

对于日期+时间:

DateFormat format = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.SHORT, Locale.getDefault());

仅限日期:

DateFormat format = DateFormat.getDateInstance(DateFormat.SHORT, Locale.getDefault());

1
投票

虽然在提出问题时接受的答案是正确的,但后来已经过时了。我正在贡献现代的答案。

java.time和ThreeTenABP

    DateTimeFormatter dateFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT);

    LocalDate dateOfBirth = LocalDate.of(1991, Month.OCTOBER, 13);
    String formattedDob = dateOfBirth.format(dateFormatter);
    System.out.println("Born: " + formattedDob);

它根据JVM的区域设置(通常从设备获取)提供不同的输出。例如:

  • 加拿大法语:Born: 91-10-13
  • 中文:Born: 1991/10/13
  • 德语:Born: 13.10.91
  • 意大利语:Born: 13/10/91

如果您想要更长的格式,可以指定不同的格式样式。美国英语语言环境中的输出示例:

  • FormatStyle.SHORTBorn: 10/13/91
  • FormatStyle.MEDIUMBorn: Oct 13, 1991
  • FormatStyle.LONGBorn: October 13, 1991
  • FormatStyle.FULLBorn: Thursday, October 13, 1991

问题:我可以在Android上使用java.time吗?

是的,java.time适用于较旧和较新的Android设备。它至少需要Java 6。

  • 在Java 8及更高版本和更新的Android设备上(来自API级别26),现代API内置。
  • 在Java 6和7中获取ThreeTen Backport,现代类的后端(JST 310的ThreeTen;请参见底部的链接)。
  • 在(较旧的)Android上使用Android版的ThreeTen Backport。它被称为ThreeTenABP。并确保从org.threeten.bp导入子包的日期和时间类。

问题:我还可以接受用户本地格式的用户输入吗?

是的你可以。格式化程序还可用于将用户的字符串解析为LocalDate

    LocalDate date = LocalDate.parse(userInputString, dateFormatter);

我建议您首先格式化一个示例日期并将其显示给用户,以便他/她可以看到您的程序对他/她的语言环境所期望的格式。作为示例日期,日期是大于12的日期和大于31的年份,以便从示例中看到日,月和年的顺序(对于更长的格式,年份无关紧要,因为它将是四位数)。

如果用户以不正确的格式或无效的日期输入日期,则解析将抛出DateTimeParseException。抓住它并允许用户再试一次。

问题:我可以在一天中的某个时间做同样的事吗?日期和时间?

是。要根据用户的区域设置格式化时间,请从DateTimeFormatter.ofLocalizedTime获取格式化程序。对于日期和时间,一起使用DateTimeFormatter.ofLocalizedDateTime的一个重载版本。

避免使用DateFormat,SImpleDateFormat和Date类

我建议你不要使用DateFormatSimpleDateFormatDate。这些课程的设计很差,而且已经过时了,前两个特别出了名的麻烦。而是使用LocalDateDateTimeFormatter和java.time中的其他类,现代Java日期和时间API。

链接


0
投票

要获得日期格式模式,您可以执行以下操作:

Format dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
String pattern = ((SimpleDateFormat) dateFormat).toLocalizedPattern();

之后,您可以根据模式格式化输入。

© www.soinside.com 2019 - 2024. All rights reserved.