Guest User

Untitled

a guest
Jun 13th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. import org.joda.time.DateTime;
  2. import org.joda.time.LocalDate;
  3. import org.joda.time.format.DateTimeFormat;
  4. import org.joda.time.format.DateTimeFormatter;
  5.  
  6. import java.io.IOException;
  7. import java.util.Calendar;
  8. import java.util.Date;
  9.  
  10. DateTimeFormatter formatter = DateTimeFormat.forPattern( "yyyyMM");
  11. Calendar calendar = Calendar.getInstance();
  12.  
  13. // get the current date
  14. // String currentDate = formatter.print(new DateTime(calendar.getTime()));
  15.  
  16. calendar.add(Calendar.YEAR, -year); // subtract n year from the current date
  17. Date earlierDate = calendar.getTime(); // get the date
  18. String baseLineDateString = formatter.print(new DateTime(earlierDate)); // format the date to yyyyMM formatted string
  19. LocalDate baseLineDate = formatter.parseLocalDate(baseLineDateString); // convert the string to the LocalDate
  20.  
  21. ////////////////////// this is for testing purpose /////////////////////////////////////
  22. DateTimeFormatter testDashedFormatter = DateTimeFormat.forPattern( "yyyy-MM-dd");
  23. String testBaseLineDate = "2016-05-23";
  24. DateTime testBaseLineDateTime = testDashedFormatter.parseDateTime(testBaseLineDate);
  25. String testBaseLineDateString = formatter.print(testBaseLineDateTime);
  26. LocalDate testLocalBaseLineDate = formatter.parseLocalDate(testBaseLineDateString);
  27. System.out.println("test baseline date: " + testLocalBaseLineDate);
  28. ///////////////////////////////////////////////////////////////////////////////////////
  29.  
  30. DateTimeFormatter parserLongFormat = DateTimeFormat.forPattern("yyyy-MM-dd");
  31. String reportingDateLong = "2018-09-12";
  32. DateTime dateTime = parserLongFormat.parseDateTime(reportingDateLong);
  33. String reportingDateString = formatter.print(dateTime); // get the yyyyMM string formatted date
  34. LocalDate reportingDate = formatter.parseLocalDate(reportingDateString); // convert the string to the LocalDate
  35.  
  36. System.out.println("reporting date: " + reportingDate);
  37.  
  38. // boolean isBefore = reportingDate.isBefore(baseLineDate);
  39. boolean isBefore = reportingDate.isBefore(testLocalBaseLineDate);
  40.  
  41. // System.out.println(reportingDate + " is before " + baseLineDate + " : " + isBefore);
  42. System.out.println(reportingDate + " is before " + testLocalBaseLineDate + " : " + isBefore);
Add Comment
Please, Sign In to add comment