1

日付 X に開始し、日付 Y に終了し、1 日ずつ増加するドキュメントがあります。私の仕事は、この文書を調べて、文書から何日が抜けているかを調べることです。

Example:
19990904 56.00
19990905 57.00
19990907 60.00

19900906 が欠落していることを印刷する必要があります。

私はいくつかの調査を行い、Java カレンダー、日付、および Joda-Time について読みましたが、それらが何であるかを理解できませんでした。私が今言及したこれらの機能が何をするのかを説明し、私の目標を達成するためにそれらを使用する方法について提案してもらえますか?

私はすでにこのコードを持っています:

String name = getFileName();
BufferedReader reader = new BufferedReader(new FileReader(name));

String line;

while ((line = reader.readLine()) != null)
{  //while
    String delims = "[ ]+";
    String [] holder = line.split(delims);

    // System.out.println("*");

    int date = Integer.parseInt(holder[0]); 
    //System.out.println(holder[0]);

    double price = Double.parseDouble(holder[1]);
4

2 に答える 2

3
LocalDate x = new LocalDate(dateX); 
LocalDate y = new LocalDate(dateY);

int i = Days.daysBetween(x, y).getDays();

missingdays = originalSizeofList - i;

これは Joda-time であり、バニラ Java よりもはるかに簡単です。

于 2013-06-20T12:42:21.943 に答える
3

JodaTimeで。(日付のみに関心がある場合は、datetimes を使用したり、時間、分、dst の問題をいじったりしないでください。)

final DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyyMMdd");

LocalDate date=null;
while( (line = getNextLine())!=null) {
   String dateAsString = line.split(delims)[0];
   LocalDate founddate = dtf.parseLocalDate(dateAsString);
   if(date==null) { date= founddate; continue;} // first
   if(founddate.before(date)) throw new RuntimeException("date not sorted?");
   if(founddate.equals(date)) continue; // dup dates are ok?
   date = date.plusDays(1);
   while(date.before(foundate)){
       System.out.println("Date not found: " +date);
       date = date.plusDays(1);
   }
}

欠落した日数のみをカウントする必要がある場合:

LocalDate date=null;
int cont=0;
while( (line = getNextLine())!=null) {
   String dateAsString = line.split(delims)[0];
   LocalDate founddate = dtf.parseLocalDate(dateAsString);
   if(date==null) { date= founddate; continue;} // first
   if(founddate.before(date)) throw new RuntimeException("date not sorted?");
   if(founddate.equals(date)) continue; // dup dates are ok?
   cont += Days.daysBetween(date, founddate)-1;
   date = founddate;
}
于 2013-06-20T12:51:12.573 に答える