2013-06-20 3 views
1

나는 날짜 X에서 시작하여 날짜 Y에서 끝나고 하루 만에 끝나는 문서를 가지고 있습니다. 내 임무는이 문서를 살펴보고 문서에서 누락 된 일수를 확인하는 것입니다.자바에서 캘린더 또는 Joda-Time 사용

Example: 
19990904 56.00 
19990905 57.00 
19990907 60.00 

19900906가 누락되었습니다.

저는 자바 캘린더, 날짜 및 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]); 

답변

3

JodaTime. (당신은 단지 날짜에 관심을 경우, 당신은 시간, 분, 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; 
} 
+0

JodaTime을 사용하기 위해 무엇이든 가져올 필요가 있습니까? – Danny

+1

@ 대니 예 조담 자체 http://mvnrepository.com/artifact/joda-time/joda-time/2.2 – NimChimpsky

+0

나는 때문에 오류가 발생합니다. LocalDate 및 DateTimeFormatter 사용 – Danny

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

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

missingdays = originalSizeofList - i; 

이것은 joda 시간, 그 훨씬 쉽게 :

나는 이미이 코드가 있습니다.

+0

일을 사람이 읽을 수있는 필드를 사용하여 날짜를 조작하려는 모든 장소에서 Joda 시간을 사용합니다. – Jim

+0

방금 ​​내 질문을 업데이트했는데, 내 정보가 충분하지 못했습니다. 버퍼링 된 판독기를 사용하고 각 줄 (파일이 20GB)이므로이 배열이 작동하지 않을 것이라고 생각합니다. :-( – Danny

+1

OP가 누락 된 모든 요일을 인쇄하기를 원했기 때문에 질문에 답하지 않습니다. – Artyom