2

I've been tasked with writing a program that records flight data so I decided to make a flight object.

Within this object is an departure time and an arrival time. I'm unsure as to which object would be best to use for this, I want to store the time in 24 hour (hh:mm) time and be able to calculate the duration of the flight in hours and minutes (hh:mm), I've looked at the docs and I've confronted with 3 different time objects.

1 java.util.Date

  • Useless because it contains too much information such as the year and month, this is a very basic program for making graphs, and most of the data manipulation uses longs.

2 java.sql.Date

  • Doesn't contain a constructor that deals with time on the same scale I want to and uses longs for manipulation.

3 java.sql.Time

  • Does seem to have the correct kind of constructors, but it's "Deprecated." and I'm sure I read somewhere that it means that's no longer going to be supported in later versions and is bad practice to use them. Which leaves me with the Time(long) constructor, again. Not very useful for me because that means I need to convert each time to longs before making the time.

tl;dr:

Are there any libraries that can create a time such that it looks like

(pseudocode):
Object time = new Constructor(23,11) 

That aren't "Deprecated" so I don't have to convert to long first. Or, do I have to make my own Object?

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
James
  • 1,259
  • 1
  • 10
  • 21
  • Have you tried your pseudocode? IIRC, Java does the promotion to long for you. – Teepeemm Nov 22 '15 at 03:26
  • Not quite sure what you mean. Like I can't make a time object using these values without getting a score though it as its "Deprecated" – James Nov 22 '15 at 03:28

3 Answers3

5

As its name imply, the java.sql is meant to interface with SQL databases and you should probably not use it in other cases.

If you can use java 8, I'd recommend using java.time.LocalTime. With this class, you won't have to bother about date informations nor timezones. You can create instances very easily :

LocalTime.of(17, 18); // the train I took home today
LocalTime.parse("10:15:30"); // From a String

Extract from Java SE 8 Date and Time

If you can't, you should use java.util.Date ; however, nothing forbids you from subclassing it to make it easier to manipulate time without regard for the date.

Aaron
  • 24,009
  • 2
  • 33
  • 57
  • Thank you for your answer, I've noted that I need to pay more attention to the package I'm in rather than just clicking on anything to do with time. I'll keep that in mind in future ^_^. – James Nov 22 '15 at 03:40
5

java.time in Java 8+

In Java 8+, you could use java.time.Duration (and perhaps java.time in general). Something like,

public static void main(String[] args) {
    Duration d = Duration.ofHours(23).plusMinutes(11);
    System.out.println(d);
}

Joda-Time in Earlier Versions of Java

If you aren't using Java 8+, you can use Joda-Time.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • java.time is basically Joda-Time. Joda-Time was so awesome compared to other options that it essentially got copied into the main SDK. java.time/Joda-Time has immutable objects and all sorts of other stuff to make it harder for you the programmer to royally !@#$ stuff up. – Matthew Gunn Nov 22 '15 at 03:44
  • 1
    @MatthewGunn - They are similar, but not *exactly* the same. See [the differences here](http://stackoverflow.com/a/24635657/634824). – Matt Johnson-Pint Nov 22 '15 at 22:48
2

If you want to maniplate time, and not have to reinvent a number of wheels, you should use a full-blown implementation such as java.time.LocalTime in Java 8 or joda-time: http://www.joda.org/joda-time/.

What's the harm in your storing year, month, and day info? It seems to me your filghts will always happen at a particular point in time. Neverthless, jode-time has a LocalTime class that has no notion of date. I'm just wondering how you are going to handle filghts that start on one day and end on another.

Robert Moskal
  • 21,737
  • 8
  • 62
  • 86
  • " I'm just wondering how you are going to handle flights that start on one day and end on another." - They don't. It's coursework for university. All flights only have a departure time (in hours and minutes) and arrival time, the same day. Not very logical to store your time in just hours and minutes for the reasons you pointed out, but its what the professors wants. :( – James Nov 22 '15 at 03:44