I have a MySQL database table that has a list of flights that contains among other things the landing date and time(timestamp) in the source country and I want to add the landing date and time(also timestamp) to it.
I have another table that a list of airports including their name, country and timezone.
I created a function that gets the timezone depending on the airport name called getAirportTimeZone. Now, after looking around on stackoverflow I found this question that lets me convert from one timezone to another so when I tried it in a main class It worked fine but when I try to implement it in a method in my connection class where I could later use that method when I try to create a new flight I get an Eclipse error: This method must return a result of type Timestamp.
Here's how i tried it in my main class:
String s = class1.getAirportTimeZone("Gabes Airport");
System.out.println(s);
DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ROOT);
df1.setTimeZone(TimeZone.getTimeZone(s));
DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ROOT);
df2.setTimeZone(TimeZone.getTimeZone(class1.getAirportTimeZone("Arcata Airport")));
try {
System.out.println(df1.format(df2.parse("2020-02-03 19:30:00")));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
And here it is as a function in the other class:
public String getAirportTimeZone(String airport) {
connect();
String sql="SELECT tz FROM airports WHERE name=?";
PreparedStatement statement;
String tz="";
try {
statement = connection.prepareStatement(sql);
statement.setString(1, airport);
ResultSet rs = statement.executeQuery();
while(rs.next()) {
tz = rs.getString("tz");
}
} catch (SQLException e) {
e.printStackTrace();
}
return tz;
}
public Timestamp conv(String airportName1, String airportName2, Timestamp ts1) {
DateFormat dfAirport1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ROOT);
dfAirport1.setTimeZone(TimeZone.getTimeZone(getAirportTimeZone(airportName1)));
DateFormat dfAirport2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ROOT);
dfAirport2.setTimeZone(TimeZone.getTimeZone(getAirportTimeZone(airportName2)));
try {
return Timestamp.valueOf(dfAirport2.format(dfAirport1.parse(ts1.toString())));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}