解析并格式化日期

时间:2017-06-28 22:07:40

标签: java android datetime datetime-format datetime-parsing

我有以下代码,用于以字符串yyyy-MM-dd HH:mm:ss(UTC时区)的形式提取日期并将其转换为EEEE d(st, nd, rd, th) MMMM yyyy HH:mm(设备的默认时区)。

然而,我的方式问题是代码看起来凌乱而且效率低下。有没有办法实现我想要的,而不需要多次格式化和解析相同的日期,以提高效率?还是其他任何改进?

最好支持Android API级别14。

String inputExample = "2017-06-28 22:44:55";

//Converts UTC to Device Default (Local)
private String convertUTC(String dateStr) {
    try {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        df.setTimeZone(TimeZone.getTimeZone("UTC"));
        Date temp = df.parse(dateStr);
        df.setTimeZone(TimeZone.getDefault());
        String local = df.format(temp);
        Date localDate = df.parse(dateStr);
        SimpleDateFormat outputDF1 = new SimpleDateFormat("EEEE ");
        SimpleDateFormat outputDF2 = new SimpleDateFormat(" MMMM yyyy HH:mm");
        return outputDF1.format(temp) + prefix(local) + outputDF2.format(temp);
    } catch(java.text.ParseException pE) {
        Log.e("", "Parse Exception", pE);
        return null;
    }
}

private String prefix(String dateStr) {
    try {
        SimpleDateFormat outputDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date temp = outputDF.parse(dateStr);
        SimpleDateFormat df = new SimpleDateFormat("d");
        int d = Integer.parseInt(df.format(temp));
        if(1 <= d && d <= 31) {
            if(11 <= d && d <= 13)
                return d + "th";
            switch (d % 10) {
                case 1: return d + "st";
                case 2: return d + "nd";
                case 3: return d + "rd";
                default: return d + "th";
            }
        }
        Log.e("", "Null Date");
        return null;
    } catch(java.text.ParseException pE) {
        Log.e("", "Parse Exception", pE);
        return null;
    }
}

1 个答案:

答案 0 :(得分:2)

SimpleDateFormat可能没什么可改进的。由于您的输出格式为EEEE(星期几)和MMMM(月份名称),因此您必须解析日期才能知道这些日期的值。如果不使用日期格式化程序,您必须执行大量if来获取每个值的相应名称。

在Android中,作为SimpleDateFormat的替代方案,您可以使用ThreeTen Backport,这是Java 8新日期/时间类的绝佳后端,以及ThreeTenABP (更多关于如何使用它here)。

所有课程均低于org.threeten.bp套餐。在下面的代码中我也使用了Locale.ENGLISH,否则它将使用系统的默认值(因为我的不是英语,我假设你的是):

String inputExample = "2017-06-28 22:44:55";
// parser for input
DateTimeFormatter parser = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
// parse the date and set to UTC
ZonedDateTime z = LocalDateTime.parse(inputExample, parser).atZone(ZoneOffset.UTC);

// map of custom values - map each numeric value to its string with suffix (st, nd...)
Map<Long, String> textLookup = new HashMap<Long, String>();
for (int i = 1; i <= 31; i++) {
    String suffix = "";
    switch (i) {
    case 1:
    case 21:
    case 31:
        suffix = "st";
        break;
    case 2:
    case 22:
        suffix = "nd";
        break;
    case 3:
    case 23:
        suffix = "rd";
        break;
    default:
        suffix = "th";
    }
    textLookup.put((long) i, i + suffix);
}
// output formatter
DateTimeFormatter fmt = new DateTimeFormatterBuilder()
    // day of week
    .appendPattern("EEEE ")
    // append day with suffix (use map of custom values)
    .appendText(ChronoField.DAY_OF_MONTH, textLookup)
    // rest of pattern
    .appendPattern(" MMMM yyyy HH:mm")
    // create formatter with English locale
    .toFormatter(Locale.ENGLISH);

// print date, convert it to device default timezone
System.out.println(fmt.format(z.withZoneSameInstant(ZoneId.systemDefault())));

输出将是:

  

2017年6月28日星期三19:44

时间设置为19:44,因为我的默认时区为America/Sao_Paulo UTC-03:00 )。

不确定它对你来说是否不那么混乱,但至少IMO比SimpleDateFormat更清晰。只创建了2个格式化程序(一个用于输出,另一个用于输出)。当然还有textLookup地图,但它只有31个条目,格式化程序也可以重复使用。

SimpleDateFormat is not thread safe,而the new API is

相关问题