JAVA中如何得到两个日期相减的月数???
我用如下函数得到的月数有问题:
public int getMonthNum(Date date1,Date date2) {
Calendar cal1=Calendar.getInstance();
cal1.setTime(date1);
Calendar cal2=Calendar.getInstance();
cal2.setTime(date2);
return (cal2.get(1)-cal1.get(1))*12+(cal2.get(2)-cal1.get(2));
}
例如用2012-7-30减去2012-8-2 得到的月数为1;
用2012-7-31进去2012-7-1得到的月数为0
该怎么写才能精确的计算两个日期相减的月数呢???????????
我用如下函数得到的月数有问题:
public int getMonthNum(Date date1,Date date2) {
Calendar cal1=Calendar.getInstance();
cal1.setTime(date1);
Calendar cal2=Calendar.getInstance();
cal2.setTime(date2);
return (cal2.get(1)-cal1.get(1))*12+(cal2.get(2)-cal1.get(2));
}
例如用2012-7-30减去2012-8-2 得到的月数为1;
用2012-7-31进去2012-7-1得到的月数为0
该怎么写才能精确的计算两个日期相减的月数呢???????????
import java.text.SimpleDateFormat;
import java.util.Calendar;public class MonthTest{ public static void main(String[] args) throws ParseException {
String d1 = "2012-01-12";
String d2 = "2010-02-11";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.setTime(sdf.parse(d1));
int year1 = c.get(Calendar.YEAR);
int month1 = c.get(Calendar.MONTH);
c.setTime(sdf.parse(d2));
int year2 = c.get(Calendar.YEAR);
int month2 = c.get(Calendar.MONTH);
int result;
if(year1 == year2) {
result = month1 - month2;
} else {
result = 12*(year1 - year2) + month1 - month2;
}
System.out.println(result);
}
}
写的一个测试程序,楼主可以改改,更完善下,比如判断哪个日期在前,哪个日期在后。
把两个日期都化成秒,然后再相减~~~
JAVA中以某个时刻为0秒,具体可以去查API!
其实日期就是以秒储存的,显示时再转化过来。
代码蛮简单呀~马上吃饭了~要不等会贴代码?
public int getMonthNum(Date date1,Date date2) {
Calendar cal1=Calendar.getInstance();
cal1.setTime(date1);
Calendar cal2=Calendar.getInstance();
cal2.setTime(date2);
long millis1=cal1.getTimeInMillis();
long millis2=cal2.getTimeInMillis();
long millis;
(millis1>millis2)?(millis=millis1-millis2):(millis=millis2-millis1);//java能这么写吗?忘记了!反正是判断谁大谁小,不能这样写就自己改下吧~~
Calendar cal=Calendar.getInstance();
cal.setTimeInMillis(millis);
return (cal.get(1)*12+cal.get(2));
}cal是两日期相差秒数所产生的日期值,若两日期差29天,cal就是0-1-29;如果相差是49天,cal就是0-2-18;所以cal的月份值就是相差月份值,我记得get(2)返回的值是从0开始计算的,即1月是0,,1是11。如果不是的话自己减一,上面代码我没测试过,出差电脑没装环境,见谅!大概就是这样了!
我也来个比较笨的方法,年月日都可以算
import java.text.SimpleDateFormat;
import java.util.Calendar;public class Test { public static void main(String[] args) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
long time1 = sdf.parse("2012-01-12").getTime();
long time2 = sdf.parse("2010-02-11").getTime();
System.out.println(getFieldDifference(time1, time2, Calendar.YEAR));
System.out.println(getFieldDifference(time1, time2, Calendar.MONTH));
System.out.println(getFieldDifference(time1, time2, Calendar.DATE));
} /**
* 区分两个日期之间指定字段的差值
*
* @param time1
* 开始时间
* @param time2
* 结束时间
* @param field
* 要比较的字段(年,月,日,...)
* @return 如果time1>time2就反回一个正的差值,如果time1<time2则返回一个负的差值,如果相等,返回0
*/
public static int getFieldDifference(long time1, long time2, int field) {
if (time1 == time2) {
return 0;
} else if (time1 > time2) {
return -getFieldDifference(time2, time1, field);
}
Calendar cal1 = Calendar.getInstance();
cal1.setLenient(false);
cal1.setTimeInMillis(time1);
Calendar cal2 = Calendar.getInstance();
cal2.setLenient(false);
cal2.setTimeInMillis(time2);
for (int x = 0; x < Calendar.FIELD_COUNT; x++) {
if (x > field) {
cal1.clear(x);
cal2.clear(x);
}
}
time1 = cal1.getTimeInMillis();
time2 = cal2.getTimeInMillis(); long ms = 0;
int min = 0, max = 1; while (true) {
cal1.setTimeInMillis(time1);
cal1.add(field, max);
ms = cal1.getTimeInMillis();
if (ms == time2) {
min = max;
break;
} else if (ms > time2) {
break;
} else {
max <<= 1;
}
} while (max > min) {
cal1.setTimeInMillis(time1);
int t = (min + max) >>> 1;
cal1.add(field, t);
ms = cal1.getTimeInMillis();
if (ms == time2) {
min = t;
break;
} else if (ms > time2) {
max = t;
} else {
min = t;
}
}
return -min;
}}
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class MonthQuestion {
public static void main(String[] args) {
String s1=new String("1987-4");
//String转换为Date类型
SimpleDateFormat sFormat=new SimpleDateFormat("yyyy-MM");
try {
Date date1=sFormat.parse(s1);
String s2=new String("1978-2");
Date date2=sFormat.parse(s2);
System.out.println(s1+"与"+s2+"相差"+getmonth(date1, date2)+"个月");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
};
}
public static int getmonth(Date date1,Date date2){
Calendar calendar=new GregorianCalendar();
calendar.setTime(date1);
Calendar scalendar=new GregorianCalendar();
scalendar.setTime(date2);
int year1=calendar.get(Calendar.YEAR);
int year2=scalendar.get(Calendar.YEAR);
int month1=calendar.get(Calendar.MONTH);
int month2=scalendar.get(Calendar.MONTH);
/*System.out.println(year1);
System.out.println(year2);
System.out.println(month1);
System.out.println(month2);*/
int count=0;
if(year1==year2&&month1>month2){
count= month1-month2;
}
else if(year1==year2&&month1<month2){
count= month2-month1;
}
else if(year1 >year2&&month1>month2){
count= (year1-year2)*12+(month1-month2);
}
else if(year1>year2&&month1<month2){
count= (year1-year1)*12+(month2-month1);
}
else if(year1<year2&&month1 >month2){
count=(year2-year1 )*12+month1-month2;
}
else if(year1<year2&&month1<month2){
count= (year2-year1)*12+month2-month1;
}
return count;
}
}
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class MonthQuestion {
public static void main(String[] args) {
String s1=new String("1987-4");
//String转换为Date类型
SimpleDateFormat sFormat=new SimpleDateFormat("yyyy-MM");
try {
Date date1=sFormat.parse(s1);
String s2=new String("1978-2");
Date date2=sFormat.parse(s2);
System.out.println(s1+"与"+s2+"相差"+getmonth(date1, date2)+"个月");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
};
}
public static int getmonth(Date date1,Date date2){
Calendar calendar=new GregorianCalendar();
calendar.setTime(date1);
Calendar scalendar=new GregorianCalendar();
scalendar.setTime(date2);
int year1=calendar.get(Calendar.YEAR);
int year2=scalendar.get(Calendar.YEAR);
int month1=calendar.get(Calendar.MONTH);
int month2=scalendar.get(Calendar.MONTH);
/*System.out.println(year1);
System.out.println(year2);
System.out.println(month1);
System.out.println(month2);*/
int count=0;
if(year1==year2&&month1>month2){
count= month1-month2;
}
else if(year1==year2&&month1<month2){
count= month2-month1;
}
else if(year1 >year2&&month1>month2){
count= (year1-year2)*12+(month1-month2);
}
else if(year1>year2&&month1<month2){
count= (year1-year1)*12+(month2-month1);
}
else if(year1<year2&&month1 >month2){
count=(year2-year1 )*12+month1-month2;
}
else if(year1<year2&&month1<month2){
count= (year2-year1)*12+month2-month1;
}
return count;
}
}