Skip to content

Instantly share code, notes, and snippets.

@bhalash
Created April 23, 2016 14:08
Show Gist options
  • Select an option

  • Save bhalash/0e9e319870b68e78ee8527c8c92c25b9 to your computer and use it in GitHub Desktop.

Select an option

Save bhalash/0e9e319870b68e78ee8527c8c92c25b9 to your computer and use it in GitHub Desktop.
Date comparison
import java.text.SimpleDateFormat;
import java.util.Date;
public class AskDate {
public static void main(String args[]) {
Date today = new Date();
Date birthday = stringToDate("May 8, 1981");
// Date birthday = stringToDate("April 23, 2016");
System.out.println(today);
System.out.println(birthday);
System.out.println(datesAreSame(today, birthday));
}
public static Date stringToDate(String str) {
SimpleDateFormat format = new SimpleDateFormat("MMM d, yyyy");
Date parsedDate = new Date();
try {
parsedDate = format.parse(str);
} catch (Exception e) {
System.out.println(e.toString());
}
return parsedDate;
}
public static boolean datesAreSame(Date date1, Date date2) {
SimpleDateFormat format = new SimpleDateFormat("MMM d, yyyy");
return format.format(date1).equals(format.format(date2));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment