Robertas Streikus
Robertas Streikus

Reputation: 1

SimpleDateFormat does not parse correctly

  String current = "07:00:00.160"

  DateFormat df2 = new SimpleDateFormat("HH:mm:ss.SSS"); 

  DateList.add(df2.parse(current));

Returns [Thu Jan 01 07:00:00 GMT 1970]

I have tried this as well

    Date currentdate;
    currentdate = df2.parse(current);
    df2.format(currentdate);
    DateList.add(currentdate);

Returns also

[Thu Jan 01 07:00:00 GMT 1970]

and I have also tried using df2.setLenient(false);

Upvotes: 0

Views: 66

Answers (1)

Anonymous
Anonymous

Reputation: 86324

java.time.LocalTime

I recommend that you use java.time, the modern Java date and time API, for your time work. The class to use for time of day is LocalTime.

We don’t even need to specify a formatter for parsing your string since it is in the default format for a time of day (also laid down in the ISO 8601 standard).

    String current = "07:00:00.160";
    LocalTime time = LocalTime.parse(current);
    System.out.println(time);

Outout:

07:00:00.160

Links

Upvotes: 2

Related Questions