Conversion variable de type long en Date
Bonjour à tous
je suis actuellement coincé sur un petit problème qui doit être facilement résolu mais je tourne un peu en rond.
Le but du prog est de transformer une variable de type long (ex : 20111108 => la date d'auj sous le format yyyyMMjj) en une variable de type Date que j'insérerais dans une base de données (sous le format Date)
Code:
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
|
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class App
{
public static void main( String[] args )
{
Long theD = new Long(20111108);
String creationDate = theD.toString();
int yyyy = Integer.parseInt(creationDate.substring(0,4));
int MM = Integer.parseInt(creationDate.substring(4,6));
int jj = Integer.parseInt(creationDate.substring(6,8));
System.out.println("Creation date : " + creationDate);
System.out.println("yyyy : " + yyyy);
System.out.println("MM : " + MM);
System.out.println("jj : " + jj);
String dateString = "" + yyyy + MM + jj;
Date theDate = new Date(yyyy, MM-1, jj);
DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
String the = dateFormat.format(theDate);
System.out.println(the);
String dateString2 = "" + jj + MM + yyyy ;
Date theDate2 = new Date(jj, MM-1, yyyy);
DateFormat dateFormat2 = new SimpleDateFormat("dd-MMM-yyyy");
String the2 = dateFormat2.format(theDate2);
System.out.println(the2);
}
} |
et voilà l'output ... la décomposition du long en String est correcte mais je ne sais pas si c'est effectivement nécessaire.
Code:
1 2 3 4 5 6 7
|
Creation date : 20111108
yyyy : 2011
MM : 11
jj : 8
08-Nov-3911
04-May-1914 |
une idée pour m'aider ??
merci