Bonjour,
J'ai une méthode qui renvoie la date du jour au format jj/mm/aaaa ou mm/aaaa. Au passage si qq à une meilleur implémentation pour cette méthode je suis preneur.
J'aimerai que cette méthode me retourne une exception si le format passer en paramètre n'est ni jj/mm/aaaa ni mm/aaaa.
Je ne sais pas qu'elle est la bonne implémentation.
Merci pour votre aide.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 /** * renvoie la date du jour au format jj/mm/yyyy * * @param format * format * * @return Date du jour au format jj/mm/yyyy ou mm/aaaa */ public static String dateDuJour(String format) { Date date = new Date(); Calendar calendar = new GregorianCalendar(); calendar.setTime(date); String jourEnCours = (new Integer(calendar.get(Calendar.DAY_OF_MONTH))).toString(); String moisEnCours = (new Integer((calendar.get(Calendar.MONTH) + 1))).toString(); if (jourEnCours.length() == 1){ jourEnCours = "0" + jourEnCours; } if (moisEnCours.length() == 1){ moisEnCours = "0" + moisEnCours; } String dateDuJour = null; String anneeEnCours = (new Integer(calendar.get(Calendar.YEAR))).toString(); if (format.compareTo(FORMAT_JOUR_MOIS_ANNEE) == 0){ dateDuJour = jourEnCours + "/" + moisEnCours + "/" + anneeEnCours; } if (format.compareTo(FORMAT_MOIS_ANNEE) == 0) { dateDuJour = moisEnCours + "/" + anneeEnCours; } return dateDuJour; }
Partager