|
3 | 3 | import java.text.ParsePosition;
|
4 | 4 | import java.text.SimpleDateFormat;
|
5 | 5 | import java.time.LocalDate;
|
| 6 | +import java.time.LocalDateTime; |
| 7 | +import java.util.Arrays; |
6 | 8 | import java.util.Scanner;
|
7 | 9 |
|
8 | 10 | public class GetData {
|
@@ -273,4 +275,29 @@ public static LocalDate getFecha(String mensaje) {
|
273 | 275 | // Devolvemos un LocalDate formada gracia al Array (año,mes,día)
|
274 | 276 | return LocalDate.of(Integer.parseInt(splitFecha[2]),Integer.parseInt(splitFecha[1]),Integer.parseInt(splitFecha[0]));
|
275 | 277 | }
|
| 278 | + |
| 279 | + public static LocalDateTime getDateTime(String s) { |
| 280 | + String date = GetData.getString(s); // Pedimos la Fecha |
| 281 | + // Creamos el formato que deberá tener la fecha |
| 282 | + SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm"); |
| 283 | + // Seleccionamos que el analizador sea estricto por lo cual debe de coincidir con el formato que le hemos puesto |
| 284 | + sdf.setLenient(false); |
| 285 | + // Indicamos que el análisis lo realize desde el index 0 en adelante |
| 286 | + ParsePosition pp = new ParsePosition(0); |
| 287 | + // Intentamos convertir el String de la Fecha en un Date si no puede devuelve null |
| 288 | + java.util.Date d = sdf.parse(date, pp); |
| 289 | + while (d == null) { // Si es null volvemos a pedir la fecha y a pasarla hasta que sea válida |
| 290 | + System.out.println("Ingrese una fecha válida (dd/mm/yyyy HH:mm)"); |
| 291 | + date = GetData.getString(s); |
| 292 | + d = sdf.parse(date, pp); |
| 293 | + } |
| 294 | + // Convertimos el String Fecha en un Array separándolo por "/" |
| 295 | + String[] splitFecha = date.split("/"); |
| 296 | + splitFecha[2] = splitFecha[2].substring(0,4); |
| 297 | + // Convertimos el String Hora en un Array separándolo por ":" |
| 298 | + String[] splitHora = date.split(":"); |
| 299 | + splitHora[0] = splitHora[0].substring(11,13); |
| 300 | + // Devolvemos un LocalDate formada gracia al Array (año,mes,día) |
| 301 | + return LocalDateTime.of(Integer.parseInt(splitFecha[2]),Integer.parseInt(splitFecha[1]),Integer.parseInt(splitFecha[0]),Integer.parseInt(splitHora[0]),Integer.parseInt(splitHora[1])); |
| 302 | + } |
276 | 303 | }
|
0 commit comments