0
  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);

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `LocalTime` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Sep 11 '21 at 05:43
  • 1
    The result that you report is the one that I would have expected. Which result did you expect? – Ole V.V. Sep 11 '21 at 05:44
  • 1
    Looks correct. What’s the problem? – Boris the Spider Sep 11 '21 at 05:51
  • The `java.util` Date-Time API and their formatting API, `SimpleDateFormat` are outdated and error-prone. It is recommended to stop using them completely and switch to the [modern Date-Time API](https://www.oracle.com/technical-resources/articles/java/jf14-Date-Time.html). I strongly recommend you use the [solution by Ole V.V.](https://stackoverflow.com/a/69140388/10819573). – Arvind Kumar Avinash Sep 14 '21 at 16:37

1 Answers1

2

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

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161