-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEjemploJavaUtilDateParse.java
More file actions
46 lines (38 loc) · 1.71 KB
/
EjemploJavaUtilDateParse.java
File metadata and controls
46 lines (38 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class EjemploJavaUtilDateParse {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
System.out.println("Ingrese una fecha con formato 'yyyy-MM-dd'");
try {
Date fecha = format.parse(s.next());
System.out.println("fecha = " + fecha);
System.out.println("format = " + format.format(fecha));
Date fecha2 = new Date();
System.out.println("fecha2 = " + fecha2);
if(fecha.after(fecha2)){
System.out.println("fecha1 (del usuario) es después que fecha2 (actual)");
} else if(fecha.before(fecha2)){
System.out.println("fecha es anterior que fecha2");
} else if(fecha.equals(fecha2)){
System.out.println("fecha es igual a fecha2");
}
if(fecha.compareTo(fecha2) > 0){
System.out.println("fecha1 (del usuario) es después que fecha2 (actual)");
} else if(fecha.compareTo(fecha2) < 0){
System.out.println("fecha es anterior que fecha2");
} else if(fecha.compareTo(fecha2) == 0){
System.out.println("fecha es igual a fecha2");
}
} catch (ParseException e) {
//e.printStackTrace();
System.err.println("La fecha tiene un formato incorrecto: " + e.getMessage());
System.err.println("El formato debe ser 'yyyy-MM-dd'");
//System.exit(1);
main(args);
}
}
}