日期:2014-05-20 浏览次数:21032 次
int m = date1.getMonth()-date1.getMonth();
------解决方案--------------------
再加上年
int m = (date1.getYear()-date2.getYear())*12+date1.getMonth()-date1.getMonth();
------解决方案--------------------
不明白你这个精确的意思!!!!给个例子?
------解决方案--------------------
加上年
------解决方案--------------------
import java.text.ParseException; 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); } }
------解决方案--------------------
顶,楼上回答正确
------解决方案--------------------
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)); }
------解决方案--------------------
上面12月是11打漏了。。。
------解决方案--------------------
keithcai兄弟讲的没太明白
我也来个比较笨的方法,年月日都可以算
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; } }