根据语言使用不同的时间格式

时间:2018-03-20 06:14:45

标签: android datetime simpledateformat datetime-format

我应该根据我的应用中的语言显示不同的时间格式。当设备是英语时,用户应该获得如下的时间格式:

  

2018年3月18日,下午2时30分

但是当用户的设备是德语时,他应该得到这样的时间格式:

  

18.03.2018,14:30 Uhr

有没有办法通过使用String格式化时间SimpleDateFormat或者我应该以其他方式执行此操作,如果是这样,我怎么能这样做?

3 个答案:

答案 0 :(得分:1)

我认为它不会起作用,但确实如此。只需将您喜欢的格式放入字符串xml文件中即可:

<string name="date_time_format">dd MMMM yyyy, hh.mm a</string>
<string name="date_time_format">dd.MM.yyyy, HH:mm</string>

然后在SDF中使用它:

SimpleDateFormat fmtOut = new SimpleDateFormat(context.getString(R.string.date_time_format), Locale.getDefault());

对于德语格式末尾的“Uhr”,我添加了一个占位符字符串,如下所示:

<string name="date_time_string">%s</string>
<string name="date_time_string">%s Uhr</string>

并使用String.format()方法返回格式化日期:

return String.format(context.getString(R.string.date_time_string), fmtOut.format(date));

答案 1 :(得分:0)

试试这段代码。希望它能解决这个问题。

        java.sql.Date date1 = new java.sql.Date((new Date()).getTime());

        SimpleDateFormat formatNowDay = new SimpleDateFormat("dd");
        SimpleDateFormat formatNowYear = new SimpleDateFormat("yyyy");
        SimpleDateFormat sdf12 = new SimpleDateFormat("hh:mm aa");
        SimpleDateFormat sdf24 = new SimpleDateFormat("HH:mm");
        SimpleDateFormat germanSdf = new SimpleDateFormat("dd.MM.yyyy");

        String currentDay = formatNowDay.format(date1);
        String currentYear = formatNowYear.format(date1);
        String time12 = sdf12.format(date1);
        String time24 = sdf24.format(date1);

        String germanTime = germanSdf.format(date1);

        Calendar mCalendar = Calendar.getInstance();
        String currentMonth = mCalendar.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.getDefault());

        String engTime = currentDay+" "+currentMonth+" "+currentYear+", "+time12;
        String germanFormat = germanTime+", "+time24+" Uhr";

答案 2 :(得分:0)

另一种方法是使用java.text.DateFormat

DateFormat df = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, Locale.ENGLISH);

日期和时间样式(第一个和第二个参数)可以是SHORTMEDIUMLONGFULL。如果需要,您可以使用Locale.getDefault()获取设备的默认语言。

我不确定哪种风格组合可以提供你想要的东西 - 所有这些风格都给了我不同的输出,没有一个给我你描述的风格。

这是因为特定于语言环境的格式嵌入在JVM中,我不确定它在不同的API级别和设备之间是如何变化的。

如果上面的解决方案有效,那就没关系了。否则,另一种方法是在每个语言环境中制作特定的模式:

// get device's locale
Locale locale = Locale.getDefault();

String pattern = "";
// check language
if ("en".equals(locale.getLanguage())) {
    // english
    pattern = "dd MMMM yyyy, h.mm a";
} else if ("de".equals(locale.getLanguage())) {
    // german
    pattern = "dd.MM.yyyy, HH:mm 'Uhr'";
} else {
    pattern = // use some default pattern for other languages?
}

SimpleDateFormat sdf = new SimpleDateFormat(pattern, locale);
String formattedDate = sdf.format(new Date());

一个细节是,在我的JVM中,英语区域设置的AM / PM符号是大写的,因此您可能需要通过执行以下操作来调整它:

// change AM/PM to am/pm (only for English)
if ("en".equals(locale.getLanguage())) {
    formattedDate  = formattedDate.toLowerCase();
}

java.time API

在API级别26中,您可以使用java.time API。对于较低级别,有一个nice backport,具有相同的类和功能。

这可以更好地使用。代码可能看起来很相似,但类本身解决了lots of internal issues of the old API

您可以先尝试获取JVM的本地化模式,看看它们是否与您的输出相匹配:

Locale locale = Locale.getDefault();
FormatStyle style = FormatStyle.MEDIUM;
String pattern = DateTimeFormatterBuilder.getLocalizedDateTimePattern(style, style, IsoChronology.INSTANCE, locale);
DateTimeFormatter fmt = DateTimeFormatter.ofPattern(pattern, locale);

或者如上所述:

// get device's locale
Locale locale = Locale.getDefault();

String pattern = "";
// check language
if ("en".equals(locale.getLanguage())) {
    // english
    pattern = "dd MMMM yyyy, h.mm a";
} else if ("de".equals(locale.getLanguage())) {
    // german
    pattern = "dd.MM.yyyy, HH:mm 'Uhr'";
} else {
    pattern = ""; // use some default pattern?
}

DateTimeFormatter fmt = DateTimeFormatter.ofPattern(pattern, locale);

String formattedDate = LocalDateTime.now().format(fmt);

在我的测试中,我遇到了大写AM / PM英语的问题。您可以通过如上所述调用toLowerCase()来解决此问题,但此API还允许您创建更灵活的格式化程序。

格式化程序是线程安全的(虽然SimpleDateFormat不是),因此您可以根据语言创建格式化程序的静态映射,并根据需要重复使用它们:

// map of formatters
Map<String, DateTimeFormatter> formatterMap = new HashMap<>();

// English formatter
Map<Long, String> customAmPmSymbols = new HashMap<>();
customAmPmSymbols.put(0L, "am");
customAmPmSymbols.put(1L, "pm");
DateTimeFormatter f = new DateTimeFormatterBuilder()
    // date/time
    .appendPattern("dd MMMM yyyy, h.mm ")
    // custom AM/PM symbols (lowercase)
    .appendText(ChronoField.AMPM_OF_DAY, customAmPmSymbols)
    // create formatter
    .toFormatter(Locale.ENGLISH);
// add to map
formatterMap.put("en", f);

// German formatter
formatterMap.put("de", DateTimeFormatter.ofPattern("dd.MM.yyyy, HH:mm 'Uhr'", Locale.GERMAN));

// get device's locale
Locale locale = Locale.getDefault();

DateTimeFormatter fmt = formatterMap.get(locale.getLanguage());

if (fmt != null) {
    String formattedDate = LocalDateTime.now().format(fmt);
}
相关问题