Repository navigation
Expand file tree
/
Copy pathfechas.java
More file actions
88 lines (79 loc) · 3.25 KB
/
Copy pathfechas.java
File metadata and controls
88 lines (79 loc) · 3.25 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
//@author Henry Joe Wong Urquiza
public class ManejadorFechas {
//Metodo usado para obtener la fecha actual
//@return Retorna un <b>STRING</b> con la fecha actual formato "dd-MM-yyyy"
public static String getFechaActual() {
Date ahora = new Date();
SimpleDateFormat formateador = new SimpleDateFormat("dd-MM-yyyy");
return formateador.format(ahora);
}
//Metodo usado para obtener la hora actual del sistema
//@return Retorna un <b>STRING</b> con la hora actual formato "hh:mm:ss"
public static String getHoraActual() {
Date ahora = new Date();
SimpleDateFormat formateador = new SimpleDateFormat("hh:mm:ss");
return formateador.format(ahora);
}
//Sumarle dias a una fecha determinada
//@param fch La fecha para sumarle los dias
//@param dias Numero de dias a agregar
//@return La fecha agregando los dias
public static java.sql.Date sumarFechasDias(java.sql.Date fch, int dias) {
Calendar cal = new GregorianCalendar();
cal.setTimeInMillis(fch.getTime());
cal.add(Calendar.DATE, dias);
return new java.sql.Date(cal.getTimeInMillis());
}
//Restarle dias a una fecha determinada
//@param fch La fecha
//@param dias Dias a restar
//@return La fecha restando los dias
public static synchronized java.sql.Date restarFechasDias(java.sql.Date fch, int dias) {
Calendar cal = new GregorianCalendar();
cal.setTimeInMillis(fch.getTime());
cal.add(Calendar.DATE, -dias);
return new java.sql.Date(cal.getTimeInMillis());
}
//Diferencias entre dos fechas
//@param fechaInicial La fecha de inicio
//@param fechaFinal La fecha de fin
//@return Retorna el numero de dias entre dos fechas
public static synchronized int diferenciasDeFechas(Date fechaInicial, Date fechaFinal) {
DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM);
String fechaInicioString = df.format(fechaInicial);
try {
fechaInicial = df.parse(fechaInicioString);
} catch (ParseException ex) {
}
String fechaFinalString = df.format(fechaFinal);
try {
fechaFinal = df.parse(fechaFinalString);
} catch (ParseException ex) {
}
long fechaInicialMs = fechaInicial.getTime();
long fechaFinalMs = fechaFinal.getTime();
long diferencia = fechaFinalMs - fechaInicialMs;
double dias = Math.floor(diferencia / (1000 * 60 * 60 * 24));
return ((int) dias);
}
//Devuele un java.util.Date desde un String en formato dd-MM-yyyy
//@param La fecha a convertir a formato date
//@return Retorna la fecha en formato Date
public static synchronized java.util.Date deStringToDate(String fecha) {
SimpleDateFormat formatoDelTexto = new SimpleDateFormat("dd-MM-yyyy");
Date fechaEnviar = null;
try {
fechaEnviar = formatoDelTexto.parse(fecha);
return fechaEnviar;
} catch (ParseException ex) {
ex.printStackTrace();
return null;
}
}
}