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
|
public class TraiterDate {
/**
* Différents formats de vérification des dates
*/
private static final DateFormat[] formats = {
new SimpleDateFormat("dd/MM/yyyy"),
new SimpleDateFormat("dd MM yyyy"),
new SimpleDateFormat("dd-MM-yyyy"),
new SimpleDateFormat("ddMMyyyy")
};
/**
* Format de sortie pour toutes les dates de nos applications
*/
private static DateFormat STANDARD_DATE_FORMAT = new SimpleDateFormat("dd/MM/yyyy");
/**
* Initialisation des formats de date
*/
static{
for(int i=0; i<formats.length;i++)
formats[i].setLenient(false);
}
/**
* Fonction permettant de transformer une date au format String en date au format date si
* le format de la date passée en paramètre correspond à un des formats que la MGPAT reconnais (table des formats)
* @param date au format String à transformer en date
* @return date ou null si date non reconnue
*/
public static Date stringToDate(String date)
{
Date dDate = null;
for(int i=0;i<formats.length;i++)
{
try
{
dDate = formats[i].parse(date);
return dDate;
}
catch(Exception e){};
}
return null;
}
/**
* Fonction permettant de transformer une date String en date String formatée au standard MGPAT
* @param date au format String à transformer
* @return date au format String transformée en date formatée au format standard de la MGPAT, si pas reconnu return chaine vide
*/
public static String dateCapturedToFormat(String date)
{
Date dDate = stringToDate(date);
return dDate == null ? "" : STANDARD_DATE_FORMAT.format(dDate);
} |
Partager