2016-10-15 4 views
-3

Javaで年と月の差を計算するので、どのように私は月からなる2つの日付のユーザーと年を促し、これら2つの日付の間の年数 と ヶ月を表示できるようにする必要があり

これは、入力、次のようになります、------------------------------、1999年4月

2002年6月

これは出力です:------- 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; 
+5

リンクを貼り付けないでください。ここにコードを貼り付けます。 – progyammer

答えて

0

あなたはほぼあります。ただ、これを実行する必要があります。

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

'numOfMonth + = 12;'と 'numOfYears - ;'が良いでしょう。 – Andreas

-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(); 
} 

注意してみてください:インポートorg.joda.time.* jarファイルをJavaアプリケーションで、このAPIは非常に多くのメソッドを持って

+2

あなたはその質問を読んだことがありますか?入力は 'June、1999'と' April、2002'です。出力は「2年10カ月」でなければならない。ピュア**年**および**月**。この答えは、*年と月を除くすべてを行います。 – Andreas

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 
関連する問題