SHARE
TWEET

[NP] LocalTime

777vasil Nov 19th, 2019 85 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 lt = LocalTime.of(23, 07);
  27.         return lt;
  28.     }
  29.  
  30.     static LocalTime localTimeOfHourToNanoSec() {
  31.         /**
  32.          * Create a {@link LocalTime} of 23:07:03.1 by using {@link LocalTime#of}
  33.          */
  34.         LocalTime lt = LocalTime.of(23, 07, 03, 100000000);
  35.         return lt;
  36.     }
  37.  
  38.     static LocalTime localTimeParse() {
  39.         /**
  40.          * Create a {@link LocalTime} of 23:07:03.1 from String by using {@link LocalTime#parse}
  41.          */
  42.         LocalTime lt = LocalTime.parse("23:07:03.1");
  43.         return lt;
  44.     }
  45.  
  46.     static LocalTime localTimeWith() {
  47.         LocalTime lt = DateAndTimes.LT_23073050;
  48.  
  49.         /**
  50.          * Create a {@link LocalTime} from {@link lt} with hour 21
  51.          * by using {@link LocalTime#withHour} or {@link LocalTime#with}
  52.          */
  53.         return lt.withHour(21);
  54.     }
  55.  
  56.     static LocalTime localTimePlus() {
  57.         LocalTime lt = DateAndTimes.LT_23073050;
  58.  
  59.         /**
  60.          * Create a {@link LocalTime} from {@link lt} with 30 minutes later
  61.          * by using {@link LocalTime#plusMinutes} or {@link LocalTime#plus}
  62.          */
  63.         return lt.plusMinutes(30);
  64.     }
  65.  
  66.     static LocalTime localTimeMinus() {
  67.         LocalTime lt = DateAndTimes.LT_23073050;
  68.  
  69.         /**
  70.          * Create a {@link LocalTime} from {@link lt} with 3 hours before
  71.          * by using {@link LocalTime#minusHours} or {@link LocalTime#minus}
  72.          */
  73.         return lt.minusHours(3);
  74.     }
  75.  
  76.  
  77.     static LocalTime localTimeMinusDuration() {
  78.         LocalTime lt = DateAndTimes.LT_23073050;
  79.  
  80.         /**
  81.          * Define a {@link Duration} of 3 hours 30 minutes and 20.2 seconds
  82.          * Create a {@link LocalTime} subtracting the duration from {@link lt} by using {@link LocalTime#minus}
  83.          */
  84.         Duration d = Duration.ofHours(3);
  85.         d = d.plusMinutes(30);
  86.         d = d.plusSeconds(20);
  87.         d = d.plusNanos(200000000);
  88.         return lt.minus(d);
  89.     }
  90.  
  91.     static boolean localDateIsBefore() {
  92.         LocalTime lt = DateAndTimes.LT_23073050;
  93.         LocalTime lt2 = DateAndTimes.LT_12100000;
  94.         /**
  95.          * Check whether {@link lt2} is before {@link lt} or not
  96.          * by using {@link LocalTime#isAfter} or {@link LocalTime#isBefore}
  97.          */
  98.         return lt2.isBefore(lt);
  99.     }
  100.  
  101.     static LocalTime localTimeTruncatedTo() {
  102.         LocalTime lt = DateAndTimes.LT_23073050;
  103.  
  104.         /**
  105.          * Create a {@link LocalTime} from {@link lt} truncated to minutes by using {@link LocalTime#truncatedTo}
  106.          */
  107.         return lt.truncatedTo(ChronoUnit.MINUTES);
  108.     }
  109.  
  110.     static class DateAndTimes {
  111.         public static final LocalTime LT_23073050 = LocalTime.of(23, 7, 30, 500000000);
  112.         public static final LocalTime LT_12100000 = LocalTime.of(12, 10);
  113.     }
  114.  
  115. }
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