正确处理作为字符串值出现的日期并进行比较

Properly working with dates that comes as a string value and compare them

在我的程序中,我有一堆表示日期的字符串,如下所示:

String someDate1 = "abcd-2020-02-19t10:23:31.180z";
String someDate2 = "xyz-2020-02-22t10:03:31.111z";
String someDate2 = "blablabla-2020-06-15t11:13:31.181z";

让我们以“someDate1”为例,我已经设法 trim 这些字符串,所以我最终只会通过这样做得到日期:

System.out.println(someDate1.substring(someDate1.length() - 24).substring(0, 19));

输出:

2020-02-19t10:23:31

现在,我想确定输出中的这个日期是否早于 7 天。

假设我将上面的字符串保存在一个列表中,所以我只想知道我的约会对象是否超过一周,或者类似:

public static void main(String[] args) {

    String someDate1 = "abcd-2020-02-19t10:23:31.180z";
    List<String> datesList = new ArrayList<>();
    datesList.add(someDate1);
    ...//adding others too

    datesList.forEach(date -> {
        if (isDateOlder(date)) {
            System.out.println(date + " - this is more than 7 days old, it has been more than a week!");
        }
        else {
            System.out.println(date + " - it has NOT been more than a week since this date!");
        }
    });
}


static boolean isDateOlder(String date) { 
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
    String afterTrimming = date.substring(date.length() - 24).substring(0, 19).replace("t", "-");

    try {
        Date afterFormatParse = formatter.parse(afterTrimming);

        LocalDate weekAgo = LocalDate.now().minusDays(7); //cool thing you can do, but i'm not using it
        System.out.println("a week ago : " + weekAgo); //it seems useful but i don't really use it because I saw I can't compare "Date" with LocalDate

        long rn = new Date().toInstant().toEpochMilli();
        long weekInMillis = 604800000;
        long oneWeekAgo = rn - weekInMillis;


        if (afterFormatParse.toInstant().toEpochMilli() <= oneWeekAgo) {
            return true;
        }
        else {
            return false;
        }

    } catch (ParseException e) {
        e.printStackTrace();
    }


}

最重要的是,它有效,但这看起来非常 naive/wasteful,我相信还有更聪明的方法... 所以基本上我的主要问题是:

可惜 LocalDate 没有 "toDate()" 方法,因此您可以在需要时对两者进行比较,最后,我在变量中使用了“固定纪元”来表示一周,这很天真。我想听听你们是否有更漂亮的方式来表示日期减去一周,就像 LocalDate 提供的那样,同时还有一种相互比较的方式?

非常感谢。

2020-02-19t10:23:31.180z 是标准的 ISO 8601 日期格式,Java 8+ Time API 原生支持,因此无需使用自定义格式模式。

因此,要检查日期是否超过过去 7 天,精确到小数秒,您可以这样做:

static boolean isDateOlder(String dateStr) {
    Instant date = Instant.parse(dateStr.substring(dateStr.indexOf('-') + 1));
    Instant weekAgo = ZonedDateTime.now().minusDays(7).toInstant();
    return date.isBefore(weekAgo);
}

如果你想忽略时间部分时区偏移量(只比较日期部分),你可以这样做这个:

static boolean isDateOlder(String dateStr) {
    LocalDate date = LocalDate.parse(dateStr.substring(dateStr.indexOf('-') + 1),
                                     DateTimeFormatter.ISO_DATE_TIME);
    LocalDate weekAgo = LocalDate.now().minusDays(7);
    return date.isBefore(weekAgo);
}

要将输入中的UTC时区偏移量(例如Z)调整为本地时区,然后比较纯日期(以防时区偏移跨越午夜),您可以这样做:

static boolean isDateOlder(String dateStr) {
    LocalDate date = Instant.parse(dateStr.substring(dateStr.indexOf('-') + 1))
                            .atZone(ZoneId.systemDefault())
                            .toLocalDate();
    LocalDate weekAgo = LocalDate.now().minusDays(7);
    return date.isBefore(weekAgo);
}

或者更宽松一点,支持 Z 以外的时区:

static boolean isDateOlder(String dateStr) {
    LocalDate date = ZonedDateTime.parse(dateStr.substring(dateStr.indexOf('-') + 1))
                                  .withZoneSameInstant(ZoneId.systemDefault())
                                  .toLocalDate();
    LocalDate weekAgo = LocalDate.now().minusDays(7);
    return date.isBefore(weekAgo);
}