Skip to content

Commit bfe33d0

Browse files
committed
Added LocalDateTime function
1 parent cf77fb9 commit bfe33d0

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

Unit11/src/Functions/GetData.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import java.text.ParsePosition;
44
import java.text.SimpleDateFormat;
55
import java.time.LocalDate;
6+
import java.time.LocalDateTime;
7+
import java.util.Arrays;
68
import java.util.Scanner;
79

810
public class GetData {
@@ -273,4 +275,29 @@ public static LocalDate getFecha(String mensaje) {
273275
// Devolvemos un LocalDate formada gracia al Array (año,mes,día)
274276
return LocalDate.of(Integer.parseInt(splitFecha[2]),Integer.parseInt(splitFecha[1]),Integer.parseInt(splitFecha[0]));
275277
}
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+
}
276303
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy