|
| 1 | +import java.time.LocalDateTime; |
| 2 | +import java.time.format.DateTimeFormatter; |
| 3 | +import java.time.temporal.ChronoUnit; |
| 4 | +import java.time.temporal.TemporalUnit; |
| 5 | + |
| 6 | +public class Josegs95 { |
| 7 | + public static void main(String[] args) { |
| 8 | + //Ejercicio |
| 9 | + LocalDateTime nowTime = LocalDateTime.now(); |
| 10 | + LocalDateTime birthdayTime = LocalDateTime.of(1995, 02, 28, 17, 30, 0); |
| 11 | + System.out.println("Fecha y hora actual: " + nowTime); |
| 12 | + System.out.println("Fecha y hora cuando nací: " + birthdayTime); |
| 13 | + |
| 14 | + System.out.println("Años entre mi nacimiento y ahora: " + birthdayTime.until(nowTime, ChronoUnit.YEARS)); |
| 15 | + //Reto |
| 16 | + System.out.println("\n"); |
| 17 | + retoFinal(birthdayTime); |
| 18 | + } |
| 19 | + |
| 20 | + public static void retoFinal(LocalDateTime birthdayTime){ |
| 21 | + System.out.println("Mi fecha de nacimiento en diferentes formatos: "); |
| 22 | + //Estándar |
| 23 | + System.out.println(birthdayTime); |
| 24 | + //Dia-mes-año |
| 25 | + System.out.println(birthdayTime.format(DateTimeFormatter.ofPattern("dd-MM-YYYY"))); |
| 26 | + //Hora-minuto-segundo |
| 27 | + System.out.println(birthdayTime.format(DateTimeFormatter.ofPattern("kk:mm:ss"))); |
| 28 | + //Dia del año |
| 29 | + System.out.println(birthdayTime.format(DateTimeFormatter.ofPattern("DDD"))); |
| 30 | + //Dia de la semana |
| 31 | + System.out.println(birthdayTime.format(DateTimeFormatter.ofPattern("EEEE"))); |
| 32 | + //Nombre del mes |
| 33 | + System.out.println(birthdayTime.format(DateTimeFormatter.ofPattern("LLLL"))); |
| 34 | + //Quadrimestre |
| 35 | + System.out.println(birthdayTime.format(DateTimeFormatter.ofPattern("QQ"))); |
| 36 | + //Semana del año |
| 37 | + System.out.println(birthdayTime.format(DateTimeFormatter.ofPattern("ww"))); |
| 38 | + //Semana del mes |
| 39 | + System.out.println(birthdayTime.format(DateTimeFormatter.ofPattern("W"))); |
| 40 | + //Hora-minuto AM/PM |
| 41 | + System.out.println(birthdayTime.format(DateTimeFormatter.ofPattern("hh:mm a"))); |
| 42 | + //Dia de la semana, Dia-mes-año, hora:minuto |
| 43 | + System.out.println(birthdayTime.format(DateTimeFormatter.ofPattern("EEEE, dd-MM-YYYY 'a las' kk:mm"))); |
| 44 | + |
| 45 | + } |
| 46 | +} |
0 commit comments