How to compare to String Date Formats in java?

前端 未结 4 909
被撕碎了的回忆
被撕碎了的回忆 2021-01-24 16:16

I am getting input as two types.

1.String date1 = \"07/01/2017\";
2.String date2 = \"\"2017-01-12 00:00:00.0\";

How to compare two date formats

4条回答
  •  庸人自扰
    2021-01-24 17:03

    Use RegEx:

        String date1 = "07/01/2017";
        if (date1.matches("^([0-9]{1,2}/){2}[0-9]{2,4}$")) {
            System.out.println("Date in MM/dd/yyyy format");
        } else if (date1.matches("^[0-9]{2,4}(-[0-9]{1,2}){2}\\s[0-9]{1,2}(:[0-9]{1,2}){2}\\.[0-9]{1,}$")) {
            System.out.println("Date in yyyy-MM-dd hh:mm:ss.t format");
        } else {
            System.err.println("Unsupported Date format.");
        }
    

提交回复
热议问题