-1

I am using this method to parse a String to SQL Time in Java. However, it does not work relly good due to the fact it return a date with a negative value when I input "12:00".

Here is the method:

public static Time isoStringToTime(String isoTimeString) {
        try {
            
        SimpleDateFormat sdf = new SimpleDateFormat("hh:mm", new Locale("es", "ES"));
        return new Time(sdf.parse(isoTimeString).getTime());
        } catch (ParseException e) {
            throw new ApplicationException("Formato ISO incorrecto para fecha: " + isoTimeString);
        }
    }

The new Time returned introducing "12:00" has a negative value, -3600000 is the value.

diegogs_
  • 97
  • 9
  • `HH:mm` is the pattern you are looking for – jmj Oct 11 '21 at 19:36
  • Yeah sorry in eclipse I have it in that format – diegogs_ Oct 11 '21 at 19:38
  • Works for me: https://ideone.com/m2gtBG – shmosel Oct 11 '21 at 19:43
  • 1
    `SimpleDateFormat` is not thread safe and you should prefer using `java.time.format.DateTimeFormatter` in new code. – David Conrad Oct 11 '21 at 19:59
  • I recommend you don’t use `SimpleDateFormat` and `Time`. `SimpleDateForamt` is notoriously troublesome, `Time` is a true hack on top of the already poorly designed `Date` class, and both (and all three) are long outdated. Instead just use `LocalTime` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Oct 11 '21 at 20:59
  • @shmosel Parsing 12:00 into 00:00 — do you call that “works”? – Ole V.V. Oct 11 '21 at 21:05
  • @OleV.V. I assume it's defaulting to am. If OP is looking for 24-hour time, `HH:mm` works fine too, as @jmj suggested. – shmosel Oct 11 '21 at 21:25

1 Answers1

3

tl;dr

LocalTime.parse( "12:00" )

Details

You are using terrible date-time classes that were years ago supplanted by the modern java.time classes defined in JSR 310. Never use java.sql.Time, Date, Calendar, or SimpleDateFormat classes.

LocalTime lt = LocalTime.parse( "12:00" ) ;

To catch faulty input, trap for DateTimeParseException.

LocalTime lt = null ;
try {
    lt = LocalTime.parse( "12:00" ) ;
} catch ( DateTimeParseException e ) {
    … // Handle faulty input.
}

Submit to a database, with a JDBC driver compliant with JDBC 4.2 or later.

myPreparedStatement.setObject( … , lt ) ;

Retrieve from a database.

LocalTime lt = myResultSet.getObject( … , LocalTime.class ) ;

All this has been covered many times already on Stack Overflow. So search to learn more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • 1
    Thanks a lot, it worked! That methods were given my teachers at the university, I guess they are not so up-to-dated :) – diegogs_ Oct 11 '21 at 19:50
  • 2
    @DiegoGonzálezSuárez That is sad. The *java.time* classes arrived in [Java 8](https://en.m.wikipedia.org/wiki/Java_version_history#Java_8) back in the year 2014, seven years ago. And Java 8 was a big deal, the first Long-Term Support (LTS) version. – Basil Bourque Oct 11 '21 at 19:53
  • Those classes were deprecated in Java 1.1 in 1997. – Marlin Pierce Oct 11 '21 at 19:53
  • 2
    @MarlinPierce No, the classes were not deprecated, only many of their methods. – Basil Bourque Oct 11 '21 at 19:54