ADD - date and time generate methods
This commit is contained in:
parent
a5e1d71532
commit
418b65bd77
4
.idea/vcs.xml
generated
4
.idea/vcs.xml
generated
@ -1,4 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings" defaultProject="true" />
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
||||
@ -1,163 +1,68 @@
|
||||
package com.banesco.common.infraestructure.helpers;
|
||||
|
||||
import java.time.DateTimeException;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Clase utilitaria para operaciones relacionadas con fechas y horas.
|
||||
* Proporciona métodos para validar, comparar y calcular períodos entre fechas.
|
||||
* Clase de utilidad para operaciones comunes de fecha y hora.
|
||||
* Utiliza el API moderno de Java (java.time).
|
||||
*/
|
||||
public class DateHelper {
|
||||
public final class DateHelper {
|
||||
|
||||
// Define la zona horaria de Venezuela una sola vez.
|
||||
private static final ZoneId VENEZUELA_ZONE_ID = ZoneId.of("America/Caracas");
|
||||
|
||||
private final Logger logger = Logger.getLogger(DateHelper.class.getName());
|
||||
private final ZoneId currentZone = ZoneId.of("America/Caracas");
|
||||
private final DateTimeFormatter DATE_YYYY_MM_DD = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
||||
private final DateTimeFormatter TIME_HH_MM_SS = DateTimeFormatter.ofPattern("HH:mm:ss");
|
||||
|
||||
private static class SingletonHelper {
|
||||
private static final DateHelper INSTANCE = new DateHelper();
|
||||
}
|
||||
|
||||
// Define los formatos de fecha y hora como constantes para reutilizarlos.
|
||||
private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
||||
private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm:ss");
|
||||
|
||||
/**
|
||||
* Constructo private to no allow other Instances
|
||||
* Constructor privado para prevenir que la clase sea instanciada.
|
||||
*/
|
||||
private DateHelper() {
|
||||
// not public instance
|
||||
// Clase de utilidad, no necesita ser instanciada.
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Instance Singleton
|
||||
* Obtiene la fecha y/o hora actual en la zona horaria de Venezuela.
|
||||
*
|
||||
* @return
|
||||
* @param separate Un flag que indica si se deben devolver la fecha y la hora por separado.
|
||||
* - Si es `false`, devuelve una sola cadena "yyyy-MM-dd HH:mm:ss".
|
||||
* - Si es `true`, devuelve un Map con las llaves "date" y "time".
|
||||
* @return Un objeto (String o Map) con la fecha y hora formateadas.
|
||||
*/
|
||||
public static DateHelper getInstance() {
|
||||
return SingletonHelper.INSTANCE;
|
||||
}
|
||||
public static Object getCurrentVenezuelanDateTime(boolean separate) {
|
||||
// Obtiene la fecha y hora actual en la zona horaria especificada.
|
||||
ZonedDateTime now = ZonedDateTime.now(VENEZUELA_ZONE_ID);
|
||||
|
||||
/**
|
||||
* Permite obtener la Fecha Hora Actual
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public LocalDateTime getCurrentDateTime() {
|
||||
return LocalDateTime.now(currentZone);
|
||||
}
|
||||
|
||||
/**
|
||||
* Permite Obtener la Hora para una FechaHora, en caso de ser null se obtiene la hora actual
|
||||
*
|
||||
* @param now Feha Hora Local
|
||||
* @return
|
||||
*/
|
||||
public String getTime(LocalDateTime now) {
|
||||
if (now == null) {
|
||||
now = LocalDateTime.now(currentZone);
|
||||
}
|
||||
return now.format(TIME_HH_MM_SS);
|
||||
}
|
||||
|
||||
/**
|
||||
* Permite Obtener la Fecha para una FechaHora, en caso de ser null se obtiene la fecha actual
|
||||
*
|
||||
* @param now Feha Hora Local
|
||||
* @return
|
||||
*/
|
||||
public String getDate(LocalDateTime now) {
|
||||
if (now == null) {
|
||||
now = LocalDateTime.now(currentZone);
|
||||
}
|
||||
return now.format(DATE_YYYY_MM_DD);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check is Current Hour is in Hour Range passed by parameters
|
||||
* if Parameters are equal then return false
|
||||
*
|
||||
* @param rangeHourBegin Cadena de la hora de inicio del Rango
|
||||
* @param rangeHourFinish Candena de la hora de fin del Rango
|
||||
* @param hourStrToCheck Hora en cadena para evaluar si se encuentra entre el rango de parametros
|
||||
* @return boolean
|
||||
*/
|
||||
public boolean isHourInRange(String rangeHourBegin, String rangeHourFinish, String hourStrToCheck) {
|
||||
|
||||
return isHourInRange(Integer.parseInt(rangeHourBegin.replace(":", "")),
|
||||
Integer.parseInt(rangeHourFinish.replace(":", "")),
|
||||
hourStrToCheck
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Check is Current Hour is in Hour Range passed by parameters
|
||||
* if Parameters are equal then return false
|
||||
*
|
||||
* @param rangeHourBegin Numero entero de la hora de inicio del Rango
|
||||
* @param rangeHourFinish Numero entero de la hora de fin del Rango
|
||||
* @param hourStrToCheck Hora en cadena para evaluar si se encuentra entre el rango de parametros
|
||||
* @return boolean
|
||||
*/
|
||||
public boolean isHourInRange(int rangeHourBegin, int rangeHourFinish, String hourStrToCheck) {
|
||||
boolean mustValidateService = false;
|
||||
|
||||
if (rangeHourBegin == rangeHourFinish) {
|
||||
return mustValidateService;
|
||||
}
|
||||
|
||||
int currentHour = Integer.parseInt(hourStrToCheck.replace(":", ""));
|
||||
|
||||
|
||||
if (rangeHourBegin > rangeHourFinish) { // hour end is less than hour start, => then validate next day
|
||||
|
||||
if (currentHour <= rangeHourFinish || currentHour >= rangeHourBegin) {
|
||||
mustValidateService = true;
|
||||
}
|
||||
} else if (currentHour >= rangeHourBegin && currentHour <= rangeHourFinish) { // Hour the same day
|
||||
mustValidateService = true;
|
||||
}
|
||||
logger.log(Level.INFO, "La hora (%s => %d) %sesta entre %d y %d ".formatted(
|
||||
hourStrToCheck,
|
||||
currentHour,
|
||||
(mustValidateService ? "" : "NO "),
|
||||
rangeHourBegin,
|
||||
rangeHourFinish));
|
||||
|
||||
return mustValidateService;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calcula el número de días entre dos fechas.
|
||||
*
|
||||
* @param startDate Fecha de inicio en formato ISO (yyyy-MM-dd)
|
||||
* @param endDate Fecha de fin en formato ISO (yyyy-MM-dd)
|
||||
* @return El número de días entre las fechas o -1 si ocurre un error al
|
||||
* procesar las fechas
|
||||
*/
|
||||
public long daysBetweenTwoDates(String startDate, String endDate) {
|
||||
try {
|
||||
return ChronoUnit.DAYS.between(LocalDate.parse(startDate), LocalDate.parse(endDate));
|
||||
} catch (DateTimeException dte) {
|
||||
return -1L;
|
||||
if (separate) {
|
||||
// Devuelve las partes en un mapa
|
||||
Map<String, String> dateTimeParts = new HashMap<>();
|
||||
dateTimeParts.put("date", now.format(DATE_FORMATTER));
|
||||
dateTimeParts.put("time", now.format(TIME_FORMATTER));
|
||||
return dateTimeParts;
|
||||
} else {
|
||||
// Devuelve una sola cadena de texto combinada
|
||||
return now.format(DATE_FORMATTER) + " " + now.format(TIME_FORMATTER);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifica si la fecha de inicio es anterior o igual a la fecha de fin.
|
||||
*
|
||||
* @param startDate Fecha de inicio en formato ISO (yyyy-MM-dd)
|
||||
* @param endDate Fecha de fin en formato ISO (yyyy-MM-dd)
|
||||
* @return true si startDate es anterior o igual a endDate, false en caso
|
||||
* contrario
|
||||
* Función de conveniencia para obtener solo la fecha actual formateada.
|
||||
* @return La fecha actual como un String "yyyy-MM-dd".
|
||||
*/
|
||||
public boolean isStartDateBeforeEndDate(String startDate, String endDate) {
|
||||
return LocalDate.parse(startDate).isBefore(LocalDate.parse(endDate))
|
||||
|| LocalDate.parse(startDate).isEqual(LocalDate.parse(endDate));
|
||||
public static String getCurrentVenezuelanDate() {
|
||||
return ZonedDateTime.now(VENEZUELA_ZONE_ID).format(DATE_FORMATTER);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Función de conveniencia para obtener solo la hora actual formateada.
|
||||
* @return La hora actual como un String "HH:mm:ss".
|
||||
*/
|
||||
public static String getCurrentVenezuelanTime() {
|
||||
return ZonedDateTime.now(VENEZUELA_ZONE_ID).format(TIME_FORMATTER);
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user