java Calendar 日期的比较和计算
public static void main(String[] args) throws Exception {
String startTime = "2012-12-12 12:45:45";
String endTime = "2012-04-12 12:45:40";
String SYSendTime = "2012-11-12 12:45:40";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date startDate = sdf.parse(startTime);
Date endDate = sdf.parse(endTime);
Calendar start = Calendar.getInstance();
Calendar end = Calendar.getInstance();
start.setTime(startDate);
end.setTime(endDate);
if(start.before(end)){
System.err.println("开始时间小于结束时间");
}else if(start.after(end)){
System.err.println("开始时间大于结束时间");
}else if(start.equals(end)){
System.err.println("开始时间等于结束时间");
}
}
public static void method(){
Date date = new Date();
Calendar c = Calendar.getInstance();
c.setTime(date);
Calendar cm = Calendar.getInstance();
Date d = cm.getTime();
System.err.println("Calendar获得时间:" + d);
System.err.println("new Date创建的时间:" + date);
long dl = c.getTimeInMillis();
long ddate = cm.getTimeInMillis();
System.err.println("毫秒数:" + dl);
System.err.println("毫秒数:" + ddate);
long sv = 123456;
Calendar sc = Calendar.getInstance();
sc.setTimeInMillis(sv);
SimpleDateFormat ss = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String st = ss.format(sc.getTime());
System.err.println(st);
int year = c.get(Calendar.YEAR);
System.err.println(year);
c.set(Calendar.YEAR, 2);
int y = c.get(Calendar.YEAR);
System.err.println(y);
try{
String startTime = "2012-12-12 12:45:39";
String endTime = "2012-12-12 12:45:40";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date startDate = sdf.parse(startTime);
Date endDate = sdf.parse(endTime);
Calendar start = Calendar.getInstance();
Calendar end = Calendar.getInstance();
start.setTime(startDate);
end.setTime(endDate);
if(start.before(end)){
System.err.println("开始时间小于结束时间");
}else if(start.after(end)){
System.err.println("开始时间大于结束时间");
}else if(start.equals(end)){
System.err.println("开始时间等于结束时间");
}
int count = start.compareTo(end);
System.err.println(count);
start.add(Calendar.YEAR, -3);
System.err.println("原来的时间:" + startTime);
System.err.println("add后的时间:" + sdf.format(start.getTime()));
System.err.println(start.toString());
}catch(Exception e){}
}
public static void param(){
Date date = new Date();
Calendar c = Calendar.getInstance();
c.setTime(date);
int year = c.get(Calendar.YEAR);
int mounth = c.get(Calendar.MONTH) + 1;
int day = c.get(Calendar.DATE);
int hour = c.get(Calendar.HOUR);
int HOUR_OF_DAY = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
int second = c.get(Calendar.SECOND);
System.err.println(year + "-" + mounth + "-" + day + " " + hour + ":" + minute + ":" + second);
int WEEK_OF_YEAR = c.get(Calendar.WEEK_OF_YEAR);
int WEEK_OF_MONTH = c.get(Calendar.WEEK_OF_MONTH);
int DAY_OF_YEAR = c.get(Calendar.DAY_OF_YEAR);
int DAY_OF_MONTH = c.get(Calendar.DAY_OF_MONTH);
int DAY_OF_WEEK = c.get(Calendar.DAY_OF_WEEK);
int DAY_OF_WEEK_IN_MONTH = c.get(Calendar.DAY_OF_WEEK_IN_MONTH);
try{
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date ampm = format.parse("2012-12-15 21:59:59");
Calendar cc = Calendar.getInstance();
cc.setTime(ampm);
int AM_PM = cc.get(Calendar.AM_PM);
}catch(Exception e){}
}
}