Bartek Nowak Bartek Nowak - 2 months ago 4x
Java Question

How to get specyfic part of String, date saved as string

I got the date and time of leaving bus form TFL(trasnport for London) API but I do not know how to perform actions on this date format

date format: 2016-08-14T14:52:28.6262963Z

I decided to get the specific part of string (14:52:28) so I can place it in date format. How can I do that, simple .replace() with removing todays date won't solve the issue as I would have to change code each day. I'm interested in extracting HH:mm:ss format.

I can only use Java standard libraries


The date is ISO-8601 compliant. To parse it:

 LocalTime time = LocalTime.parse("2016-08-14T14:52:28.6262963Z", DateTimeFormatter.ISO_DATE_TIME);

Then you can print it while ignoring nanoseconds:


Or use the convenient LocalTime object if you wish. You can also use LocalDateTime (without any changes in method calls) if you want the date, too.