1

I'm new on Java and the convertion formats and I have a doubt with my convertion, this the code

Time time_normal_format;
long time_milliseconds;

time_milliseconds = time_normal_format.getTime();
//time_normal_format prints on screen "00:00:05"//

System.out.println("Time in milliseconds: "+time_milliseconds);

My doubt it's why my convertion sets "21605000" on time_milliseconds instead only 5000.

  • 2
    21600000 would represent 6 hours - any chance you are 6 hours from UTC? Printing usually involves time zone adjustments whereas the raw time value may be referenced to UTC or some other common time reference. Which time class package are you referring to? –  May 16 '21 at 23:45
  • Are you trying to get a count of milliseconds between midnight and a time-of-day value? – Basil Bourque May 17 '21 at 04:11
  • Is 00:00:05 a time of day (5 seconds past midnight) or is it an amount of time, for example a duration? Asking because those two are handled very differently in Java. – Ole V.V. May 17 '21 at 04:58
  • Under all circumstances I recommend you don’t use `java.sql.Time`. That class is poorly designed and long outdated. Instead use `LocalTime` for a time of day or `Duration` for an amount of time; both are from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. May 17 '21 at 05:00
  • Related: [Time.valueOf method returning wrong value](https://stackoverflow.com/questions/61448647/time-valueof-method-returning-wrong-value) – Ole V.V. May 17 '21 at 05:26

1 Answers1

5

tl;dr

Duration
.between(
    LocalTime.MIDNIGHT , 
    myJavaSqlTime.toLocalTime()
)
.toMillis()

Avoid legacy date-time classes

Apparently you are using java.sql.Time class. Don’t.

The date-time classes bundled with the earliest versions of Java are terrible, written by people who did not understand the nuances and complexities of date-time handling. Add on some very poor choices in design, and the result is an awful mess.

java.time

The legacy date-time classes were years ago supplanted by the modern java.time classes.

LocalTime

To represent a time-of-day, without a date and without a time zone, use LocalTime.

LocalTime lt = LocalTime.of( 15 , 30 ) ;    // 3:30 PM
LocalTime lt = LocalTime.parse( "15:30" ) ; // ditto

To get a count of milliseconds since midnight, use Duration.

Duration d = Duration.between( LocalTime.MIDNIGHT , lt ) ;
long millis = d.toMillis() ;

Going the other direction, from a count of milliseconds since midnight to a time-of-day.

Duration d = Duration.ofMillis( 5_000 ) ;
LocalTime lt = LocalTime.MIDNIGHT.plus( d ) ;

Keep in mind that this kind of code only works for generic 24-hour days. In the real world, in various time zones, days can be other than 24 hours long. They can be 23 or 25 hours with Daylight Saving Time (DST) cutovers. And they can actually be any length as dictated by politicians, such as 23.5 hours, or off by 15 minutes, or any other adjustment deemed worthwhile for various political, cultural, diplomatic, or martial reasons.

Conversion to/from legacy class

If you must interoperate with some old code not yet updated to java.time, you can easily convert back-and-forth. Look to new methods added to the old classes. If given a java.sql.Time object, call toLocalTime to get a LocalTime object.

LocalTime lt = myJavaSqlTime.toLocalTime() ;

Going the other direction, from modern class to legacy class.

java.sql.Time myJavaSqlTime = Time.valueOf( lt ) ;

Duration

If your real goal is to represent a span-of-time not related to the clock or calendar, then you should not be using LocalTime or java.sql.Time.

You should instead be using Duration class to represent a span of time on the scale of hours-minutes-seconds while unattached to the timeline. (For a scale of years-month-days, use Period class.)

To represent such a span-of-time textually, I strongly recommend not using clock-time formatting such as 00:00:05. Such strings are inherently ambiguous, looking like a time-of-day.

ISO 8601

Instead, use standard ISO 8601 format: PnYnMnDTnHnMnS. The P marks the beginning. The T separates any years-months-days from any hours-minutes-seconds. So your example of five seconds would be PT5S.

The java.time classes use ISO 8601 formats by default when parsing/generating date-time text.

Duration d = Duration.parse( "PT5S" ) ;

Going the other direction.

String output = Duration.ofSeconds( 5 ).toString() ;   // PT5S

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes. Hibernate 5 & JPA 2.2 support java.time.

Where to obtain the java.time classes?

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154