2014-09-25 38 views
-1

我需要在天數中找到當前日期時間LocalDateTIme.now()與另一個LocaDateTime對象issueDateTime之間的差異。還有從issueDateTime開始計算的日子。也就是說,如果issueDateTime是2014年12月4日下午5:00,應按下午5點計算,而不是上午12點。我想在天內找到發行日期和當前日期之間的差異,然後將其乘以rentalTariff/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

請顯示一個簡短而完整的例子證明了問題 - 只要一個帶有兩個LocalDateTime值的簡單控制檯應用程序,以及您目前如何解決該問題。 – 2014-09-25 10:51:36

+0

添加代碼,看看。 – 2014-09-25 11:00:16

+1

這不是一個簡短但完整的例子,是嗎?它不包含任何樣本數據,並且它有一堆不必要的位。在這種情況下,一個簡短但完整的例子很可能是一個只包含大約三行代碼的'main'方法的類... – 2014-09-25 11:23:37

回答

0

使用從LocalDateTime一年中的一天來計算這一點。考慮到如果年份存在差異,原始日期時間可能會在當前日期之後。 (這會導致負面結果)

+1

如果它可以跨越多年,那麼每年的年度可能都不合適...有更簡單的方法。 – 2014-09-25 11:24:03

+0

您確實可以使用Joda時間的Days類來計算兩個日期之間的天數,如下所示:http://stackoverflow.com/questions/3299972/difference-in-days-between-two-dates-in-java? RQ = 1。 – 2014-09-25 11:37:01

+0

這裏沒有必要使用喬達時間。 'ChronoUnit.DAYS'是Java 8中廣義的等價物。 – 2014-09-25 11:37:38

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)