gpt4 book ai didi

java - 字符串时间到实际时间

转载 作者:行者123 更新时间:2023-12-01 09:14:11 28 4
gpt4 key购买 nike

我想做的是使用JGraphT创建飞行路径的行程。我面临的问题是将我设置的字符串时间转换为实际时间,然后我可以在多天内进行计算,即如果航类在 16.00 起飞并于 18.30 到达,但转接航类在 14.00 起飞并到达最终目的地16:00 就已经过去了 24 小时(即一天)。我迷失了方向,因为我试图将字符串解析为 Flight 类中的日期,并且还使用了导致错误的简单日期格式。

我的代码如下;

Flight3.java

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.StringTokenizer;
import org.jgrapht.alg.DijkstraShortestPath;
import org.jgrapht.graph.DefaultWeightedEdge;
import org.jgrapht.graph.SimpleDirectedWeightedGraph;

public class Flight3
{
private static ArrayList<String[]> myEdges;
private static ArrayList<Flight> flight;

public Flight3()
{
}



public static void main(String [] args)
{
myEdges = new ArrayList<String[]>();
flight = new ArrayList<Flight>();
SimpleDirectedWeightedGraph<String, DefaultWeightedEdge> Graph = createGraph();

System.out.println("Airlines!");
Scanner sc = new Scanner(System.in);
System.out.println("Enter the airport you wish to fly from");
String startVertex = sc.nextLine();
while(!Graph.containsVertex(startVertex))
{
System.out.println("Sorry, that airport does not exist. Please select another;");
startVertex = sc.nextLine();
}
System.out.println("Enter destination airport");
String endVertex = sc.nextLine();
while(!Graph.containsVertex(endVertex))
{
System.out.println("Sorry, that airport does not exist. Please select another;");
endVertex = sc.nextLine();
}
calculatePath(Graph, startVertex, endVertex);
}

private static SimpleDirectedWeightedGraph<String, DefaultWeightedEdge> createGraph()
{
SimpleDirectedWeightedGraph<String, DefaultWeightedEdge> g =
(SimpleDirectedWeightedGraph<String, DefaultWeightedEdge>) new SimpleDirectedWeightedGraph<String, DefaultWeightedEdge>(DefaultWeightedEdge.class);
{
g.addVertex("London");
g.addVertex("France");
g.addVertex("Spain");

createTwoWayWeightedEdge(g, "London", "France", 80);
generateFlight("1600", "1830", "EH445", "0000", 80);
generateFlight("0400", "0600", "HE452", "0000", 80);
createTwoWayWeightedEdge(g, "France", "Spain", 130);
generateFlight("1400", "1600", "HD123", "0400", 130);
generateFlight("0400", "0600", "DH712", "0000", 130);
}
return g;
}

private static void createTwoWayWeightedEdge(SimpleDirectedWeightedGraph<String, DefaultWeightedEdge> g, String vertex1, String vertex2, double weight)
{
g.addEdge(vertex1, vertex2);
g.addEdge(vertex2, vertex1);

g.setEdgeWeight(g.getEdge(vertex1, vertex2), weight);
g.setEdgeWeight(g.getEdge(vertex2, vertex1), weight);

String[] tmp1 = {vertex1, vertex2};
myEdges.add(tmp1);
String[] tmp2 = {vertex2, vertex1};
myEdges.add(tmp2);
}

private static void generateFlight(String depTime, String arrTime, String flightNo, String locTime, int duration)
{
Flight f = new Flight(depTime, arrTime, flightNo, locTime, duration);
flight.add(f);
}

private static String textToPrint(String[] format)
{
String text = " ";
for(int i = 0; i < format.length; i++)
{
switch(i)
{
case 0:
text = text + format[i];
for(int j = format[i].length(); j < 6 ; j++)
text = text + " ";
break;

case 1:
text = text + format[i];
for(int j = format[i].length(); j < 15 ; j++)
text = text + " ";
break;

case 2:
text = text + format[i];
for(int j = format[i].length(); j < 10 ; j++)
text = text + " ";
break;

case 3:
text = text + format[i];
for(int j = format[i].length(); j < 10 ; j++)
text = text + " ";
break;

case 4:
text = text + format[i];
for(int j = format[i].length(); j < 15 ; j++)
text = text + " ";
break;

case 5:
text = text + format[i];
for(int j = format[i].length(); j < 10 ; j++)
text = text + " ";
break;
case 6:
text = text + format[i];
for(int j = format[i].length(); j < 10 ; j++)
text = text + " ";
break;
}
}
return text;
}

private static void calculatePath(SimpleDirectedWeightedGraph<String, DefaultWeightedEdge> g, String startVertex, String endVertex)
{
DijkstraShortestPath path = new DijkstraShortestPath(g, startVertex, endVertex);
path.getPath();
List<Object> edges = path.getPathEdgeList();

String item;
int count = 1;
double totalDuration = 0;

if(edges!=null)
{
System.out.println("\n The lowest cost route is:");
String[] labels = {"Flight.", "Leave from", "At", "On", "Arrive", "At", "Local Time"};
System.out.println(textToPrint(labels));

for(Object edge : edges)
{
item = edge.toString();

StringTokenizer st = new StringTokenizer(item, ":");

String firstAirport = st.nextToken().trim().substring(1);
String secondAirport = st.nextToken().trim();
secondAirport = secondAirport.substring(0, secondAirport.length()-1);

String depTime = null;
String arrTime = null;
String flightNo = null, locTime = null;
double price, flightDuration;

for(int i=0;i<flight.size();i++)
{
if(firstAirport.equals(myEdges.get(i)[0]) && secondAirport.equals(myEdges.get(i)[1]))
{
Flight details = flight.get(i);
flightNo = details.flightNo;
depTime = details.depTime;
arrTime = details.arrTime;
price = details.price;
flightDuration = details.duration;
totalDuration = totalDuration + details.getDuration();
locTime = details.getLocTime();

String[] flightInfo = {count+".", firstAirport, depTime, flightNo, secondAirport, arrTime, locTime};
System.out.println(textToPrint(flightInfo));
}
}
count++;

}
System.out.println("Cost of route = £"+path.getPathLength());
System.out.println("Total time in the air = "+totalDuration +"hrs");
}
else
System.out.println("Sorry you can't fly there from " + startVertex);
}

Flight.java

import java.text.SimpleDateFormat;

public class Flight {
String depTime;
String arrTime;
String flightNo;
String locTime;
double duration;
int price;

public Flight(String depTime, String arrTime, String flightNo, String locTime, int duration){
this.depTime = depTime;
this.arrTime = arrTime;
this.flightNo = flightNo;
this.locTime = locTime;
this.duration = duration;
}

public double getDuration(){
double duration = Integer.parseInt(arrTime) - Integer.parseInt(depTime);
return duration / 100;
}

public String getLocTime(){
int value = Integer.parseInt(locTime) + Integer.parseInt(arrTime);
locTime = ""+value;
return locTime;
}

public String getFlightNo(){
return flightNo;
}


public double getPrice(){
return price;
}
}

最佳答案

你的问题不太清楚。但这里有一些一般性提示。

出发 + 持续时间 = 到达

不要关注到达。到达时间是[到达时间+持续时间]的结果。换句话说,是输出而不是输入。这解决了您在午夜昼夜轮换的问题。

使用对象,而不是字符串

使用对象,而不是字符串。 Java 在 java.time 类中有一个优秀的业界领先的日期时间框架。使用它们。但是不要使用众所周知的麻烦的旧遗留日期时间类,即java.time包之外的类。仅根据用户界面需要和序列化数据使用字符串。

特别是,您应该查看 LocalTimeDuration类。请参阅Oracle Tutorial .

ISO 8601

对于序列化,请遵循标准 ISO 8601诸如 HH:MM 之类的格式仅表示时间值,对于“基本”版本,冒号是可选的,但我建议为“扩展”版本保留冒号。在解析和生成表示日期时间值的字符串时,java.time 类默认使用 ISO 8601 的扩展版本。

使 Flight 不了解图表,反之亦然

将数据模型与图表绘图分开。将 Flight 定义为仅航类信息和功能,而不考虑图表。

class Flight {
LocalTime departure;
Duration duration;

LocalTime getArrival() {
LocalTime arrival = departure.plus( duration );
// If called *many* times, and you account for changing-data and thread-safety, you could cache this result for performance.
return arrival;
}

Flight( LocalTime departureArg , Duration durationArg ) {
this.departure = departureArg;
this.duration = durationArg;
}
}

要生成图表所需的准确数据,请在 Flight 上使用 getter 方法,或者在 Flight 和图表之间使用中介类。尽可能保持类(class)清晰分开。您汽车的 radio 立体声不需要了解有关空调的任何信息,而空调也不需要了解发动机的燃油-氧气混合比。

时区

房间里的大大象是时区。你在这里还没有明确表达你的意图。

通常最好在 UTC 工作。仅根据需要转换为本地时区以呈现给用户。

在您澄清业务环境的意图之前,对此无需多说。

Java 类 LocalDateTimeOffsetDateTimeZonedDateTimeZoneOffset 的搜索堆栈溢出ZoneId 了解更多信息。

关于java - 字符串时间到实际时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40695078/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com