计算出任意两个日期之间相隔的天数

 package com.fuxi.test.date;
/**
 * 计算出任意两个日期之间相隔的天数
 */
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Scanner;

 

public class CaculateDay {

 public static void main(String[] args) {
  long m = 0;
  Day day1 = new Day();
  Day day2 = new Day();
  System.out.println("请输入第一个日期:");
  Calendar d1 = day1.setDay();
  System.out.println("请输入第二个日期:");
  Calendar d2 = day2.setDay();
  if(d1.compareTo(d2)<0){
   m = d2.getTimeInMillis()-d1.getTimeInMillis();
  }else if(d2.compareTo(d1)<0){
   m = d1.getTimeInMillis()-d2.getTimeInMillis();
  }
  int d = (int) (m/(1000*60*60*24));
  System.out.println("相隔天数:"+d);
 }
}

 class Day{
  public Calendar setDay(){
   int year = 0,month = 0,dayofmonth =0;
   Scanner y = new Scanner(System.in);
   System.out.println("请输入年份:");
   year = y.nextInt();
   System.out.println("请输入月份:");
   month = y.nextInt();
   System.out.println("请输入某一天:");
   dayofmonth = y.nextInt();
   GregorianCalendar date = new GregorianCalendar(year,month,dayofmonth);
   return date;
  }
 }

你可能感兴趣的:(j2se,calendar,date,class,string)