2014-09-25 4 views
-1

현재 날짜 시간 LocalDateTIme.now()issueDateTime이라는 오브젝트의 다른 숫자를 일 단위로 찾아야합니다. 또한 시간을 시작으로 issueDateTime부터 계산됩니다. 즉, issueDateTime이 2012 년 4 월 12 일 오후 5:00 일인 경우 오후 5 시가 아닌 12 시가 계산되어야합니다. 나는 발행일과 현재 날짜의 차이를 일 단위로 찾은 다음, rentTariff/day를 곱하여 방법 amountPayable()의 금액을 반환합니다. 감사합니다두 LocalDateTIme 오브젝트 간의 차이 일수

public class RentOut { 

private int rentalTariff ; 
private LocalDateTime dateTime; 
private String rentedTo; 
private LocalDateTime returnDateTime; 

public RentOut(int rentalTariff) { 
    this.rentalTariff = rentalTariff; 
} 
public void RentIt(LocalDateTime dateTime, String rentedTo) { 
    this.dateTime = dateTime; 
    this.rentedTo = rentedTo; 
    this.returnDateTime = dateTime.plusHours(24); 
} 
public LocalDateTime getRentDateTime() { 
    return dateTime; 
} 
public LocalDateTime returnDateTime() { 
    return returnDateTime; 
} 
public String getCustomerName() { 
    return rentedTo; 
} 
public int amountPayable() { 
    //return LocalDateTime.now().minus(dateTime)*rentalTariff; 
} 
} 
+2

문제를 보여주는 짧지 만 완전한 예제를 보여주십시오 - 단지 2 개의 LocalDateTime 값을 가진 간단한 콘솔 앱과 현재 문제를 어떻게 해결하려고하는지에 대해 설명합니다. –

+0

코드가 추가되었습니다. –

+1

그것은 짧지 만 완전한 예가 아닙니다. 그렇습니까? 샘플 데이터가 포함되어 있지 않으며 불필요한 비트가 많이 있습니다. 이 경우에 짧지 만 완전한 예제는 약 3 행의 코드가있는'main' 메소드를 포함하는 클래스 일 가능성이 높습니다. –

답변

0

올해를 사용하여 LocalDateTime에서 이것을 계산하십시오. 연도에 차이가있을 경우 원래 날짜 시간이 현재 날짜 이후 일 수 있음을 고려하십시오. (부정적인 결과가 발생할 수 있음)

+1

연중 계속되는 경우에 일의 년은 부적당 할 것 같다 ... 더 간단한 접근이있다. –

+0

실제로 Joda 시간의 Days 클래스를 사용하여 두 날짜 사이의 날짜를 계산할 수 있습니다 (http://stackoverflow.com/questions/3299972/difference-in-days-between-two-dates-in-java? rq = 1. –

+0

Joda Time을 사용할 필요가 없습니다. 'ChronoUnit.DAYS'는 Java 8의 광범위한 기능입니다. –

2

이 경우 ChronoUnit.DAYS을 사용할 수 있습니다. 시작은 "다음 날"에있는 것으로 간주해야하는지 여부를 확인하기 위해 LocalTime 값을 비교 :

import java.time.*; 
import java.time.temporal.*; 

class Test { 
    public static void main(String[] args) { 
     // Start time before end time 
     showDiff(2014, 9, 25, 15, 0, 
       2014, 9, 27, 17, 0); 
     // Start time equal to end time 
     showDiff(2014, 9, 25, 17, 0, 
       2014, 9, 27, 17, 0); 
     // Start time later than end time 
     showDiff(2014, 9, 25, 18, 0, 
       2014, 9, 27, 17, 0); 
    } 

    public static void showDiff(
     int startYear, int startMonth, int startDay, 
     int startHour, int startMinute, 
     int endYear, int endMonth, int endDay, 
     int endHour, int endMinute) { 
     LocalDateTime start = LocalDateTime.of(
      startYear, startMonth, startDay, startHour, startMinute); 
     LocalDateTime end = LocalDateTime.of(
      endYear, endMonth, endDay, endHour, endMinute); 

     System.out.printf("%s to %s is %d day(s)%n", start, end, 
      differenceInDays(start, end)); 
    } 

    public static int differenceInDays(LocalDateTime start, LocalDateTime end) { 
     LocalDate startDate = start.toLocalDate(); 
     LocalDate endDate = end.toLocalDate(); 
     if (start.toLocalTime().isAfter(end.toLocalTime())) { 
      startDate = startDate.plusDays(1); 
     } 
     return (int) ChronoUnit.DAYS.between(startDate, endDate); 
    } 
} 

출력 :

2014-09-25T15:00 to 2014-09-27T17:00 is 2 day(s) 
2014-09-25T17:00 to 2014-09-27T17:00 is 2 day(s) 
2014-09-25T18:00 to 2014-09-27T17:00 is 1 day(s)