源代码参照如下
package xxx; import java.text.*; import java.util.*; public class DifferDate { private String differMsg = ""; /** コンストラクタ * * @param startDateStr 開始日付 【YYYY/MM/DD】OR【YYYY/MM/DD hh:mm:ss】 * @param endDateStr 終了日付 【YYYY/MM/DD】OR【YYYY/MM/DD hh:mm:ss】 * * @throws ParseException 日付フォーマットが不正な場合 * @author 曹 */ public DifferDate(String startDateStr, String endDateStr) throws ParseException { int difMonth = 0; int differDays = 0; if (this.checkDate(startDateStr, endDateStr)) { difMonth = this.differenceMonths(startDateStr, endDateStr); differDays = this.differenceDaysInOneMonth(startDateStr, endDateStr); differMsg = ""; } else { difMonth = this.differenceMonths(endDateStr, startDateStr); differDays = this.differenceDaysInOneMonth(endDateStr, startDateStr); differMsg = "-"; } int differYears = difMonth / 12; int differMonths = difMonth - differYears * 12; differMsg += differYears + "年" + differMonths + "ヵ月" + differDays + "日"; } // public String getDifferMsg() { return differMsg; } /** 日付のチェック * * @param startDateStr 開始日付 【YYYY/MM/DD】OR【YYYY/MM/DD hh:mm:ss】 * @param endDateStr 終了日付 【YYYY/MM/DD】OR【YYYY/MM/DD hh:mm:ss】 * * @return 開始日付 <= 終了日付 は True なければ False * * @throws ParseException 日付フォーマットが不正な場合 * @author 曹 */ private boolean checkDate(String startDateStr, String endDateStr) throws ParseException { Date startDate = DateFormat.getDateInstance().parse(startDateStr); Date endDate = DateFormat.getDateInstance().parse(endDateStr); Calendar startCal = Calendar.getInstance(); Calendar endCal = Calendar.getInstance(); startCal.setTime(startDate); endCal.setTime(endDate); return !startCal.after(endCal); } /** 2つの日付の一ヶ月以内日数の差を求めます。 * * @param startDateStr 開始日付 【YYYY/MM/DD】OR【YYYY/MM/DD hh:mm:ss】 * @param endDateStr 終了日付 【YYYY/MM/DD】OR【YYYY/MM/DD hh:mm:ss】 * * @return 2つの日付の一ヶ月以内日数の差を整数で返します。 * * @throws ParseException 日付フォーマットが不正な場合 * @author 曹 */ private int differenceDaysInOneMonth(String startDateStr, String endDateStr) throws ParseException { int count = 0; Date startDate = DateFormat.getDateInstance().parse(startDateStr); Date endDate = DateFormat.getDateInstance().parse(endDateStr); Calendar startCal = Calendar.getInstance(); Calendar endCal = Calendar.getInstance(); startCal.setTime(startDate); endCal.setTime(endDate); int index = 1; //開始日付 <= 終了日付 while (!startCal.after(endCal)) { startCal.clear(); startCal.setTime(startDate); startCal.add(Calendar.MONTH, index); // 開始日付 > 終了日付 if (startCal.after(endCal)) { startCal.clear(); startCal.setTime(startDate); startCal.add(Calendar.MONTH, index - 1); String lastDate = startCal.get(Calendar.YEAR) + "/" + (startCal.get(Calendar.MONTH) + 1) + "/" + startCal.get(Calendar.DATE); System.out.println("lastDate01="+lastDate); count = (int) this.differenceDays(lastDate, endDateStr); break; } // 開始日付 == 終了日付 else if (!startCal.before(endCal)) { String lastDate = startCal.get(Calendar.YEAR) + "/" + (startCal.get(Calendar.MONTH) + 1) + "/" + startCal.get(Calendar.DATE); System.out.println("lastDate02="+lastDate); count = (int) this.differenceDays(lastDate, endDateStr); break; } index++; } return count; } /** 2つの日付の日数の差を求めます。 * * @param startDateStr 開始日付 【YYYY/MM/DD】OR【YYYY/MM/DD hh:mm:ss】 * @param endDateStr 終了日付 【YYYY/MM/DD】OR【YYYY/MM/DD hh:mm:ss】 * * @return 2つの日付の日数の差を整数で返します。 * * @throws ParseException 日付フォーマットが不正な場合 * @author 曹 */ private long differenceDays(String startDateStr, String endDateStr) throws ParseException { Date startDate = DateFormat.getDateInstance().parse(startDateStr); Date endDate = DateFormat.getDateInstance().parse(endDateStr); return (endDate.getTime() - startDate.getTime()) / 86400000;//1000*60* 60*24 = 86400000; } /** 2つの日付の月数の差を求めます。 * * @param startDateStr 開始日付 【YYYY/MM/DD】OR【YYYY/MM/DD hh:mm:ss】 * @param endDateStr 終了日付 【YYYY/MM/DD】OR【YYYY/MM/DD hh:mm:ss】 * * @return 2つの日付の月数の差を整数で返します。※端数の日数は無視します。 * * @throws ParseException 日付フォーマットが不正な場合 * @author 曹 */ private int differenceMonths(String startDateStr, String endDateStr) throws ParseException { Date startDate = DateFormat.getDateInstance().parse(startDateStr); Date endDate = DateFormat.getDateInstance().parse(endDateStr); Calendar startCal = Calendar.getInstance(); Calendar endCal = Calendar.getInstance(); startCal.setTime(startDate); endCal.setTime(endDate); int count = -1; while (!startCal.after(endCal)) { startCal.add(Calendar.MONTH, 1); count++; } return count; } }
没有评论:
发表评论