SHARE
TWEET

[NP] LocalDate

777vasil Nov 19th, 2019 (edited) 83 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Довршете ги методите
  2.  
  3. import java.sql.Date;
  4. import java.sql.Timestamp;
  5. import java.time.*;
  6. import java.time.temporal.TemporalAdjusters;
  7. import java.util.*;
  8. import java.time.LocalDate;
  9. import java.time.Month;
  10. import java.time.ZoneId;
  11. import java.time.format.DateTimeFormatter;
  12.  
  13. /**
  14.  * LocalDate test
  15.  */
  16. public class LocalDateTest {
  17.     public static void main(String[] args) {
  18.         System.out.println(create());
  19.         System.out.println(parse());
  20.         System.out.println(with().getYear());
  21.         System.out.println(withAdjuster());
  22.         System.out.println(plus());
  23.         System.out.println(minus());
  24.         System.out.println(plusPeriod());
  25.         System.out.println(isAfter());
  26.         System.out.println(until());
  27.     }
  28.  
  29.     static LocalDate create() {
  30.        /**
  31.         * Create a {@link LocalDate} of 2015-06-18 by using {@link LocalDate#of}
  32.         */
  33.        
  34.         LocalDate ld = LocalDate.of(2015, Month.JUNE, 18);
  35.         return ld; 
  36.     }
  37.  
  38.     static LocalDate parse() {
  39.         /**
  40.          * Create a {@link LocalDate} of 2015-06-18 from String by using {@link LocalDate#parse}
  41.          */
  42.         LocalDate ld = LocalDate.parse("2015-06-18", DateTimeFormatter.ISO_LOCAL_DATE);
  43.         return ld;
  44.     }
  45.  
  46.     static LocalDate with() {
  47.         LocalDate ld = DateAndTimes.LD_20150618;
  48.         /**
  49.          * Create a {@link LocalDate} from {@link ld} with year 2015
  50.          * by using {@link LocalDate#withYear} or {@link LocalDate#with}
  51.          */
  52.         LocalDate lD = ld.withYear(2015);
  53.  
  54.         return lD;
  55.     }
  56.  
  57.     static LocalDate withAdjuster() {
  58.         LocalDate ld = DateAndTimes.LD_20150618;
  59.         /**
  60.          * Create a {@link LocalDate} from {@link ld} adjusted into first day of next year
  61.          * by using {@link LocalDate#with} and {@link TemporalAdjusters#firstDayOfNextYear}
  62.          */
  63.         LocalDate lD = ld.with(TemporalAdjusters.firstDayOfNextYear());
  64.         return lD;
  65.     }
  66.  
  67.     static LocalDate plus() {
  68.         LocalDate ld = DateAndTimes.LD_20150618;
  69.  
  70.         /**
  71.          * Create a {@link LocalDate} from {@link ld} with 10 month later
  72.          * by using {@link LocalDate#plusMonths} or {@link LocalDate#plus}
  73.          */
  74.         return ld.plusMonths(10);
  75.     }
  76.  
  77.     static LocalDate minus() {
  78.         LocalDate ld = DateAndTimes.LD_20150618;
  79.  
  80.         /**
  81.          * Create a {@link LocalDate} from {@link ld} with 10 days before
  82.          * by using {@link LocalDate#minusDays} or {@link LocalDate#minus}
  83.          */
  84.         return ld.minusDays(10);
  85.     }
  86.  
  87.     static LocalDate plusPeriod() {
  88.         LocalDate ld = DateAndTimes.LD_20150618;
  89.         Period p = Period.of(1, 2, 3);
  90.         /**
  91.          * Define a {@link Period} of 1 year 2 month 3 days
  92.          * Create a {@link LocalDate} adding the period to {@link ld} by using {@link LocalDate#plus}
  93.          */
  94.         return ld.plus(p);
  95.     }
  96.  
  97.     static boolean isAfter() {
  98.         LocalDate ld = DateAndTimes.LD_20150618;
  99.         LocalDate ld2 = DateAndTimes.LD_20150807;
  100.  
  101.         /**
  102.          * Check whether {@link ld2} is after {@link ld} or not
  103.          * by using {@link LocalDate#isAfter} or {@link LocalDate#isBefore}
  104.          */
  105.         return ld2.isAfter(ld);
  106.     }
  107.  
  108.     static Period until() {
  109.         LocalDate ld = DateAndTimes.LD_20150618;
  110.         LocalDate ld2 = DateAndTimes.LD_20150807;
  111.  
  112.         /**
  113.          * Create a period from {@link ld} till {@link ld2}
  114.          * by using {@link LocalDate#until}
  115.          */
  116.         return ld.until(ld2);
  117.     }
  118.  
  119. }
  120.  
  121. class DateAndTimes {
  122.     public static final LocalDate LD_20150618 = LocalDate.of(2015, 6, 18);
  123.     public static final LocalDate LD_20150807 = LocalDate.of(2015, 8, 7);
  124. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top