Skip to content

Commit adf6174

Browse files
authored
Merge pull request mouredev#6818 from h4ckxel/h4ckxel
#14 - Java
2 parents cabb429 + 12018a3 commit adf6174

File tree

2 files changed

+120
-0
lines changed

2 files changed

+120
-0
lines changed

Roadmap/14 - FECHAS/java/h4ckxel.java

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import java.time.*;
2+
import java.time.format.DateTimeFormatter;
3+
import java.time.temporal.ChronoUnit;
4+
5+
public class Main {
6+
7+
public static void main(String[] args) {
8+
// Fecha actual
9+
LocalDate fechaActual = LocalDate.now();
10+
// Fecha de nacimiento
11+
LocalDate fechaNacimiento = LocalDate.of(2005, 4, 15);
12+
13+
// Calculando la diferencia en años
14+
long añosTranscurridos = ChronoUnit.YEARS.between(fechaNacimiento, fechaActual);
15+
System.out.println("Han transcurrido " + añosTranscurridos + " años desde mi nacimiento.");
16+
17+
/*
18+
* DIFICULTAD EXTRA
19+
*/
20+
System.out.println("1. Día, mes y año: " + fechaNacimiento.getDayOfMonth() + "/"
21+
+ fechaNacimiento.getMonthValue() + "/" + fechaNacimiento.getYear());
22+
23+
System.out.println("2. Hora, minuto y segundo: " + fechaNacimiento.atTime(LocalTime.NOON).getHour() + ":"
24+
+ fechaNacimiento.atTime(LocalTime.NOON).getMinute() + ":"
25+
+ fechaNacimiento.atTime(LocalTime.NOON).getSecond());
26+
27+
System.out.println("3. Día de año: " + fechaNacimiento.getDayOfYear());
28+
29+
System.out.println("4. Día de la semana: " + fechaNacimiento.getDayOfWeek());
30+
31+
System.out.println("5. Nombre del mes: " + fechaNacimiento.getMonth());
32+
33+
System.out.println("6. Mes abreviado y año: " + fechaNacimiento.getMonth().name().substring(0, 3) + " "
34+
+ fechaNacimiento.getYear());
35+
36+
System.out.println("7. Hora en formato de 12 horas: "
37+
+ DateTimeFormatter.ofPattern("hh:mm:ss a").format(fechaNacimiento.atTime(LocalTime.NOON)));
38+
39+
System.out.println("8. Edad en días: " + ChronoUnit.DAYS.between(fechaNacimiento, fechaActual));
40+
41+
System.out.println("9. Edad en meses: " + ChronoUnit.MONTHS.between(fechaNacimiento, fechaActual));
42+
43+
System.out.println("10. Día de la semana abreviado, día de mes y año: "
44+
+ fechaNacimiento.getDayOfWeek().name().substring(0, 3) + ", " + fechaNacimiento.getDayOfMonth()
45+
+ " de " + fechaNacimiento.getMonth() + " de " + fechaNacimiento.getYear());
46+
}
47+
}
+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
public class h4ckxel{
2+
3+
public static void main(String[] args) {
4+
try {
5+
exercise();
6+
extra();
7+
8+
} catch (Exception e) {
9+
System.out.println(e.getMessage());
10+
}
11+
}
12+
13+
private static Thread asynFunction(String funcitonName, int duration) {
14+
return new Thread(() -> {
15+
16+
long startTime = System.currentTimeMillis();
17+
System.out.println(
18+
funcitonName + " started. Time: " + startTime +
19+
" milliseconds. Should take " + duration * 1000 + " milliseconds.");
20+
try {
21+
Thread.sleep((long) duration * 1000);
22+
} catch (InterruptedException e) {
23+
System.out.println(e.getMessage());
24+
}
25+
26+
long endTime = System.currentTimeMillis();
27+
System.out.println(funcitonName + " has done. Time: " + (endTime - startTime) + " milliseconds");
28+
29+
});
30+
}
31+
32+
private static void exercise() {
33+
System.out.println("EXERCISE:");
34+
Thread thread = asynFunction("my function1", 20);
35+
thread.start();
36+
try {
37+
38+
thread.join();
39+
} catch (InterruptedException e) {
40+
e.printStackTrace();
41+
}
42+
43+
}
44+
45+
private static void extra() {
46+
47+
System.out.println();
48+
System.out.println("EXTRA:");
49+
50+
Thread threadA = asynFunction("Function A", 1);
51+
Thread threadB = asynFunction("Function B", 2);
52+
Thread threadC = asynFunction("Function C", 3);
53+
Thread threadD = asynFunction("Function D", 1);
54+
55+
try {
56+
threadA.start();
57+
threadB.start();
58+
threadC.start();
59+
60+
threadA.join(5000);
61+
threadB.join(5000);
62+
threadC.join(5000);
63+
64+
System.out.println("Starting Function D...");
65+
threadD.start();
66+
threadD.join();
67+
68+
} catch (InterruptedException e) {
69+
System.out.println(e.getMessage());
70+
}
71+
72+
}
73+
}

0 commit comments

Comments
 (0)