Skip to content

Instantly share code, notes, and snippets.

@wellingtonpgp
Created August 17, 2020 02:59
Show Gist options
  • Save wellingtonpgp/ce02546872df35d7f1de74377be15d91 to your computer and use it in GitHub Desktop.
Save wellingtonpgp/ce02546872df35d7f1de74377be15d91 to your computer and use it in GitHub Desktop.
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;
public class DataHoraNoJava8 {
public static void main(String[] args) {
// LocalDate - representa uma data: 06/08/2020
LocalDate now = LocalDate.now();
System.out.println(now);
LocalDate of = LocalDate.of(1990, Month.AUGUST, 6);
System.out.println(of);
// LocalTime - representa uma hora: 11:23:00
LocalTime now2 = LocalTime.now();
System.out.println(now2);
LocalTime of2 = LocalTime.of(11, 10, 01, 100000);
// LocalDateTime - representa data+hora: 06/06/2020 11:45:00 (UTC)
LocalDateTime now3 = LocalDateTime.now();
System.out.println(now3);
LocalDateTime.of(now, now2);
// Instant - representa um instante/momento na linha do tempo (milisegundos a
// partir de 01/01/1970)
// 06/06/2020 11:45:00 GMT/UTC
Instant now4 = Instant.now();
System.out.println(now4);
Instant ii = Instant.ofEpochMilli(5000000L);
System.out.println(ii);
// ZoneDateTime - LocalDateTime
// TimeZone (fuso horário)
// 06/08/1990 11:34:00 GMT-3 (America/Sao_Paulo)
ZonedDateTime now5 = ZonedDateTime.now();
System.out.println(now5);
ZoneId of3 = ZoneId.of("America/Montevideo");
ZonedDateTime of4 = ZonedDateTime.of(now3, of3);
System.out.println(of4);
class Years {
LocalDate inicial;
LocalDate proximo;
}
List<String> datas = new ArrayList<>();
for (int i = 0; i < 15; i++) {
LocalDate atual = LocalDate.now();
Years ano = new Years();
ano.inicial = atual.plusYears(i);
ano.proximo = ano.inicial.plusYears(1);
datas.add(ano.inicial.getYear() + "/" + ano.proximo.getYear());
}
System.out.println(datas);
}
}
/**
2020-08-16
1990-08-06
23:53:11.525
2020-08-16T23:53:11.525
2020-08-17T02:53:11.525Z
1970-01-01T01:23:20Z
2020-08-16T23:53:11.537-03:00[America/Sao_Paulo]
2020-08-16T23:53:11.525-03:00[America/Montevideo]
[2020/2021, 2021/2022, 2022/2023, 2023/2024, 2024/2025, 2025/2026, 2026/2027, 2027/2028, 2028/2029, 2029/2030, 2030/2031, 2031/2032, 2032/2033, 2033/2034, 2034/2035]
**/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment