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

sha*_*enq 50 android date-format internationalization

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

Arn*_*aud 81

您可以使用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);
Run Code Online (Sandbox Code Playgroud)

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

  • 查看源代码,`android.text.format.DateFormat.getDateFormat(context)`只返回`java.text.DateFormat.getDateInstance(java.text.DateFormat.SHORT)`.事实上,甚至没有使用`context`参数.我想我更喜欢直接使用`java.text.DateFormat.getDateInstance(DateFormat.SHORT)`. (4认同)
  • "DateFormat dateFormat"应该写成"java.text.DateFormat dateFormat".各种库都有不同版本的DateFormat. (3认同)

Fel*_*shi 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);
Run Code Online (Sandbox Code Playgroud)

然后,当您更改区域设置时,日期格式将根据它进行更改.有关日期模式的更多信息: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");
    }
}
Run Code Online (Sandbox Code Playgroud)

您可以查看以下代码.

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);
Run Code Online (Sandbox Code Playgroud)


Dav*_*d_E 9

很简单

对于日期+时间:

DateFormat format = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.SHORT, Locale.getDefault());
Run Code Online (Sandbox Code Playgroud)

仅限日期:

DateFormat format = DateFormat.getDateInstance(DateFormat.SHORT, Locale.getDefault());
Run Code Online (Sandbox Code Playgroud)


Ole*_*.V. 8

提出问题时,虽然可接受的答案是正确的,但后来却过时了。我正在做出现代答案。

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);
Run Code Online (Sandbox Code Playgroud)

根据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,这是现代类的backport(JSR 310的ThreeTen;请参见底部的链接)。
  • 在较旧的Android上,请使用Android版本的ThreeTen Backport。它称为ThreeTenABP。并确保您导入org.threeten.bp带有子包的日期和时间类。

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

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

    LocalDate date = LocalDate.parse(userInputString, dateFormatter);
Run Code Online (Sandbox Code Playgroud)

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

DateTimeParseException如果用户以错误的格式或无效的日期输入日期,则解析将引发。赶上它,让用户再试一次。

问题:我是否可以每天都这样做?日期和时间?

是。要根据用户的语言环境格式化一天中的某个时间,请从获取格式器DateTimeFormatter.ofLocalizedTime。对于日期和时间,请同时使用的重载版本之一DateTimeFormatter.ofLocalizedDateTime

避免使用DateFormat,SImpleDateFormat和Date类

我建议你不要使用DateFormatSimpleDateFormatDate。这些类的设计很差,而且已经过时,尤其是前两个类非常麻烦。相反使用LocalDateDateTimeFormatter并从java.time其他类,现代的Java日期和时间API。

链接

  • @PrajwalWaingankar 感谢您的评论。不,不是的。我编辑了*问题:我可以在 Android 上使用 java.time 吗?* 部分,并试图使其更清晰一些。 (2认同)