Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. package com.javamultiplex.datetime;
  2.  
  3. import java.text.DateFormat;
  4. import java.text.ParseException;
  5. import java.text.SimpleDateFormat;
  6. import java.time.Instant;
  7. import java.time.LocalDate;
  8. import java.time.Period;
  9. import java.time.ZoneId;
  10. import java.util.Date;
  11. import java.util.Scanner;
  12.  
  13. /**
  14. *
  15. * @author Rohit Agarwal
  16. * @category Date and Time
  17. * @problem How to calculate age?
  18. *
  19. */
  20. public class CalculateAge {
  21.  
  22. public static void main(String[] args) {
  23.  
  24. Scanner input = null;
  25. try {
  26. input = new Scanner(System.in);
  27. System.out.println("Enter your DOB in dd/MM/yyyy format :");
  28. String dob = input.next();
  29. Date current = new Date();
  30. // If Date is valid, converting String to date.
  31. Date mydob = getValidDate(dob);
  32. if (mydob != null && mydob.before(current)) {
  33.  
  34. // Get default Time zone Id.
  35. ZoneId defaultZoneId = ZoneId.systemDefault();
  36.  
  37. // Convert Date mydob to LocalDate
  38. Instant instant1 = mydob.toInstant();
  39. LocalDate localeDateMyDob = instant1.atZone(defaultZoneId).toLocalDate();
  40. // Convert Date current to LocalDate
  41. Instant instant2 = current.toInstant();
  42. LocalDate localeDateCurrent = instant2.atZone(defaultZoneId).toLocalDate();
  43.  
  44. Period period = Period.between(localeDateMyDob, localeDateCurrent);
  45. System.out.println("Age : " + period.getYears() + " Years " + period.getMonths() + " Months " + period.getDays() + " Days");
  46.  
  47. } else {
  48. System.out.println("DOB is invalid.");
  49. }
  50. } finally {
  51. if (input != null) {
  52. input.close();
  53. }
  54. }
  55.  
  56. }
  57.  
  58. private static Date getValidDate(String date) {
  59.  
  60. Date mydate = null;
  61. if (isValidDateFormat(date)) {
  62. /**
  63. * d -> Day of month
  64. * M -> Month of year
  65. * y -> Year
  66. */
  67. DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
  68. /**
  69. * By default setLenient() is true. We should make it false for
  70. * strict date validations.
  71. *
  72. * If setLenient() is true - It accepts all dates. If setLenient()
  73. * is false - It accepts only valid dates.
  74. */
  75. dateFormat.setLenient(false);
  76. try {
  77. mydate = dateFormat.parse(date);
  78. } catch (ParseException e) {
  79. mydate = null;
  80. }
  81. }
  82. return mydate;
  83. }
  84.  
  85. private static boolean isValidDateFormat(String date) {
  86.  
  87. /**
  88. * Regular Expression that matches String with format dd/MM/yyyy.
  89. * dd -> 01-31
  90. * MM -> 01-12
  91. * yyyy -> 4 digit number
  92. */
  93. String pattern = "(0?[1-9]|[12][0-9]|3[01])\\/(0?[1-9]|1[0-2])\\/([0-9]{4})";
  94. boolean result = false;
  95. if (date.matches(pattern)) {
  96. result = true;
  97. }
  98. return result;
  99. }
  100.  
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement