标签:
满足的要求是Timestamp的类型,计算时间差,若满不足一个月按一个月计算。用户5月27订购的,到6月27日到期,即使是5月有31天。
package file;
import java.sql.Timestamp;
public class timeUtil {
public static void main(String[] args) {
String time1="2000-02-29 01:12:12";
String time2="2000-03-30 12:12:12";
Timestamp a=Timestamp.valueOf(time1);
Timestamp b=Timestamp.valueOf(time2);
System.out.println(a+"||"+b);
int y1=a.getYear()+1900; int m1=a.getMonth()+1; int d1=a.getDate();
int y2=b.getYear()+1900; int m2=b.getMonth()+1; int d2=b.getDate();
System.out.println(y1+"-"+m1+"-"+d1);
System.out.println(y2+"-"+m2+"-"+d2);
int month=(y2-y1)*12+m2-m1;
if(m1==2&&m2!=2){
if(d2>d1){
month=month+1;
}
}if(m1!=2&&m2==2){
if(d2>d1){
month=month+1;
}
}else if((m2==1||m2==3||m2==5||m2==7||m2==8||m2==10||m2==12)&&(m1==2||m1==4||m1==6||m1==9||m1==11)){
//计算闰年29天
if(m1==2&&((y1%4==0&&y1%100!=0)||y1%400==0)){
if (d2==29||d2==30||d2==31||d1==29){
}else{
month=month+1;
}
}else{
if (d2==29||d2==30||d2==31||d1==28){
}else{
month=month+1;
}
}
}else if((m1==1||m1==3||m1==5||m1==7||m1==8||m1==10||m1==12)&&(m2==4||m2==6||m2==9||m2==11)){
if(d2>d1){
month=month+1;
}
}
System.out.println(month);
}
}
标签:
原文地址:http://www.cnblogs.com/JAYIT/p/5169667.html