Advertisement
StefiIOE

[NP] 4.1

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