日期:2014-05-16 浏览次数:20352 次
// format yyyy-MM-dd HH:mm:ss/yyyyMMddHHmmss/ yyyyMMdd / yyyy/MM/dd DateFormat dateFormat = null; String format = "yyyy/MM/dd"; String stime = null; Date dstime = null; System.out.println("Date.parse(\"Jul 8, 2005\")" + " " +Date.parse("Jul 8, 2005")); format = "yyyy/MM/dd"; stime = "2005/07/08"; dateFormat = new SimpleDateFormat(format, Locale.CHINA); dstime = dateFormat.parse(stime); System.out.println(format + " " + stime + " " +dstime.getTime()); format = "yyyy-MM-dd"; stime = "2005-07-08"; dateFormat = new SimpleDateFormat(format, Locale.CHINA); dstime = dateFormat.parse(stime); System.out.println(format + " " + stime + " " +dstime.getTime()); format = "yyyyMMdd"; stime = "20050708"; dateFormat = new SimpleDateFormat(format, Locale.CHINA); dstime = dateFormat.parse(stime); System.out.println(format + " " + stime + " " +dstime.getTime());
Date.parse("Jul 8, 2005") 1120752000000 yyyy/MM/dd 2005/07/08 1120752000000 yyyy-MM-dd 2005-07-08 1120752000000 yyyyMMdd 20050708 1120752000000 结论:时间都一致。
<html> <body> <script type="text/javascript"> var d = Date.parse("Jul 8, 2005"); document.write(d); //1120752000000 d = Date.parse("2005/07/08"); document.write('---------' + d); //1120752000000 d = Date.parse("2005-07-08"); document.write('---------' + d);//1120780800000 d = Date.parse("20050708"); document.write('---------' + d);//NaN </script> </body> </html>
1120752000000---------1120752000000---------1120780800000---------NaN 结论:Jul 8, 2005 和 2005/07/08 时间一样,其他的不一样。
function compareDate(d1, d2) { // 时间比较的方法,如果d1时间比d2时间大,则返回true return Date.parse(d1.replace(/-/g, "/")) >= Date.parse(d2.replace(/-/g, "/")) }