日期:2014-05-17 浏览次数:20879 次
String time="2012-05-12,18:00"; DateFormat f= new SimpleDateFormat("yyyy-MM-dd HH:mm"); Date date = f.parse(time); Date date2 = f.parse("2012-05-13 8:01"); int i = date.compareTo(date2); /*the value 0 if the argument Date is equal to this Date; a value less than 0 if this Date is before the Date argument; and a value greater than 0 if this Date is after the Date argument.*/ //date.before(date2); //date.after(date2);
------解决方案--------------------
package leedp.login;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateCompare {
public static void main(String[] args) throws ParseException {
/*
* the value 0 if the argument Date is equal to this Date; a value less
* than 0 if this Date is before the Date argument; and a value greater
* than 0 if this Date is after the Date argument.
*/
String time = "2012-05-12,18:00";
time = time.replace(",", " ");
DateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date date1 = f.parse(time);
Date date2 = f.parse("2012-05-13 8:01");
int i = date1.compareTo(date2);
System.out.println(i);
Calendar c = Calendar.getInstance();
Date date = null;
try {
date = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse("2012-05-12 18:00");
} catch (ParseException e) {
e.printStackTrace();
}
c.setTime(date);
int day = c.get(Calendar.DATE);
c.set(Calendar.DATE, day - 1);
String dayBefore = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(c
.getTime());
System.out.println(dayBefore);
}
}
------解决方案--------------------