Created
December 7, 2018 06:16
-
-
Save era5mx/97a980722ccd503ac5631c5b409519f5 to your computer and use it in GitHub Desktop.
Utileria de fecha con Locale configurable en properties
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
App.Env.Lang=es-MX |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.text.ParseException; | |
import java.text.SimpleDateFormat; | |
import java.util.Calendar; | |
import java.util.Date; | |
import java.util.Locale; | |
import org.apache.commons.lang3.StringUtils; | |
import org.apache.log4j.Logger; | |
/** | |
* The Class DateUtils | |
* @version 1.0 | |
*/ | |
public class DateUtils { | |
/** Constant EJECUTANDO_GET_DATE_STR. */ | |
private static final String EJECUTANDO_GET_DATE_STR = "Ejecutando getDateStr("; | |
/** The Constant LOGGER. */ | |
private static final Logger LOGGER = Logger.getLogger(DateUtils.class); | |
/** The Constant TIME_FORMAT HHmmss. */ | |
public static final String TIME_FORMAT_H_HMMSS = "HHmmss"; | |
/** The Constant DATE_FORMAT yyMMdd. */ | |
public static final String DATE_FORMAT_YY_M_MDD = "yyMMdd"; | |
/** The Constant DATE_FORMAT dd/MM/yyyy. */ | |
public static final String DATE_FORMAT_DD_MM_YYYY = "dd/MM/yyyy"; | |
/** Constant DATE_FORMAT dd-MMM-yy */ | |
public static final String DATE_FORMAT_DD_MMM_YY = "dd-MMM-yy"; | |
/** The Constant DATE_FORMAT yyyy-MM-dd. */ | |
public static final String DATE_FORMAT_YYYY_MM_DD = "yyyy-MM-dd"; | |
/** The Constant DATE_FORMAT yyyy-MM-dd hh:mm:ss. */ | |
public static final String DATE_FORMAT_YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd hh:mm:ss"; | |
/** The Constant DATE_FORMAT yyyy-MM-dd hh:mm:ss a. */ | |
public static final String DATE_FORMAT_YYYY_MM_DD_HH_MM_SS_A = "yyyy-MM-dd hh:mm:ss a"; | |
/** | |
* Constructor privado para ocultar el constructor predeterminado. | |
*/ | |
private DateUtils() { | |
throw new IllegalAccessError("Utility class"); | |
} | |
/** | |
* Gets the date str. | |
* | |
* @param fecha the fecha | |
* @param formato the formato | |
* @return the date str | |
*/ | |
public static String getDateStr(Date fecha, String formato) { | |
LOGGER.debug(EJECUTANDO_GET_DATE_STR + fecha + ")"); | |
String langTag = I18n.COMMON.getString("App.Env.Lang"); | |
return DateUtils.getDateStr(fecha, formato, | |
StringUtils.isNotBlank(langTag)?Locale.forLanguageTag(langTag):Locale.getDefault()); | |
} | |
/** | |
* Gets the date str. | |
* | |
* @param fecha the fecha | |
* @param formato the formato | |
* @param locale the locale | |
* @return the date str | |
*/ | |
public static String getDateStr(Date fecha, String formato, Locale locale) { | |
LOGGER.debug(EJECUTANDO_GET_DATE_STR + fecha + ")"); | |
SimpleDateFormat dateFormat = new SimpleDateFormat( | |
StringUtils.isBlank(formato) ? DATE_FORMAT_DD_MM_YYYY : StringUtils.trim(formato), locale); | |
return dateFormat.format(fecha!=null?fecha.getTime():StringUtils.EMPTY); | |
} | |
/** | |
* Gets the date str. | |
* | |
* @param fecha the fecha | |
* @param formato the formato | |
* @param locale the locale | |
* @return the date str | |
* @throws ParseException the parse exception | |
*/ | |
public static Date getDateStr(String fecha, String formato, Locale locale) throws ParseException { | |
LOGGER.debug(EJECUTANDO_GET_DATE_STR + fecha + ")"); | |
SimpleDateFormat dateFormat = new SimpleDateFormat( | |
StringUtils.isBlank(formato) ? DATE_FORMAT_DD_MM_YYYY : StringUtils.trim(formato), locale); | |
return dateFormat.parse(StringUtils.isNotBlank(fecha)?fecha:StringUtils.EMPTY); | |
} | |
/** | |
* * | |
* Obtener el nombre de mes correspondiente a su indice. | |
* | |
* @param month numero de mes | |
* @return nombre de mes | |
*/ | |
public static String getNombreMes(int month) { | |
String mes = ""; | |
switch(month) { | |
case 1: mes = "Enero"; break; | |
case 2: mes = "Febrero"; break; | |
case 3: mes = "Marzo"; break; | |
case 4: mes = "Abril"; break; | |
case 5: mes = "Mayo"; break; | |
case 6: mes = "Junio"; break; | |
case 7: mes = "Julio"; break; | |
case 8: mes = "Agosto"; break; | |
case 9: mes = "Septiembre"; break; | |
case 10: mes = "Octubre"; break; | |
case 11: mes = "Noviembre"; break; | |
case 12: mes = "Diciembre"; break; | |
default: mes = "Enero"; break; | |
} | |
return mes; | |
} | |
/*** | |
* Obtener el nombre de mes en base a la fecha indicada | |
* @param fecha fecha de la que se extrae el mes | |
* @return el nombre de mes | |
*/ | |
public static String getNombreMes(Date fecha) { | |
Calendar c = Calendar.getInstance(); | |
c.setTime(fecha); | |
int mes = c.get(Calendar.MONTH) + 1; | |
return getNombreMes(mes); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.MissingResourceException; | |
import java.util.ResourceBundle; | |
import org.apache.log4j.Logger; | |
/** | |
* Ayudante del paquete de recursos. | |
* | |
* <p> | |
* Cada módulo tiene su propio archivo de paquete de recursos para cadenas i18n. Siempre lea | |
* la configuración regional predeterminada <code>Locale.getDefault()</code>. | |
* | |
* <p> | |
* <pre> | |
* I18n.MODULE_NAME.getString("stringKey"); | |
* </pre> | |
* | |
* @author Manuel Arriola | |
* @version 1.0 | |
* @see ResourceBundle | |
*/ | |
public enum I18n { | |
/** Modulo Common. */ | |
COMMON("common"); | |
/** El paquete de recursos. */ | |
private final ResourceBundle resourceBundle; | |
/** La constante DEFAULT_LOCATION. */ | |
private static final String DEFAULT_LOCATION = "com.sky.aclaraciones.resources.i18n."; | |
/** La constante LOGGER. */ | |
private static final Logger LOGGER = Logger.getLogger(I18n.class); | |
/** | |
* Instancia un nuevo i 18 n. | |
* | |
* @param bundleFile el archivo del lote | |
*/ | |
I18n(String bundleFile) { | |
resourceBundle = ResourceBundle.getBundle(DEFAULT_LOCATION + bundleFile); | |
} | |
/** | |
* Obtiene una cadena para la clave dada desde el paquete de recursos. | |
* | |
* @param key la clave para la cadena deseada | |
* @return la cadena para la clave dada | |
*/ | |
public String getString(String key) { | |
try { | |
return resourceBundle.getString(key); | |
} catch (MissingResourceException ex) { | |
LOGGER.error(ex.getMessage(), ex); | |
return "err#"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment