我正在尝试向TrainTimeTable
添加延迟。我使用Date
物品使火车到达和离开。当我第一次打印时间表时,一切都显示正确的到达和离开时间。
然而,在埃德蒙顿和之后的所有车站增加了30分钟的延迟。
一些到达和离开时间变得不正确。
延迟只应该推迟到达和离开时间,但它会改变一些城市之间的旅行时间。
我已经让我的吸气剂和制定者如此:
/**
* 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();
}
这是添加延迟的方法
我使用LinkedList
个Station
个对象来保存计划:
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 :(得分: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);
现在为下一个站点添加延迟(假设下一站点的站点比当前站点更高)
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);
}
从Date&lt; - &gt;转换的helppers方法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());
}
这个替代方案基于使用LocalDateTime的java.time api,java.time对我来说非常适合。使用java.uitl.Date有很多转换要做,很难处理。如果您有兴趣了解有关java.time的更多信息,请参阅教程Java Date Time Api.
希望它有所帮助。