2017-08-27 64 views
0

我已經有一個.txt文件,它包含一些bdays的月份,日期和年份,我可以找到它們之間的差異(假設有五個需要排序)。我做了一個for循環,可以找到日期之間的差異,但我似乎無法弄清楚如何計算for循環的轉數(計算dayCount的五次差)。調用一個循環值

這是保存日期的Bday.txt文件。

1 01 2011  John Pickard 
    5 16 1968  Dad Apostol 
    2 12 2003  Pax Johnson 
    6 12 2009  Tired Joe 
    12 1 2002  Joshua Ike 

如果我們假設今天是2017年8月26日,兩個日期之間的總數(使用jodatime)是

//this is sorted, and the CORRECT 
[-237, -195, -102, -75, 97] 
//it goes as Pickard, Johnson, Apostol, Joe, Ike 
//what the wrong output is 
[1, 2, 3, 4, 5] 
//me attempting to put dayCount = 1 inside a for loop. 

這裏是我的代碼:

import chn.util.*; 
import java.util.Arrays; 

import org.joda.time.DateTime; 
import org.joda.time.DateTimeZone; 
import org.joda.time.LocalDate; 
import org.joda.time.Days; 
class starter{ 
    /* 
    Trying to implement changes from mark 2.5 into mark 3 
    */ 
    public static void main(String args[]){ 

    //to orginize what todays date is. 
    EasyReader todaysmon; 
    EasyReader todayday; 
    EasyReader todayyear; 

    //month-day-year 

    //current 
    int month; 
    int day; 
    int year; 

    //the bday time month thing 
    int bmonth; 
    int bday; 
    int byear; 

    //set x = 0; x<numoflines; x++ 

    System.out.println(" "); 
    System.out.println(" "); 
    System.out.print("What is the month right now? "); 
    todaysmon = new EasyReader(); 
    month = todaysmon.readInt(); 
    System.out.print("What is today day? "); 
    todayday = new EasyReader(); 
    day = todayday.readInt(); 
    System.out.print("What is the year today? "); 
    todayyear = new EasyReader(); 
    year = todayyear.readInt(); 

    EasyReader database; 
    database = new EasyReader("Bday.txt"); 

    //x = to how many lines are in the .TXT file. 
    for (int x=0; x<5; x++){ 

    String[] myStrings = {database.readWord(),database.readWord(),database.readWord(),database.readWord(),database.readWord()}; 

    //System.out.println(Arrays.toString(myStrings)); 

     //this should do it with every value 

     //this is a test if the thing is intilizing correctly. 

     // for(int g = 0; g < myStrings.length; g++){ 
      // if(g == 0){ 
       //arrays go like this [0,1,2] 
      // System.out.println("This is the month value: "+ myStrings[0]); 
      // } 
     // } 

     //this will determine how maby days are the closest. 

      for(int diffinday = 0; diffinday < myStrings.length; diffinday++){ 

      //making another copy 

      bmonth = Integer.parseInt(myStrings[0]); 
      bday = Integer.parseInt(myStrings[1]); 
      byear = Integer.parseInt(myStrings[2]); 
      LocalDate start = new LocalDate(year,month,day); 
      LocalDate end = new LocalDate(year,bmonth,bday); 
      int dayCount = Days.daysBetween(start, end).getDays(); 
      System.out.println(dayCount); 

       if(dayCount < 0){ 
       System.out.println("This value is null!");    
       } 
       else{ 
       /*for now, we can only identify pos ints. we need to make an array that will 
       find the closest date, W/O IT BEING NEGATIVE*/ 
       System.out.println("The closest birthday is "+myStrings[4]); 
       } 
     } 
    } 
} 

}

+1

僅供參考,Joda-Time項目現在處於維護模式。該團隊建議遷移到Java 8及更高版本中構建的java.time類。 –

回答

1

試試這個:

public class DatesDiff { 
    private static String[] bdayDates = {"1 01 2011","5 16 1968","2 12 2003","6 12 2009","12 1 2002"}; 
    static int [] myArray = new int [bdayDates.length]; 
    public static void main(String[] args) { 
     for(int diffinday = 0; diffinday < bdayDates.length; diffinday++){ 
      String bday = bdayDates[diffinday]; 
      int bmonth = Integer.parseInt(bday.split(" ")[0]); 
      int birthday = Integer.parseInt(bday.split(" ")[1]); 

      LocalDate start = new LocalDate(LocalDate.now().getYear(),bmonth,birthday); 
      LocalDate end = LocalDate.now(); 
      int dayCount = Days.daysBetween(end, start).getDays(); 
      System.out.println(dayCount); 
      myArray[diffinday] = dayCount; 
      Arrays.sort(myArray); 
      System.out.println(Arrays.toString(myArray)); 

      if(dayCount < 0){ 
      System.out.println("This value is null!");    
      } 
      else{ 
      System.out.println("The closest birthday is "+bday); 
      } 
     } 
    } 
} 
+0

這不起作用,因爲我需要單獨的.txt文件中的日期。好的回答雖然 –

+0

一旦循環完成,希望你可以複製日期在單獨的.txt文件。 – Unknown

+0

我需要從單獨的文件中獲取日期,並且能夠使用EasyReader計算它在temp.java文件中的差異。最終,我的想法是,當我獲得500多個日期時,我可以通過使for循環等於500+來輕鬆修改代碼。雖然我不能將500個日期複製到數組中。這不是解決問題的有效方法。但感謝您的答案。 –