Advertisement
Guest User

Untitled

a guest
Dec 17th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1. import java.sql.Date;
  2. import java.sql.Timestamp;
  3. import java.time.*;
  4. import java.time.format.DateTimeFormatter;
  5. import java.time.temporal.ChronoUnit;
  6. import java.time.temporal.TemporalAdjuster;
  7. import java.time.temporal.TemporalAdjusters;
  8.  
  9. /**
  10. * LocalDate test
  11. */
  12. public class LocalDateTest {
  13. public static void main(String[] args) {
  14. System.out.println(create());
  15. System.out.println(parse());
  16. System.out.println(with().getYear());
  17. System.out.println(withAdjuster());
  18. System.out.println(plus());
  19. System.out.println(minus());
  20. System.out.println(plusPeriod());
  21. System.out.println(isAfter());
  22. System.out.println(until());
  23. }
  24.  
  25. static LocalDate create() {
  26. /**
  27. * Create a {@link LocalDate} of 2015-06-18 by using {@link LocalDate#of}
  28. */
  29. return LocalDate.of(2015,Month.JUNE,18);
  30. }
  31.  
  32. static LocalDate parse() {
  33. /**
  34. * Create a {@link LocalDate} of 2015-06-18 from String by using {@link LocalDate#parse}
  35. */
  36. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MMMM-dd");
  37.  
  38. String date = "2015-June-18";
  39.  
  40. return LocalDate.parse(date, formatter);
  41. }
  42.  
  43. static LocalDate with() {
  44. LocalDate ld = DateAndTimes.LD_20150618;
  45. /**
  46. * Create a {@link LocalDate} from {@link ld} with year 2015
  47. * by using {@link LocalDate#withYear} or {@link LocalDate#with}
  48. */
  49.  
  50. return ld.withYear(2015);
  51. }
  52.  
  53. static LocalDate withAdjuster() {
  54. LocalDate ld = DateAndTimes.LD_20150618;
  55. /**
  56. * Create a {@link LocalDate} from {@link ld} adjusted into first day of next year
  57. * by using {@link LocalDate#with} and {@link TemporalAdjusters#firstDayOfNextYear}
  58. */
  59. return ld.with(TemporalAdjusters.firstDayOfNextYear());
  60. }
  61.  
  62. static LocalDate plus() {
  63. LocalDate ld = DateAndTimes.LD_20150618;
  64.  
  65. /**
  66. * Create a {@link LocalDate} from {@link ld} with 10 month later
  67. * by using {@link LocalDate#plusMonths} or {@link LocalDate#plus}
  68. */
  69. return ld.plus(10,ChronoUnit.MONTHS);
  70. }
  71.  
  72. static LocalDate minus() {
  73. LocalDate ld = DateAndTimes.LD_20150618;
  74.  
  75. /**
  76. * Create a {@link LocalDate} from {@link ld} with 10 days before
  77. * by using {@link LocalDate#minusDays} or {@link LocalDate#minus}
  78. */
  79. return ld.minus(10, ChronoUnit.DAYS);
  80. }
  81.  
  82. static LocalDate plusPeriod() {
  83. LocalDate ld = DateAndTimes.LD_20150618;
  84.  
  85. /**
  86. * Define a {@link Period} of 1 year 2 month 3 days
  87. * Create a {@link LocalDate} adding the period to {@link ld} by using {@link LocalDate#plus}
  88. */
  89. Period y2m3d = Period.of(1, 2, 3);
  90.  
  91. return ld.plus(y2m3d);
  92. }
  93.  
  94. static boolean isAfter() {
  95. LocalDate ld = DateAndTimes.LD_20150618;
  96. LocalDate ld2 = DateAndTimes.LD_20150807;
  97.  
  98. /**
  99. * Check whether {@link ld2} is after {@link ld} or not
  100. * by using {@link LocalDate#isAfter} or {@link LocalDate#isBefore}
  101. */
  102. return ld.isBefore(ld2);
  103. }
  104.  
  105. static Period until() {
  106. LocalDate ld = DateAndTimes.LD_20150618;
  107. LocalDate ld2 = DateAndTimes.LD_20150807;
  108.  
  109. /**
  110. * Create a period from {@link ld} till {@link ld2}
  111. * by using {@link LocalDate#until}
  112. */
  113. return Period.between(ld, ld2);
  114. }
  115.  
  116. }
  117.  
  118. class DateAndTimes {
  119. public static final LocalDate LD_20150618 = LocalDate.of(2015, 6, 18);
  120. public static final LocalDate LD_20150807 = LocalDate.of(2015, 8, 7);
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement