Reputation: 1
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
Reputation: 86324
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
Upvotes: 2