我正在尝试为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();
}
这是我添加延迟的方法
我使用
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
谢谢,麻烦您了,
阿森·侯赛因。
最佳答案
这是一种可能的解决方案(基于java 8和java.time api)
车站等级
public class Station {
private long id;
private String name;
private Date arrivalDate;
private Date departureDate; ....getters and setters
阵列电台列表
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(此刻,我正在显示一种替代方法来表示foor循环)
Station current = myStations.stream()
.filter(s -> s.getName().equals(cityName))
.findFirst().orElse(null);
当前电台等于城市名称,因此可以增加延迟
delay(current,minutes,false);
现在为下一个电台添加延迟(假设下一个电台的ID比当前电台更强)
myStations.stream()
.filter(s -> s.getId()>current.getId())
.collect(Collectors.toList())
.forEach(s -> delay(s,minutes,true));
delay方法(此延迟方法基于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方法从Date 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.
希望能帮助到你。
关于java - Date类问题:Date对象被设置为我未设置的对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45395015/