|
1 | 1 | package com.mouredev.weeklychallenge2022 |
2 | 2 |
|
| 3 | +import java.time.LocalDateTime |
| 4 | +import java.time.ZoneId |
| 5 | +import java.time.format.DateTimeFormatter |
| 6 | +import java.util.Calendar |
| 7 | +import java.util.Date |
| 8 | + |
3 | 9 | /* |
4 | 10 | * Reto #48 |
5 | 11 | * EL CALENDARIO DE ADEVIENTO 2022 |
@@ -27,3 +33,90 @@ package com.mouredev.weeklychallenge2022 |
27 | 33 | * https://retosdeprogramacion.com/semanales2022. |
28 | 34 | * |
29 | 35 | */ |
| 36 | + |
| 37 | +fun main() { |
| 38 | + |
| 39 | +// aDEViento2022(LocalDateTime.of(2022, 12, 5, 20, 27, 56).toDate()) |
| 40 | + |
| 41 | + val formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss") |
| 42 | + println(aDEViento2022(LocalDateTime.parse("2022/12/05 20:27:56", formatter).toDate())) |
| 43 | + println(aDEViento2022(LocalDateTime.parse("2022/12/01 00:00:00", formatter).toDate())) |
| 44 | + println(aDEViento2022(LocalDateTime.parse("2022/12/24 23:59:59", formatter).toDate())) |
| 45 | + println(aDEViento2022(LocalDateTime.parse("2022/11/30 23:59:59", formatter).toDate())) |
| 46 | + println(aDEViento2022(LocalDateTime.parse("2022/12/25 00:00:00", formatter).toDate())) |
| 47 | + println(aDEViento2022(LocalDateTime.parse("2022/10/30 00:00:00", formatter).toDate())) |
| 48 | + println(aDEViento2022(LocalDateTime.parse("2022/12/30 04:32:12", formatter).toDate())) |
| 49 | + println(aDEViento2022(LocalDateTime.parse("2020/10/30 00:00:00", formatter).toDate())) |
| 50 | + println(aDEViento2022(LocalDateTime.parse("2024/12/30 04:32:12", formatter).toDate())) |
| 51 | +} |
| 52 | + |
| 53 | +private fun LocalDateTime.toDate(): Date { |
| 54 | + return Date.from(this.atZone(ZoneId.systemDefault()).toInstant()) |
| 55 | +} |
| 56 | + |
| 57 | +private fun aDEViento2022(date: Date): String { |
| 58 | + |
| 59 | + val formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss") |
| 60 | + val startDate = LocalDateTime.parse("2022/12/01 00:00:00", formatter).toDate() |
| 61 | + val endDate = LocalDateTime.parse("2022/12/24 23:59:59", formatter).toDate() |
| 62 | + |
| 63 | + if (date in startDate..endDate) { |
| 64 | + |
| 65 | + val gifts = arrayOf( |
| 66 | + "El programador pragmático", |
| 67 | + "while True: learn()", |
| 68 | + "Aprende Javascript ES9, HTML, CSS3 y NodeJS desde cero", |
| 69 | + "Patrones de Diseño en JavaScript y TypeScript", |
| 70 | + "Aprende Python en un fin de semana", |
| 71 | + "Regalo 6", |
| 72 | + "Regalo 7", |
| 73 | + "Regalo 8", |
| 74 | + "Regalo 9", |
| 75 | + "Regalo 10", |
| 76 | + "Regalo 11", |
| 77 | + "Regalo 12", |
| 78 | + "Regalo 13", |
| 79 | + "Regalo 14", |
| 80 | + "Regalo 15", |
| 81 | + "Regalo 16", |
| 82 | + "Regalo 17", |
| 83 | + "Regalo 18", |
| 84 | + "Regalo 19", |
| 85 | + "Regalo 20", |
| 86 | + "Regalo 21", |
| 87 | + "Regalo 22", |
| 88 | + "Regalo 23", |
| 89 | + "Regalo 24") |
| 90 | + |
| 91 | + val calendar = Calendar.getInstance() |
| 92 | + calendar.time = date |
| 93 | + calendar.set(Calendar.HOUR_OF_DAY, 23) |
| 94 | + calendar.set(Calendar.MINUTE, 59) |
| 95 | + calendar.set(Calendar.SECOND, 59) |
| 96 | + |
| 97 | + val day = calendar.get(Calendar.DAY_OF_MONTH) |
| 98 | + |
| 99 | + return "El regalo del día es: ${gifts[day - 1]} y el sorteo del día acaba en: ${diffTimeComponentsText(date, calendar.time)}" |
| 100 | + } |
| 101 | + |
| 102 | + val intro = if (date < startDate) "El calendario de aDEViento 2022 comenzará en:" else "El calendario de aDEViento 2022 ha finalizado hace:" |
| 103 | + val timeComponents = diffTimeComponentsText(if (date < startDate) date else endDate, |
| 104 | + if (date < startDate) startDate else date) |
| 105 | + return "$intro $timeComponents" |
| 106 | +} |
| 107 | + |
| 108 | +private fun diffTimeComponentsText(startDate: Date, endDate: Date): String { |
| 109 | + |
| 110 | + val diffInMillis = endDate.time - startDate.time |
| 111 | + |
| 112 | + println(diffInMillis) |
| 113 | + |
| 114 | + val second = diffInMillis / 1000L % 60 |
| 115 | + val minutes = diffInMillis / (1000L * 60) % 60 |
| 116 | + val hours = diffInMillis / (1000L * 60 * 60) % 24 |
| 117 | + val days = diffInMillis / (1000L * 60 * 60 * 24) % 365 |
| 118 | + val years = diffInMillis / (1000L * 60 * 60 * 24 * 365) |
| 119 | + |
| 120 | + return "$years años, $days días, $hours horas, $minutes minutos, $second segundos" |
| 121 | +} |
| 122 | + |
0 commit comments