Since you have mentioned that you have the long myFileTime
around, you should be able to use:
Date updatedate=new Date(myFileTime);
To convert back to your date. If only your String
is available, you should modify your function to this:
public Date stringToDate(String s){
Calendar c = Calendar.getInstance();
c.set(Calendar.DAY_OF_MONTH, Integer.parseInt(s.substring(0, 2)));
c.set(Calendar.MONTH, Integer.parseInt(s.substring(2, 4))-1 );
c.set(Calendar.YEAR, Integer.parseInt(s.substring(4, 8)));
return c.getTime();
}
Note the changed indexes.
In Java SE, you should be able to use the following line, instead of setting each fields separately:
c.setTimeInMillis(Long.parseLong(s));
Since in s
you have the dateField.getDate().getTime()
that is equal to myFileTime
, the number of seconds starting with January 1, 1970, based on your provided code.
Your stringToDate
should work only if your string will have the following format: ddMMyyyy
. Also note that in this case you should use a SimpleDateFormat to parse, something like:
Date updatedate = new java.text.SimpleDateFormat("ddMMyyyy HH:mm:ss").parse(date);