Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to store time in java, in format of HH:MM

Tags:

java

types

time

After doing my research I wasn't able to find a method or data type that should be used for variable in order to store time in format of HH:MM, I did find methods to get this from a string like "14:15:10", but I think this is not the best way, as I'll need to add or subtract from time. I tried doing this as a double, but ran into following issue, when you have a time like 05.45 stored and add 0.15 (or 15 minutes) to it, the result is 05.60 where as with HH:MM format you'd expect it to be 06.00.

I'm looked through java documentation and still am, but can't seem to find any way to achieve this, closest I got to is date format like dd/mm/yyyy hh:mm:ss

like image 984
Ilja Avatar asked Nov 01 '13 18:11

Ilja


People also ask

How do you represent HH MM SS in Java?

The format SimpleDateFormat(“HH. mm. ss”) displays time.

How does Java Store time?

Historically using 32-bit signed integer in UNIX time will cause year 2038 problem. Thus, Java stores time in 64-bit integer, which is sufficient even if you increment it a thousand times more often. That being said, the simplest, yet valid way of storing time in Java is... Date now = new Date();

What is the data type to store time in Java?

TIME Type. The time data type. The format is yyyy- MM -dd hh:mm:ss, with both the date and time parts maintained. Mapped to java.


2 Answers

Use Joda Time. It provides much better operations to do date/time manipulation than standard java dates. If you want to use internal JDK classes, use java.util.Date.

like image 159
Jeff Storey Avatar answered Oct 08 '22 01:10

Jeff Storey


Since Java 8, you can use the new API for dates and times, including Instant, ZonedDateTime and LocalDateTime. This removes the use for the third party library Joda time. It also makes calculations more easy and correct. The advice below is a bit dated but still has some good points.

—————

What you definitely should NOT do is store them in your own custom format. Store the Long value that represents the Unix Epoch.

A DateTime is nothing more than a number to a computer. This number represents the amount of seconds (or milliseconds) since 1970-01-01 00:00:00 UTC. It's beyond the scope of this answer to explain why this date was universally chosen but you can find this by searching for Unix Epoch or reading http://en.wikipedia.org/wiki/Unix_time.

This also means there is NO timezone information stored in a DateTime itself. It is important to keep this in mind when reasoning about dates and times. For things such as comparing DateTime objects, nothing concerning localization or timezones is done. Only when formatting time, which means as much as making it readable to humans, or for operations such as getting the beginning of the day, timezones come into play.

This is also why you shouldn't store the time like 20:11:15 in a string-like format because this information is meaningless without timezone information. I will give you 1 example here: Consider the moment when the clock is moved back 1 hour, such as when moving away from daylight savings time. It just happened in a lot of countries. What does your string 02:30 represent? The first or the second one?

Calculations such as subtraction are as easy as doing the same with numbers. For example: Date newDate = new Date(date1.getTime() - date2.getTime());. Or want to add an hour to a date? Date newDate = new Date(oldDate.getTime() + 1000 * 60 * 60);

If you need more complex stuff then using Joda time would be a good idea, as was already suggested. But it's perfectly possible to just do even that with the native libraries too.

If there's one resource that taught me a lot about date/time, it would be http://www.odi.ch/prog/design/datetime.php

like image 26
Sebastiaan van den Broek Avatar answered Oct 08 '22 01:10

Sebastiaan van den Broek