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
具体取决于您希望拥有的详细程度.
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)
很简单
对于日期+时间:
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)
提出问题时,虽然可接受的答案是正确的,但后来却过时了。我正在做出现代答案。
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.SHORT
: Born: 10/13/91
FormatStyle.MEDIUM
: Born: Oct 13, 1991
FormatStyle.LONG
: Born: October 13, 1991
FormatStyle.FULL
: Born: Thursday, October 13, 1991
是的,java.time在较新和较旧的Android设备上均可正常运行。它至少需要Java 6。
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
。这些类的设计很差,而且已经过时,尤其是前两个类非常麻烦。相反使用LocalDate
,DateTimeFormatter
并从java.time其他类,现代的Java日期和时间API。
java.time
第一被描述。java.time
Java 6和7的反向移植(JSR-310的ThreeTen)。 归档时间: |
|
查看次数: |
48199 次 |
最近记录: |