lunes, 29 de noviembre de 2021

Java: utilizar el valor de una property en una clase.

En .yml:

application.yml
sirec:
    kill-list-enabled: ${KILL_LIST_ENABLED:false}

O en .properties:

application.properties
sirec.kill-list-enabled: ${KILL_LIST_ENABLED:false}

Y en Java:

ApiTareasService.java
@Value("#{new Boolean('${sirec.kill-list-enabled:false}')}")
private Boolean killListEnabled;

if (killListEnabled) {
    ...
}

Java: sumar días o meses a una fecha en formato String

public static final String FECHA_FORMATO_ENTRADA = "yyyy-MM-dd HH:mm:ss.S";
public static final String FECHA_FORMATO_SALIDA = "dd/MM/yyyy";

SimpleDateFormat formatIn = new SimpleDateFormat(Constants.FECHA_FORMATO_ENTRADA);
SimpleDateFormat formatOut = new SimpleDateFormat(Constants.FECHA_FORMATO_SALIDA);

String tmpFecha = null;

try {
    Date dateSel = formatIn.parse(valoracionesBienInner.getFecha());
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(dateSel);
    calendar.add(Calendar.MONTH, Integer.valueOf(valoracionesBienInner.getPeriodoValidez()));
    Date salida = calendar.getTime();

    tmpFecha = formatOut.format(salida);

} catch (ParseException e) {
    throw new SirecApplicationException(0, "Error parseo fecha validez en valoraciones", e);
}

aElementoListaValoraciones.setFechaValidez(getNullHandlingValue(tmpFecha));

public static String getNullHandlingValue(String valor) {
    return valor == null ? "" : valor;
}