2017-07-29 28 views
3

我试图给TrainTimeTable添加延迟。我已经使用Date物体使火车到达和离开。当我第一次打印时间表时,一切都显示正确的到达和离开时间。 然而,在埃德蒙顿和之后的所有车站增加了30分钟的延迟。 部分抵达和离开时间不正确。 延迟只能将抵达和离开时间推回,但它会改变一些城市之间的行程时间。日期类别问题:日期对象正在设置为我没有设置的东西

我已经作出了getter和setter方法,像这样:

/** 
* Accessor for arrival Date variable. Returns a copy of the arrival date. 
* @return - a copy of the arrival date 
*/ 
public Date getArrivalDate() 
{ 
    //return a copy of the original date 
    return (Date) arrival.clone(); 
} 


/** 
* Mutator for arrival date variable. 
* @param arrival - the date to set 
*/ 
public void setArrivalDate(Date arrival) 
{ 
    //uses copy of Date passed in 
    this.arrival = (Date) arrival.clone(); 
} 

这里是我将延迟 我用StationLinkedList一个对象来保存计划方法:

public void delay(String Station, int minute) { 
    //sequential search for Station 
    //iterate through list for Station 
    boolean delayPoint = false; 

    //1) adjust time of day 
    //add delay to arrival and departure date objects 
    for (Station current : schedule) { 
     //By checking for delay before checking for the delay city 
     //we will see the delayPoint variable set while on 
     //the loop is at the station right afer the delayed Station 
     //if after the delay point 
     if (delayPoint) { 
      //then add delay to arrive date 
      Date arrivalDate = current.getArrivalDate(); 
      long arrivalTime = current.getArrivalDate().getTime(); 
      arrivalTime += minute * MIN_TO_MS; 
      arrivalDate.setTime(arrivalTime); 
      current.setArrivalDate(arrivalDate); 
      //and add delay to depart date 
      Date departDate = current.getDepartureDate(); 
      long departTime = current.getDepartureDate().getTime(); 
      departTime += minute * MIN_TO_MS; 
      departDate.setTime(departTime); 
      current.setDepartureDate(departDate); 

     } //if the Station matches the point of delay 
     else if (current.getCity().equals(Station)) { 
      //then set the boolean 
      delayPoint = true; 
      //and add delay to departure 
      Date departDate = current.getDepartureDate(); 
      long departTime = current.getDepartureDate().getTime(); 
      departTime += minute * MIN_TO_MS; 
      departDate.setTime(departTime); 
      current.setDepartureDate(departDate); 
     } 

    } 
    //2) adjust day of trip 
} 

Here is my output after adding a delay of 30 min to Edmonton departure

请和谢谢,
Ahsen Husain。

+0

你能告诉我们'delay()'方法的调用 – Ali

+0

'this.arrival = new Date(arrival.getTime()); – c0der

回答

0

这是一个可能的解决方案(基于java 8 java.time API)

站类站

public class Station { 
     private long id; 
     private String name; 
     private Date arrivalDate; 
     private Date departureDate; ....getters and setters 

的ArrayList

List<Station> myStations = Arrays.asList(
      new Station(1,"City1",new Date(),new Date()), 
      new Station(2,"City2",new Date(),new Date()), 
      new Station(3,"City3",new Date(),new Date()), 
      new Station(4,"City4",new Date(),new Date()) 
    ); 

城市名称来查找和延迟时间在几分钟内。

String cityName = "City2"; 
int minutes = 30; 

获取等于CITYNAME站(在这一刻我展示代表福尔环替代)

Station current = myStations.stream() 
      .filter(s -> s.getName().equals(cityName)) 
      .findFirst().orElse(null); 

目前站等于城市名称,以便让我们添加延迟

delay(current,minutes,false); 

现在添加延迟到下一个站(假设下一个站具有和id grather比当前站)

> LocaleDateTime

public static LocalDateTime getLocalDateTimeFromDate(Date date){ 
    return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()); 
} 

public static Date getDateFromLocalDateTime(LocalDateTime localDateTime){ 
    return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant()); 
} 

这种替代是基于java -

myStations.stream() 
       .filter(s -> s.getId()>current.getId()) 
       .collect(Collectors.toList()) 
       .forEach(s -> delay(s,minutes,true)); 

延迟方法(该延迟的方法是基于java.time API)

public static void delay(Station current, int minutes, boolean isDelay){ 
    System.out.println("Current Station | Before: " + current); 
    LocalDateTime stationArrival =null; 
    LocalDateTime stationDeparture=null; 


    if (current !=null && isDelay){ 
     stationArrival = getLocalDateTimeFromDate(current.getArrivalDate()); 
     stationDeparture = getLocalDateTimeFromDate(current.getDepartureDate()); 
     stationArrival=stationArrival.plusMinutes(minutes); 
     stationDeparture=stationDeparture.plusMinutes(minutes); 
     current.setArrivalDate(getDateFromLocalDateTime(stationArrival)); 
     current.setDepartureDate(getDateFromLocalDateTime(stationDeparture)); 
    }else if(current!=null && !isDelay){ 
     stationDeparture = getLocalDateTimeFromDate(current.getDepartureDate()); 
     stationDeparture= stationDeparture.plusMinutes(minutes); 
     current.setDepartureDate(getDateFromLocalDateTime(stationDeparture)); 
    } 

    System.out.println("Current Station | After: " + current); 
} 

helppers方法从日期<变换。使用LocalDateTime的时间API,java.time对我来说非常完美。使用java.uitl.Date有许多转换要做,这很难处理。如果您有兴趣了解更多有关java.time的信息,请点击这里教程Java Date Time Api.

希望它有帮助。