How to compare to String Date Formats in java?

前端 未结 4 857
被撕碎了的回忆
被撕碎了的回忆 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 16:51

    I guess, we can declare two date formats and parse the dates and compare the two Date instances. Here is sample code:

    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    public class DateCompare {
    
        private static final String FORMAT1 = "MM/dd/yyyy";
        private static final String FORMAT2 = "yyyy-MM-dd hh:mm:ss";
    
        private Date parseDate(String format1, String format2, String dateStr) throws ParseException {
            boolean isException = false;
            Date date = null;
            try {
                date = parseDate(format1, dateStr);
            } catch (ParseException e) {
                isException = true;
            }
            if (isException) {
                date = parseDate(format2, dateStr);
            }
            return date;
        }
    
        private Date parseDate(String format1, String dateStr) throws ParseException {
            SimpleDateFormat dateFormat = new SimpleDateFormat(format1);
            return dateFormat.parse(dateStr);
        }
    
        public static void main(String[] args) throws ParseException {
            DateCompare compare = new DateCompare();
            Date date1 = compare.parseDate(FORMAT1, FORMAT2,"07/01/2017");
            Date date2 = compare.parseDate(FORMAT1, FORMAT2, "2017-07-01 00:00:00");
    
            if (date1.compareTo(date2) == 0) {
                System.out.println("Dates are equal.");
            } else {
                System.out.println("Dates are not equal.");
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-24 16:56

    If you want to know if the date is in format mm/dd/yyyy or mm-dd-yyyy or something in the same idea, you probably need to use regex expressions.

    You can check this for a way to implement it What is the regular expression for Date format dd\mm\yyyy?

    0 讨论(0)
  • 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.");
        }
    
    0 讨论(0)
  • 2021-01-24 17:16

    The solution with RegEx is good, but you can also do in this way:

    The first parameter is your string representation of the date. The next parameter is var arg. So you can pass as many date formats as you wish. It will try to parse and if success then returns proper format.

    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Objects;
    import java.util.Optional;
    import java.util.stream.Stream;
    
    public class Test {
        public static void main(String[] args) {
            Optional<String> format = test("07-01-2017", "dd/mm/yyyy", "dd-mm-yyyy");
            if (format.isPresent())
                System.out.println(format.get());
        }
    
        public static Optional<String> test(String date, String... formats) {
            return Stream.of(formats)
                    .map(format -> new Pair<>(new SimpleDateFormat(format), format))
                    .map(p -> {
                        try {
                            p._1.parse(date);
                            return p._2;
                        } catch (ParseException e) {
                            return null;
                        }
                    })
                    .filter(Objects::nonNull)
                    .findFirst();
        }
    
        public static class Pair<F, S> {
            public final F _1;
            public final S _2;
    
            public Pair(F _1, S _2) {
                this._1 = _1;
                this._2 = _2;
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题