-3

Any ideas on how I could compare 2 dates in string format:

String1 = "Wed May 18 00:00:00 CDT 2011"
String2= "May. 18, 2011"

I tried converting String1 to date format of String2 but was not very successful.

I tried converting String1 to date using pattern "EEE mmm dd HH:mm:ss z yyyy"

public String formatDateTime(Date date, String pattern) {  
    SimpleDateFormat formatter = new SimpleDateFormat(pattern);  
    String dateString = formatter.format(date);  
    return dateString;  
}
R.Sav
  • 9
  • 3
  • Use `DateFormat`s (or Java 8's `DateTimeFormatter`), convert to machine objects and compare those. – Mena Aug 03 '17 at 15:46
  • Duplicate: [Java string to date conversion](https://stackoverflow.com/q/4216745/642706) & [How to compare dates in Java?](https://stackoverflow.com/q/2592501/642706) – Basil Bourque Aug 03 '17 at 19:08

2 Answers2

1

This solution will work, however, you do need to be mindful of the timezone that the computer is running on. The second date does not have a timezone will cause problems if the second date was generated in say EDT.

String d1 = "Wed May 18 00:00:00 CDT 2011";
String d2 = "May. 18, 2011";

SimpleDateFormat d1Formatter = new SimpleDateFormat("E MMMM dd HH:mm:ss z yyyy");
SimpleDateFormat d2Formatter = new SimpleDateFormat("MMMM. dd, yyyy");

Date date1 = d1Formatter.parse(d1);
Date date2 = d2Formatter.parse(d2);

date1.equals(date2);

Note all of the patterns that I used came from the documentation. I would suggest creating a unit test and testing what different combinations do.

@Test
public void test_date_experimentation() {
    SimpleDateFormat d1Formatter = new SimpleDateFormat("E MMMM dd HH:mm:ss z yyyy");

    // Print the current time in the specified format.
    System.out.println(d1Formatter.format(Date.from(Instant.now())));
}
icirellik
  • 746
  • 6
  • 21
-1

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.