Created
April 23, 2016 14:08
-
-
Save bhalash/0e9e319870b68e78ee8527c8c92c25b9 to your computer and use it in GitHub Desktop.
Date comparison
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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