StefiIOE

[NP] 4.2

Nov 12th, 2020
436
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.time.Duration;
  2. import java.time.LocalTime;
  3. import java.time.temporal.ChronoField;
  4. import java.time.temporal.ChronoUnit;
  5.  
  6. /**
  7.  * LocalTime API tests
  8.  */
  9. public class LocalTimeTest {
  10.     public static void main(String[] args) {
  11.         System.out.println(localTimeOfHourToMinute());
  12.         System.out.println(localTimeOfHourToNanoSec());
  13.         System.out.println(localTimeParse());
  14.         System.out.println(localTimeWith());
  15.         System.out.println(localTimePlus());
  16.         System.out.println(localTimeMinus());
  17.         System.out.println(localTimeMinusDuration());
  18.         System.out.println(localDateIsBefore());
  19.         System.out.println(localTimeTruncatedTo());
  20.     }
  21.  
  22.     static LocalTime localTimeOfHourToMinute() {
  23.         /**
  24.          * Create a {@link LocalTime} of 23:07 by using {@link LocalTime#of}
  25.          */
  26.         LocalTime time = LocalTime.of(23, 07);
  27.  
  28.          return time;
  29.     }
  30.  
  31.     static LocalTime localTimeOfHourToNanoSec() {
  32.         /**
  33.          * Create a {@link LocalTime} of 23:07:03.1 by using {@link LocalTime#of}
  34.          */
  35.         LocalTime time = LocalTime.of(23, 07, 03, 100000000);
  36.         return time;
  37.     }
  38.    
  39.     static LocalTime localTimeParse() {
  40.         /**
  41.          * Create a {@link LocalTime} of 23:07:03.1 from String by using {@link LocalTime#parse}
  42.          */
  43.         LocalTime time = LocalTime.parse("23:07:03.1");
  44.         return time;
  45.     }
  46.  
  47.     static LocalTime localTimeWith() {
  48.         LocalTime lt = DateAndTimes.LT_23073050;
  49.  
  50.         /**
  51.          * Create a {@link LocalTime} from {@link lt} with hour 21
  52.          * by using {@link LocalTime#withHour} or {@link LocalTime#with}
  53.          */
  54.         return  lt.withHour(21);
  55.        
  56.     }
  57.  
  58.     static LocalTime localTimePlus() {
  59.         LocalTime lt = DateAndTimes.LT_23073050;
  60.  
  61.         /**
  62.          * Create a {@link LocalTime} from {@link lt} with 30 minutes later
  63.          * by using {@link LocalTime#plusMinutes} or {@link LocalTime#plus}
  64.          */
  65.         return lt.plusMinutes(30);
  66.          
  67.     }
  68.  
  69.     static LocalTime localTimeMinus() {
  70.         LocalTime lt = DateAndTimes.LT_23073050;
  71.  
  72.         /**
  73.          * Create a {@link LocalTime} from {@link lt} with 3 hours before
  74.          * by using {@link LocalTime#minusHours} or {@link LocalTime#minus}
  75.          */
  76.         return lt.minusHours(3);
  77.        
  78.     }
  79.  
  80.  
  81.     static LocalTime localTimeMinusDuration() {
  82.         LocalTime lt = DateAndTimes.LT_23073050;
  83.  
  84.         /**
  85.          * Define a {@link Duration} of 3 hours 30 minutes and 20.2 seconds
  86.          * Create a {@link LocalTime} subtracting the duration from {@link lt} by using {@link LocalTime#minus}
  87.          */
  88.         lt = lt.minusHours(3);
  89.         lt = lt.minusMinutes(30);
  90.         lt = lt.minusSeconds(20);
  91.         lt = lt.minusNanos(200000000);
  92.         return lt;
  93.     }
  94.  
  95.     static boolean localDateIsBefore() {
  96.         LocalTime lt = DateAndTimes.LT_23073050;
  97.         LocalTime lt2 = DateAndTimes.LT_12100000;
  98.         /**
  99.          * Check whether {@link lt2} is before {@link lt} or not
  100.          * by using {@link LocalTime#isAfter} or {@link LocalTime#isBefore}
  101.          */
  102.         if(lt.isAfter(lt2)){return true;}
  103.         else {return false;}
  104.        
  105.     }
  106.  
  107.     static LocalTime localTimeTruncatedTo() {
  108.         LocalTime lt = DateAndTimes.LT_23073050;
  109.  
  110.         /**
  111.          * Create a {@link LocalTime} from {@link lt} truncated to minutes by using {@link LocalTime#truncatedTo}
  112.          */
  113.         return lt.truncatedTo(ChronoUnit.MINUTES);
  114.        
  115.     }
  116.  
  117.     static class DateAndTimes {
  118.         public static final LocalTime LT_23073050 = LocalTime.of(23, 7, 30, 500000000);
  119.         public static final LocalTime LT_12100000 = LocalTime.of(12, 10);
  120.     }
  121.  
  122. }
  123.  
RAW Paste Data