fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.math.BigDecimal;
  7. import java.time.Instant;
  8. import java.time.LocalDate;
  9. import java.time.Year;
  10. import java.time.YearMonth;
  11. import java.time.ZoneOffset;
  12. import java.time.format.DateTimeParseException;
  13. import java.time.format.DateTimeFormatter;
  14.  
  15.  
  16. /* Name of the class has to be "Main" only if the class is public. */
  17. class Ideone
  18. {
  19. public static void main(String[] args) {
  20. String[] strs = {"2018", "2010", "2009",
  21. "2018-03", "2010-02", "2010-03",
  22. "2012-01-05", "2020-03-01", "2020-04-01"};
  23.  
  24. Instant start = Instant.parse("2010-05-01T00:00:00Z");
  25. Instant end = Instant.parse("2020-03-01T23:59:59.999999999Z");
  26.  
  27. for (String str : strs) {
  28. System.out.println(isValid(str, start, end));
  29. }
  30. }
  31.  
  32. public static boolean isValid(String date, Instant start, Instant end) {
  33. LocalDate sld = start.atOffset(ZoneOffset.UTC).toLocalDate();
  34. LocalDate eld = end.atOffset(ZoneOffset.UTC).toLocalDate();
  35. try {
  36. LocalDate ld = LocalDate.parse(date);
  37. return ld.isAfter(sld) && ld.isBefore(eld);
  38. } catch (DateTimeParseException e) {}
  39.  
  40. try {
  41. YearMonth ym = YearMonth.parse(date);
  42. return ym.isAfter(YearMonth.from(sld)) && ym.isBefore(YearMonth.from(eld));
  43. } catch (DateTimeParseException e) {}
  44.  
  45. try {
  46. Year y = Year.parse(date);
  47. return y.isAfter(Year.from(sld)) && y.isBefore(Year.from(eld));
  48. } catch (DateTimeParseException e) {
  49. }
  50. return false;
  51. }
  52. }
Success #stdin #stdout 0.09s 35124KB
stdin
Standard input is empty
stdout
true
false
false
true
false
false
true
false
false