如何计算java中的年份和月份差异

时间:2016-10-15 05:21:00

标签: java

我需要能够向用户提示包含一个月和一年的2个日期并显示该数字 多年来 这两个日期之间的月份如此

这将是输入:1999年6月

------------------------------ April,2002

这是输出:------- 3年和-2个月

这就是我需要的,相隔2年和10个月。

这是我的代码所在,任何帮助表示赞赏。

public class AgeDifference{
public static void main(String[] args){
    //Scanner input = new Scanner(System.in);
    String month1 = "June";
    String month2 = "April";
    int year1 = 1999;
    int year2 = 2002;
    int firstMonthNumber = 0;
    int secondMonthNumber = 0;
    int totalYear = 0;
    int totalMonth = 0;
    //input:
    System.out.println("For the first date,");
    System.out.print("Enter month: ");
    //month1 = input.next();
    System.out.print("Enter year: ");
    //year1 = input.nextInt();
    System.out.println("");
    System.out.println("For the second date,");
    System.out.print("Enter month: ");
    //month2 = input.next();
    System.out.print("Enter year: ");
    //year2 = input.nextInt();
    System.out.println("");

    //processing:
    if (month1.equalsIgnoreCase("January")){      
        firstMonthNumber = 1;
    } else if(month1.equalsIgnoreCase("February")){     
        firstMonthNumber = 2; 
    } else if (month1.equalsIgnoreCase("March")){      
        firstMonthNumber = 3;
    } else if(month1.equalsIgnoreCase("April")){     
        firstMonthNumber = 4; 
    } else if (month1.equalsIgnoreCase("May")){      
        firstMonthNumber = 5;
    } else if(month1.equalsIgnoreCase("June")){     
        firstMonthNumber = 6; 
    } else if (month1.equalsIgnoreCase("July")){      
        firstMonthNumber = 7;
    } else if(month1.equalsIgnoreCase("August")){     
        firstMonthNumber = 8; 
    } else if (month1.equalsIgnoreCase("September")){      
        firstMonthNumber = 9;
    } else if(month1.equalsIgnoreCase("October")){     
        firstMonthNumber = 10; 
    } else if (month1.equalsIgnoreCase("November")){      
        firstMonthNumber = 11;
    } else if(month1.equalsIgnoreCase("December")){     
        firstMonthNumber = 12; 
    }
    if (month2.equalsIgnoreCase("January")){      
        secondMonthNumber = 1;
    } else if(month2.equalsIgnoreCase("February")){     
        secondMonthNumber = 2; 
    } else if (month2.equalsIgnoreCase("March")){      
        secondMonthNumber = 3;
    } else if(month2.equalsIgnoreCase("April")){     
        secondMonthNumber = 4; 
    } else if (month2.equalsIgnoreCase("May")){      
        secondMonthNumber = 5;
    } else if(month2.equalsIgnoreCase("June")){     
        secondMonthNumber = 6; 
    } else if (month2.equalsIgnoreCase("July")){      
        secondMonthNumber = 7;
    } else if(month2.equalsIgnoreCase("August")){     
        secondMonthNumber = 8; 
    } else if (month2.equalsIgnoreCase("September")){      
        secondMonthNumber = 9;
    } else if(month2.equalsIgnoreCase("October")){     
        secondMonthNumber = 10; 
    } else if (month2.equalsIgnoreCase("November")){      
        secondMonthNumber = 11;
    } else if(month2.equalsIgnoreCase("December")){     
        secondMonthNumber = 12; 
    }

    totalYear = year1 - year2;
    totalMonth = firstMonthNumber - secondMonthNumber;

3 个答案:

答案 0 :(得分:1)

Java 8有很好的类:

public static void main(String[] args) {
    test("June, 1999", "April, 2002");
}
private static void test(String fromMonthYear, String toMonthYear) {
    DateTimeFormatter monthYearFormat = DateTimeFormatter.ofPattern("MMMM, uuuu");
    YearMonth from = YearMonth.parse(fromMonthYear, monthYearFormat);
    YearMonth to   = YearMonth.parse(toMonthYear  , monthYearFormat);
    long months = from.until(to, ChronoUnit.MONTHS);
    System.out.printf("%d years and %d months%n", months / 12, months % 12);
}

输出

2 years and 10 months

答案 1 :(得分:0)

你快到了。只需要这样做:

if(numOfMonth < 0) {
   numOfMonth = 12 + numOfMonth;
   numOfYears = numOfYears - 1; 
}

答案 2 :(得分:-1)

试试这个

String dateStart = "01/14/2012 09:29:58";
String dateStop = "01/15/2012 10:31:48";
 SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");

Date d1 = null;
Date d2 = null;

try {
    d1 = format.parse(dateStart);
    d2 = format.parse(dateStop);

    DateTime dt1 = new DateTime(d1);
    DateTime dt2 = new DateTime(d2);

    System.out.print(Days.daysBetween(dt1, dt2).getDays() + " days, ");
    System.out.print(Hours.hoursBetween(dt1, dt2).getHours() % 24 + " hours, ");
    System.out.print(Minutes.minutesBetween(dt1, dt2).getMinutes() % 60 + " minutes, ");
    System.out.print(Seconds.secondsBetween(dt1, dt2).getSeconds() % 60 + " seconds.");

 } catch (Exception e) {
    e.printStackTrace();
 }

注意:在您的Java应用程序中导入org.joda.time.* jar这个API有很多方法可以享受编码