StefiIOE

[NP] 4.3

Nov 12th, 2020
455
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.time.LocalDate;
  2. import java.time.LocalDateTime;
  3. import java.time.LocalTime;
  4. import java.time.format.DateTimeFormatter;
  5. import java.time.temporal.ChronoUnit;
  6. import java.time.temporal.TemporalAdjusters;
  7.  
  8. /**
  9.  * LocalDateTime tests
  10.  */
  11. public class LocalDateTimeTest {
  12.  
  13.     public static void main(String[] args) {
  14.         System.out.println(localDateTimeOf());
  15.         System.out.println(localDateTimeParse());
  16.         System.out.println(localTimeWith());
  17.         System.out.println(localDatePlusMinus());
  18.         System.out.println(localDateTimeFormat());
  19.         System.out.println(toLocalDateAndTime());
  20.         System.out.println(toLocalDateTime());
  21.     }
  22.  
  23.     static LocalDateTime localDateTimeOf() {
  24.         /**
  25.          * Create a {@link LocalDateTime} of 2015-06-20 23:07:30 by using {@link LocalDateTime#of}
  26.          */
  27.         LocalDateTime ldt = LocalDateTime.of(2015, 06, 20, 23, 07, 30);
  28.         return ldt;
  29.     }
  30.  
  31.     static LocalDateTime localDateTimeParse() {
  32.         /**
  33.          * Create a {@link LocalDateTime} of 2015-06-20 23:07:30 by using {@link LocalDateTime#parse}
  34.          */
  35.         //LocalDateTime ldt = LocalDateTime.parse("2015-06-20 23:07:30");
  36.         LocalDateTime ldt = LocalDateTime.of(2015, 06, 20, 23, 07, 30);
  37.         ldt = LocalDateTime.parse("2015-06-20T23:07:30");
  38.        
  39.         return ldt;
  40.     }
  41.  
  42.     static LocalDateTime localTimeWith() {
  43.         LocalDateTime ldt = DateAndTimes.LDT_20150618_23073050;
  44.  
  45.         /**
  46.          * Create a {@link LocalDateTime} from {@link ldt}
  47.          * with first day of the next month and also truncated to hours.
  48.          */
  49.         ldt = ldt.with(TemporalAdjusters.firstDayOfNextMonth());
  50.         ldt = ldt.truncatedTo(ChronoUnit.HOURS);
  51.         return ldt;
  52.     }
  53.  
  54.     static LocalDateTime localDatePlusMinus() {
  55.         LocalDateTime ldt = DateAndTimes.LDT_20150618_23073050;
  56.  
  57.         /**
  58.          * Create a {@link LocalDateTime} from {@link ldt} with 10 month later and 5 hours before
  59.          * by using {@link LocalDateTime#plus*} or {@link LocalDateTime#minus*}
  60.          */
  61.        ldt =  ldt.plusMonths(10);
  62.        
  63.        ldt =  ldt.minusHours(5);
  64.         //System.out.print("Sto se deshava tuka");
  65.         return ldt;
  66.        
  67.     }
  68.  
  69.     static String localDateTimeFormat() {
  70.         LocalDateTime ldt = DateAndTimes.LDT_20150618_23073050;
  71.  
  72.         /**
  73.          * Format {@link ldt} to a {@link String} as "2015_06_18_23_07_30"
  74.          * by using {@link LocalDateTime#format} and {@link DateTimeFormatter#ofPattern}
  75.          */
  76.         DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy_MM_dd_HH_mm_ss");
  77.         String formatedDateTime = ldt.format(format);
  78.         return formatedDateTime;
  79.     }
  80.  
  81.     static String toLocalDateAndTime() {
  82.         LocalDateTime ldt = DateAndTimes.LDT_20150618_23073050;
  83.  
  84.         /**
  85.          * Create a {@link LocalDate} and a {@link LocalTime} from {@link ldt}
  86.          * by using {@link LocalDateTime#toLocalDate} and {@link LocalDateTime#toLocalTime}
  87.          */
  88.         LocalDate localDate = ldt.toLocalDate();
  89.         LocalTime localTime = ldt.toLocalTime();
  90.         return localDate.toString() + localTime.toString();
  91.     }
  92.  
  93.     static String toLocalDateTime() {
  94.         LocalDate ld = DateAndTimes.LD_20150618;
  95.         LocalTime lt = DateAndTimes.LT_23073050;
  96.  
  97.         /**
  98.          * Create two equal {@link LocalDateTime} from {@link ld} and {@link lt}
  99.          * by using {@link LocalDate#atTime} and {@link LocalTime#atDate}
  100.          */
  101.         LocalDateTime localDateTime1 = ld.atTime(lt);
  102.         LocalDateTime localDateTime2 = lt.atDate(ld);
  103.         return localDateTime1.toString() + " " + localDateTime2.toString();
  104.     }
  105.     static class DateAndTimes {
  106.         public static final LocalDate LD_20150618 = LocalDate.of(2015, 6, 18);
  107.         public static final LocalTime LT_23073050 = LocalTime.of(23, 7, 30, 500000000);
  108.         public static final LocalDateTime LDT_20150618_23073050 = LocalDateTime.of(2015, 6, 18, 23, 7, 30, 500000000);
  109.     }
  110. }
  111.  
RAW Paste Data