Created
October 14, 2019 18:45
-
-
Save analog-alex/6e16b763181410aa13f39091c0be3f1d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.analog.alex; | |
import java.time.*; | |
import java.time.format.DateTimeFormatter; | |
public class Main { | |
public static void main(String[] args) { | |
/* * | |
* Java Time (some examples) | |
*/ | |
// Instant -- representa um instante especifico no tempo (Coordinated Time Zone) | |
Instant instant = Instant.now(); | |
System.out.println(instant); // 2019-10-14T18:41:41.173378Z | |
Instant instantPlusTwoSeconds = instant.plusSeconds(2); | |
Instant instantPlusOneHour = instant.plus(Duration.ofMinutes(1)); | |
System.out.println(instantPlusTwoSeconds); // 2019-10-14T18:41:43.173378Z | |
System.out.println(instantPlusOneHour); // 2019-10-14T18:42:41.173378Z | |
// LocalTime -- representa um tempo (hora:minuto:segundo) sem informação de Fuso Horário | |
LocalTime localTime = LocalTime.now(); | |
System.out.println(localTime); // 19:41:41.209834 | |
LocalTime exactlyAt = LocalTime.of(22, 33, 42); | |
System.out.println(exactlyAt); // 22:33:42 | |
// LocalDate & LocalDateTime | |
// LocalDate -- representa uma data de caléndário (ano, mes, dia) sem informação de Fuso Horário | |
// LocalDateTime -- a junção do LocalDate e LocalTime | |
LocalDate localDate = LocalDate.of(2000, Month.JULY, 31); | |
System.out.println(localDate); // 2000-07-31 | |
LocalDateTime localDateTime = LocalDateTime.of(localDate, localTime); | |
System.out.println(localDateTime); // 2000-07-31T19:43:24.734356 | |
// ZonedDateTime -- representa uma data (calendário + hora:minuto) tendo em conta o Fuso Horário | |
// **** no meu caso 'Lisboa' (GMT) **** | |
ZonedDateTime zonedDateTime = ZonedDateTime.now(); | |
System.out.println(zonedDateTime); // 2000-07-31T19:43:24.734356 | |
System.out.println( zonedDateTime.getHour() + ":" + zonedDateTime.getMinute()); // 19:43 | |
// some string operations | |
var format = DateTimeFormatter.ofPattern("dd/MM/yyyy"); | |
var parsed = LocalDate.parse("22/01/2001", format); | |
System.out.println(parsed); // 2001-01-22 | |
var formatTo = DateTimeFormatter.ofPattern("dd -MMM-yyyy"); | |
System.out.println(parsed.format(formatTo)); // 22 -Jan-2001 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment