How to compare to String Date Formats in java?

2020-04-21 04:11发布

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. I want to perform one functionality if I get format as date1.If I get date2 format then Another functionality. How to compare two date formats using strings.

Ex:

 if( date1.equals('dd/mm/yyyy')){

    //perform functionality

    }

标签: java date
4条回答
成全新的幸福
2楼-- · 2020-04-21 04:22

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?

查看更多
够拽才男人
3楼-- · 2020-04-21 04:30

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.");
        }
    }
}
查看更多
家丑人穷心不美
4楼-- · 2020-04-21 04:33

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.");
    }
查看更多
Anthone
5楼-- · 2020-04-21 04:39

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;
        }
    }
}
查看更多
登录 后发表回答