Skip to content

Commit

Permalink
Merge pull request #2662 from ASJordi/main
Browse files Browse the repository at this point in the history
#14 - Java
  • Loading branch information
Roswell468 authored Apr 2, 2024
2 parents b12ba73 + ddeb0ab commit 9aa416c
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions Roadmap/14 - FECHAS/java/asjordi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import java.time.LocalDateTime;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;

public class Main {

public static void main(String[] args) {
calculateYears();
dateFormat();
}

/**
* Crea dos variables utilizando los objetos fecha (date, o semejante) de tu lenguaje:
* - Una primera que represente la fecha (día, mes, año, hora, minuto, segundo) actual.
* - Una segunda que represente tu fecha de nacimiento (te puedes inventar la hora).
* Calcula cuántos años han transcurrido entre ambas fechas.
*/
static void calculateYears() {
LocalDateTime currentDate = LocalDateTime.now();
LocalDateTime birthDate = LocalDateTime.of(1999, 1, 10, 9, 30, 5);
Period p = Period.between(birthDate.toLocalDate(), currentDate.toLocalDate());
System.out.println("Years: " + p.getYears());
}

/**
* DIFICULTAD EXTRA (opcional):
* Utilizando la fecha de tu cumpleaños, formatéala y muestra su resultado de
* 10 maneras diferentes. Por ejemplo:
* - Día, mes y año.
* - Hora, minuto y segundo.
* - Día de año.
* - Día de la semana.
* - Nombre del mes.
*/
static void dateFormat() {
LocalDateTime date = LocalDateTime.of(1999, 1, 10, 9, 30, 5);
System.out.println("yyyy-MM-dd HH:mm:ss: -> " + date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
System.out.println("dd/MM/yyyy HH:mm:ss: -> " + date.format(DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss")));
System.out.println("dd/MM/yyyy -> " + date.format(DateTimeFormatter.ofPattern("dd/MM/yyyy")));
System.out.println("dd/MM/yyyy HH:mm: -> " + date.format(DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm")));
System.out.println("hh:mm:ss a -> " + date.format(DateTimeFormatter.ofPattern("hh:mm:ss a")));
System.out.println("HH:mm:ss -> " + date.format(DateTimeFormatter.ofPattern("HH:mm:ss")));
System.out.println("yyyy-MM-dd -> " + date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
System.out.println(DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL).format(date));
System.out.println(DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM).format(date));
System.out.println(DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT).format(date));
System.out.println("u -> " + date.format(DateTimeFormatter.ofPattern("u")));
System.out.println("M -> " + date.format(DateTimeFormatter.ofPattern("M")));
System.out.println("d -> " + date.format(DateTimeFormatter.ofPattern("d")));
}

}

0 comments on commit 9aa416c

Please sign in to comment.