我已經有一個.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]);
}
}
}
}
}
僅供參考,Joda-Time項目現在處於維護模式。該團隊建議遷移到Java 8及更高版本中構建的java.time類。 –