0

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();
    }
}
i'mgnome
  • 483
  • 1
  • 3
  • 17
  • 1
    Please [edit] your question to include a [mcve]. – Jonny Henly Feb 03 '20 at 19:33
  • 2
    What happens if there's a ParseException? What does your method return? – Compass Feb 03 '20 at 19:33
  • @Compass I don't know I never tried to run it(if that's what you mean). I just didn't know when Eclipse showed me that error and that I should either **Add a return statement** or **Change return type to 'Void'** – i'mgnome Feb 03 '20 at 19:38
  • 1
    I would do what the compiler suggests and add a return statement. – Compass Feb 03 '20 at 19:42
  • @Compass I would usually do that too but in this case isn't just going to return **ts1** which is the timestamp that I'm giving as a parameter? – i'mgnome Feb 03 '20 at 19:44
  • 1
    I recommend you don’t use `SimpleDateFormat`, `TimeZone` and `Timestamp`. Those classes are poorly designed and long outdated, the first in particular notoriously troublesome. Instead use `LocalDateTime`, `ZoneId`, `DateTimeFormatter` and other classes from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Feb 03 '20 at 20:26

1 Answers1

0

As @Compass noted, the key to this problem is the expected behavior when a ParseException is thrown. In the happy path case where there's no problem parsing the date format, your function returns a value and that's great. But when ParseException is thrown, the stacktrace is printed and...then what? Eclipse is telling you that you need to specify a return value for the case where the catch block is executed. For example:

public String conv(...) {
    try {
        return Timestamp.valueOf(dfAirport2.format(dfAirport1.parse(ts1.toString())));
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    // This line will be executed if a ParseException is thrown within the try block above. We're returning a default timestamp string here because I don't know what your expected behavior is when the given format is invalid.
    return "00:00:00";
}
spork
  • 1,185
  • 1
  • 11
  • 17
  • Oh I got it now. For some reason, I thought that he will always return the "00:00:00" as given in your example. Thank you – i'mgnome Feb 03 '20 at 19:48
  • 1
    I get what you're saying, and that would be absolutely true if the `return Timestamp.valueOf(...)` line wasn't there. – spork Feb 03 '20 at 19:49