- 论坛徽章:
- 0
|
There is a case that if we use String to save Date value and we need to sort by Date, then we need to cast Strng to Date and then compare them. And depending on different format of the string date like mm/dd/yy, or dd/mm/yy, or dd/mm/yyyy, or mm/dd/yyyy. we need to parse all the different format to the same date.
Thanks to the API DateFormat, we can do it very easily
Like if we want to parse mm/dd/yyyy, as we know that this is the date format for english.
The following code can do this job
try {
DateFormat formatter = new SimpleDateFormat("MM/dd/yy",Locale.ENGLISH);
Date date = (Date)formatter.parse("04/29/02");
Date date2 = (Date)formatter.parse("01/02/03");
System.out.println(date.getTime()+"\t"+ date.toString());
System.out.println(date2.getTime()+"\t"+ date2.toString());
long long1 = date.getTime();
long long2=date2.getTime();
System.out.println(new Date(long1) + "\t"+ new Date(long2));
} catch (ParseException e) {
}
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/2956/showart_272683.html |
|