SHARE
TWEET

Untitled

a guest Jul 16th, 2019 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package dev.rinaldo.datahora;
  2.  
  3. import java.time.Instant;
  4. import java.time.LocalDate;
  5. import java.time.LocalDateTime;
  6. import java.time.LocalTime;
  7. import java.time.Month;
  8. import java.time.ZoneId;
  9. import java.time.ZonedDateTime;
  10. import java.time.temporal.ChronoUnit;
  11.  
  12. /**
  13.  * @author RinaldoDev
  14.  */
  15. // JAVA 8 - API de Data e Hora
  16. public class DataHoraJava8 {
  17.  
  18.   public static void main(String[] args) {
  19.    
  20.     // LocalDate - representa uma data: 06/08/1990
  21.     LocalDate ldNow = LocalDate.now();
  22.     System.out.println(ldNow);
  23.    
  24.     LocalDate ld = LocalDate.of(1990, Month.AUGUST, 6);
  25.     System.out.println(ld);
  26.     LocalDate ld2 = ld.plusWeeks(2).plusDays(1).minusMonths(1);
  27.     System.out.println(ld2);
  28.    
  29.     // LocalTime - representa uma hora: 11:40:00
  30.     LocalTime ltNow = LocalTime.now();
  31.     System.out.println(ltNow);
  32.     LocalTime lt = LocalTime.of(11, 10, 01, 100000);
  33.     System.out.println(lt);
  34.    
  35.     // LocalDateTime - representa data+hora:
  36.     // 06/08/1990 11:40:00
  37.     LocalDateTime ldtNow = LocalDateTime.now();
  38.     System.out.println(ldtNow);
  39.     LocalDateTime ldt = LocalDateTime.of(ld, lt);
  40.     System.out.println(ldt);
  41.    
  42.     // Instant - representa um instante/momento na
  43.     // linha do tempo (milissegundos a partir de
  44.     // 01/01/1970 00:00:00
  45.     // 06/08/1990 11:40:00 GMT/UTC
  46.     Instant iNow = Instant.now();
  47.     System.out.println(iNow);
  48.     Instant i = Instant.ofEpochMilli(500000000000L);
  49.     System.out.println(i);
  50.     Instant plus = i.plus(1, ChronoUnit.MONTHS);
  51.     System.out.println(plus);
  52.    
  53.     // ZonedDateTime - LocalDateTime com
  54.     // TimeZone (fuso horĂ¡rio)
  55.     // 06/08/1990 11:40:00 GMT-3 (America/Sao_Paulo)
  56.     ZonedDateTime zdtNow = ZonedDateTime.now();
  57.     System.out.println(zdtNow);
  58.     ZoneId zoneId = ZoneId.of("America/Montevideo");
  59.     ZonedDateTime zdt = ZonedDateTime.of(ld, lt, zoneId);
  60.     System.out.println(zdt);
  61.    
  62.   }
  63.  
  64. }
  65.  
  66. //YouTube:  RinaldoDev
  67. //Twitter:  @rinaldodev
  68. //LinkedIn: rinaldodev
  69. //Facebook: rinaldo.dev
  70. //Blog:     rinaldo.dev
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