Java 计算两个日期相差月数

package com.myjava;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar; public class MyCalendar { /**
* @param args
* @throws ParseException
*/
public static void main(String[] args) throws ParseException {
System.out.println(MyCalendar.getMonthSpace("1982-12-8", "2012-12-12")); } /**
*
* @param date1 <String>
* @param date2 <String>
* @return int
* @throws ParseException
*/
public static int getMonthSpace(String date1, String date2)
throws ParseException { int result = 0; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Calendar c1 = Calendar.getInstance();
Calendar c2 = Calendar.getInstance(); c1.setTime(sdf.parse(date1));
c2.setTime(sdf.parse(date2)); result = c2.get(Calendar.MONDAY) - c1.get(Calendar.MONTH); return result == 0 ? 1 : Math.abs(result); } }
上一篇:Hibernate 关联关系映射实例


下一篇:Windows及Android倍速播放视频软件下载