代码演示:
//数据库中去的日期
Date s = list.get(0).getSdate();
System.out.println(s);// Tue Apr 28 00:00:00 CST 2015
//手动new
Date d = new Date();
System.out.println(d);// Tue Apr 28 20:53:15 CST 2015
System.out.println(“++” + s.after(d));
//使用Calendar
Calendar calendar = Calendar.getInstance();
Date time = calendar.getTime();
System.out.println(time);// Tue Apr 28 20:55:16 CST 2015
//使用日期格式化
SimpleDateFormat format = new SimpleDateFormat(“yyyy/MM/dd”);
String format2 = format.format(s);
System.out.println(“format2” + format2);// 2015/04/28
//再解析
Date parse = format.parse(format2);
System.out.println(“parse” + parse);// Tue Apr 28 00:00:00 CST 2015
System.out.println(“???” + (s.getTime() >= parse.getTime()));// true
//使用jodatime
DateTime dt = new DateTime();
System.out.println(dt);// 2015-04-28T21:00:28.780+08:00
System.out.println(dt.toDate());// Tue Apr 28 21:00:28 CST 2015
写了2个方法,用于处理:
1.比较2个日期大小(只比较年月日部分) /** * 对2个日期进行比较,无论是从数据库中取出的date,还是new的 * @param d1 * @param d2 * @return */ private String compareDate(Date d1,Date d2){ if(d1==null || d2==null){ return "error"; } long time1 = new LocalDate(d1).toDate().getTime(); long time2 = new LocalDate(d2).toDate().getTime(); if(time1>time2){ return ">"; }else if(time1==time2){ return "=="; }else{ return "<"; } } 2.比较2个日期大小(只比较年月日部分) // 只比较年月日大小 private String compareDate(Date s, Date d) throws ParseException { SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd"); if (format.parse(format.format(s)).getTime() > format.parse( format.format(d)).getTime()) { return ">"; } else if (format.parse(format.format(s)).getTime() == format.parse( format.format(d)).getTime()) { return "=="; } else { return "<"; } }
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/10687.html