檔案下載:39269289
/* (程序頭部注釋開始) * 程序的版權和版本聲明部分 * Copyright (c) 2011, 煙台大學計算機學院學生 * All rights reserved. * 文件名稱:判斷兩個日期的大小關系 * 作 者: 雷恒鑫 * 完成日期: 2012 年 10 月 25 日 * 版 本 號: V1.0 * 對任務及求解方法的描述部分 * 輸入描述: * 問題描述: * 程序輸出: * 程序頭部的注釋結束 */ import java.util.*; import javax.swing.JOptionPane; public class Test { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int year,month,date; String str1=JOptionPane.showInputDialog("請輸入年:"); year = Integer.parseInt(str1); String str2=JOptionPane.showInputDialog("請輸入月:"); month = Integer.parseInt(str2); String str3=JOptionPane.showInputDialog("請輸入日期:"); date = Integer.parseInt(str3); Date date1 = new Date(year,month,date); String str4=JOptionPane.showInputDialog("請輸入您想比較的年:"); year = Integer.parseInt(str4); String str5=JOptionPane.showInputDialog("請輸入您想比較的月:"); month = Integer.parseInt(str5); String str6=JOptionPane.showInputDialog("請輸入您想比較的日期:"); date = Integer.parseInt(str6); Date date2 = new Date(year,month,date); if(date1.judge_big_small(date2)) { //System.out.println("您輸入的第二個日期大於第一個日期"); JOptionPane.showMessageDialog( null,"您輸入的第二個日期大於第一個日期", "success", JOptionPane.DEFAULT_OPTION ); } else { //System.out.println("您輸入的第二個日期小於第一個日期"); JOptionPane.showMessageDialog( null,"您輸入的第二個日期小於第一個日期", "success", JOptionPane.DEFAULT_OPTION ); } //System.out.println("相隔天數为"+date1.judge_time(date2)+"天"); JOptionPane.showMessageDialog( null,"兩個日期的相隔天數为"+date1.judge_time(date2)+"天", "success", JOptionPane.DEFAULT_OPTION ); } }
Date類
import java.util.Calendar; public class Date { private int year,month,date; Date(int year,int month,int date) { this.year = year; this.month = month; this.date = date; } boolean judge_big_small(Date date){ if(this.year>date.year) { return true; } else if(this.month>date.month) { return true; } else if(this.date>date.date) { return true; } return false; } long judge_time(Date date){ Calendar calendar1 = Calendar.getInstance(); Calendar calendar2 = Calendar.getInstance(); calendar1.set(this.year, this.month, this.date); calendar2.set(date.year, date.month, date.date); long time2006 = calendar1.getTimeInMillis(); long time2008 = calendar2.getTimeInMillis(); long time = (time2008-time2006)/(1000*60*60*24); return time; } }
From:CSDN
全站熱搜
留言列表