Reputation: 2321
I have two disparate date formats presented in my application as strings. Here are the formats:
I'm looking for the most efficient way to assert their equality.
Upvotes: 2
Views: 375
Reputation: 7070
You can use a SimpleDateFormat
to make them into Dates
(or Calendar
objects) and compare them like that.
SimpleDateFormat format1 = new SimpleDateFormat("MM/dd/yyyy");
SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd");
MM is the month make sure they are capitalized.
Upvotes: 1
Reputation: 11308
Parse both dates using SimpleDateFormat
and then use the equals()
method.
The formats to use will be "MM/dd/yyyy"
and "yyyy-MM-dd"
.
Sample code:
SimpleDateFormat format1 = new SimpleDateFormat("MM/dd/yyyy");
SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = format1.parse(value1);
Date date2 = format2.parse(value2);
return date1.equals(date2);
Upvotes: 7