Best way to save time in java, in HH: MM format

After doing my research, I could not find a method or data type that should be used for a variable to store time in HH: MM format, I found methods to get this from a string like "14: 15: 10", but I think that this is not the best way, since I will need to add or subtract from time. I tried to do this as a double , but ran into the following problem when you have a time like 05.45 and add 0.15 (or 15 minutes) to it, the result is 05.60 , where, as in the HH: MM format, you expect this to be will be 06.00.

I am browsing the java documentation and still have, but cannot find any way to achieve this, the closest I got is the date format, for example dd/mm/yyyy hh:mm:ss

+7
java types time
source share
6 answers

Use Joda Time . It provides much better operations for handling dates / times than standard java dates. If you want to use JDK inner classes, use java.util.Date .

+5
source share

What you definitely should NOT do is store them in your own format. Save the Long value representing the Unix era.

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

It also means the lack of time zone information stored in DateTime itself. It is important to keep this in mind when discussing dates and times. For things like comparing DateTime objects, nothing happens regarding localization or time zones. Only when formatting the time, which means enough to make it readable to people, or for operations such as getting the start of the day, do time zones take effect.

This is why you should not store time like 20:11:15 in a string-like format, because this information is meaningless without time zone information. I will give one example here: consider the moment when the clock moves back 1 hour, for example, when changing from summer time. This has happened in many countries. What is your line 02:30? First or second?

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

If you need more sophisticated material, then using time in Joda would be a good idea, as already suggested. But it is entirely possible to do this even with native libraries.

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

+3
source share

The answer that suits your case depends on what you want to do.

  • Do you use RDBMS as a persistence mechanism?
  • If so, are you already working with legacy data formats or are you building a database from scratch?
  • Are you just storing this data or will you perform extensive arithmetic calculations on dates and / or priorities?
  • Are you in the same time zone or do you need to work with time points in many time zones?

All of these things are important and influence your decision about how to represent your times and dates.

If your needs require a lot of date arithmetic (for example, determining days between dates) or sorting based on timestamps, consider using a floating point date format. The advantage of using the number format for timestamps is that performing arithmetic operations on dates and comparing / sorting becomes trivial; you are just doing simple arithmetic. Another advantage is that float and longs are primitive data types. They do not need serialization, they are already very lightweight, and everything you need to use them does not require external dependencies.

The main disadvantage of using number formats for timestamps is that they are not human friendly. You will need to convert them to and from String format so that users can interact. This is often worth the effort. See: How to use Julian numeric numbers with the Java Calendar API?

I recommend that you consider storing timestamps as Julian number (JDN) or Modified Julian day number (MJD). Both will represent dates and times to millisecond precision, using 8 bytes each. Conversion algorithms to and from display formats for both of them are highly standardized. They give all the benefits of using numeric dates. Moreover, they are defined only for GMT / UTC, which means that your timestamps are universal for all time zones right out of the box (provided that they are correctly localized).

+1
source share

Java has the java.sql.Time format for working with time of day values. Just import it and create variables.

 import java.sql.Time; //now we can make time variables Time myTime; 

Just saw it at https://db.apache.org/derby/docs/10.4/ref/rrefsqlj21908.html

+1
source share

If you do not need the full date object, it is best to store it in a string, but I personally still recommend date , as it also contains many convenient methods that will come in handy. You can simply get the time as a whole from the date object and ignore the rest.

0
source share

In terms of "saving" the date, you should use long . This is how the system sees this and how all the calculations are done. Yes, since some indicate that you will eventually need to create a String so that a person can read it, but where people encounter difficulties is when they start thinking about a date in format. The format is for reading, not for computing. java.util.Date and java.util.Calendar fraught with problems (effective Java, Bloch, etc. there is a lot to say about this), but are still the norm if you need convenient date operations.

0
source share

All Articles