To convert a String
to a Date
, you should use the parse
method. But you're using the format
method, which does the opposite (it converts a Date
to a String
).
To check if the 2 String
's correspond to the same date, you must parse them with a SimpleDateFormat
for each one. But there are 2 details to keep in mind:
Note that the month and day of week are in English, so you should use a java.util.Locale
to indicate what language you want to use. If you don't specify one, it'll use the system's default, and it's not guaranteed to always be English.
Even if it is, this configuration can be changed without notice, even at runtime, so it's always better to explicity use a specific one.
The first String
has timezone information: CDT
- which I'm assuming it's Central Daylight Time of USA (although it could also be Cuba Daylight Time). But the second String
doesn't have any timezone information, so I'm assuming it'll be in the same timezone as the first String
.
If I don't specify it, the system's default will be used and it might give you different results if it's not CDT. And similar to what happen to locales, the default can be changed without notice, even at runtime, so it's better to specify which one you're using.
String s1 = "Wed May 18 00:00:00 CDT 2011";
String s2 = "May. 18, 2011";
// use English locale for month and day of week
SimpleDateFormat sdf1 = new SimpleDateFormat("E MMMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
SimpleDateFormat sdf2 = new SimpleDateFormat("MMMM. dd, yyyy", Locale.ENGLISH);
// set timezone, because String 2 has no timezone information
sdf2.setTimeZone(TimeZone.getTimeZone("America/Chicago"));
// parse the dates
Date date1 = sdf1.parse(s1);
Date date2 = sdf2.parse(s2);
// compare them
boolean areDatesEqual = date1.equals(date2);
The value of variable areDatesEqual
will be true
(dates are the same).
Note that I used America/Chicago
as timezone name. If I used CDT
, the TimeZone
class doesn't recognize it and it gives incorrect results.
It's always better to use IANA timezones names (always in the format Region/City
, like America/Chicago
or Europe/London
).
Avoid using the 3-letter abbreviations (like CDT
or PST
) because they are ambiguous and not standard.
There is more than one timezone that uses CDT, so you must choose the one that fits best to your system.
You can get a list of available timezones by calling TimeZone.getAvailableIDs()
.
Java new Date/Time API
The old classes (Date
, Calendar
and SimpleDateFormat
) have lots of problems and design issues, and they're being replaced by the new APIs.
If you're using Java 8, consider using the new java.time API. It's easier, less bugged and less error-prone than the old APIs.
If you're using Java <= 7, you can use the ThreeTen Backport, a great backport for Java 8's new date/time classes. And for Android, there's the ThreeTenABP (more on how to use it here).
The code below works for both.
The only difference is the package names (in Java 8 is java.time
and in ThreeTen Backport (or Android's ThreeTenABP) is org.threeten.bp
), but the classes and methods names are the same.
As you are comparing just the date (day/month/year), it's better to just work with those fields and ignore the rest (hour/minute/second and timezone information). For that, you can use a LocalDate
and a DateTimeFormatter
:
String s1 = "Wed May 18 00:00:00 CDT 2011";
String s2 = "May. 18, 2011";
// use English locale for month and day of week
DateTimeFormatter fmt1 = DateTimeFormatter.ofPattern("E MMMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
DateTimeFormatter fmt2 = DateTimeFormatter.ofPattern("MMMM. dd, yyyy", Locale.ENGLISH);
// parse the strings
LocalDate d1 = LocalDate.parse(s1, fmt1);
LocalDate d2 = LocalDate.parse(s2, fmt2);
// compare the dates
boolean areDatesEqual = d1.equals(d2);
As the LocalDate
has only day, hour and minute (no hours/minutes/seconds and no timezone information), you don't need to care about setting timezones in the formatters - although the concern on the English locale remains.
The result will also be true
.