I'm using an API that gives Start Time in this format 019-01-04T18:30:00.000Z, so how can I extract only the time from this start time format in java?
-
Let me guess, there’s a `2` missing? It should have been `2019-01-04T18:30:00.000Z`? Approximately two and a half years ago. – Ole V.V. Aug 08 '21 at 19:53
-
1Your string is in UTC (denoted by the trailing Z). Do you want the time of day in UTC, in your own time zone or some other time zone? Which one? Your profile says you’re in India, so the time in your time zone will be `00:00:00` (hardly any coincidence). – Ole V.V. Aug 08 '21 at 19:54
-
Welcome to Stack Overflow. Please learn that you are supposed to search before asking a question here and when you post a question, tell us what your search brought up and specify how it fell short of solving your problem. It’s for your own sake since (1) you often find a better answer faster that way (2) it allows us to give preciser and more focused answers. I downvoted because there is no research effort documented in your question (and I seriously wonder why anyone upvoted). – Ole V.V. Aug 08 '21 at 20:21
-
1@OleV.V. sorry for the inconvenience will keep that in mind in the future and also for the context I'm making an android application in java that is using a Rest API and it gives date/time in this format i.e "2021-08-24T14:35:00.000Z" I was able to extract the dd/mm/yy form this String using DateTimeFormatter class but was not able to extract the time from that string, I have even searched for the solution on different site but was not able to get the latest updated version of the codes. API LINK:-https://kontests.net/api – NEELANGSHU NATH Aug 10 '21 at 19:01
2 Answers
Your Date-Time string doesn't seem to be correct.
You need to parse your Date-Time string into a Date-Time object from which you can extract the time part.
Given below is a solution using java.time
, the modern Date-Time API:
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
String strDateTime = "019-01-04T18:30:00.000Z";
DateTimeFormatter dtfInput = DateTimeFormatter.ofPattern("u-M-d'T'H:m:s.SSSX", Locale.ENGLISH);
OffsetDateTime odt = OffsetDateTime.parse(strDateTime, dtfInput);
LocalTime time = odt.toLocalTime();
System.out.println(time);
// As a string
String strTime = time.toString();
System.out.println(strTime);
// As a string in a custom format
DateTimeFormatter dtfOutput = DateTimeFormatter.ofPattern("HH:mm:ss.SSS", Locale.ENGLISH);
String formattedTime = time.format(dtfOutput);
System.out.println(formattedTime);
}
}
Output:
18:30
18:30
18:30:00.000
Learn more about the modern Date-Time API* from Trail: Date Time.
* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

- 71,965
- 6
- 74
- 110
java.time through desugaring
Use java.time, the modern Java date and time API, for your time work. I can easily give you the code. I hope you won’t be disappointed about the result from it.
Your string identifies a point in time. At that point the time of day differs from time zone to time zone. So you need to decide in which time zone you want the time. For example:
ZoneId zone = ZoneId.of("Asia/Kolkata");
String startTimeString = "2019-01-04T18:30:00.000Z";
ZonedDateTime dateTime = Instant.parse(startTimeString).atZone(zone);
System.out.println("Date and time: " + dateTime);
LocalTime time = dateTime.toLocalTime();
System.out.println("Time: " + time);
Output:
Date and time: 2019-01-05T00:00+05:30[Asia/Kolkata] Time: 00:00
So your example string denotes the start of day in India. The trailing Z
in the string you got tells us that the string is in UTC, which is the standard for exchanging date and time data, especially across time zones. The call to atZone()
converts to your time zone.
If it’s true that you receive the string without the first digit, the 2
, first see if you can have that bug fixed. If you can’t, just add the 2
yourself. Unless you consider it possible that you may receive times from before year 2000 (or after 2999).
I am parsing your string without specifying any formatter. I can do this because your string is in the default format for an instant. The format is known as ISO 8601.
Question: Doesn’t java.time require Android API level 26?
java.time works nicely on both older and newer Android devices. It just requires at least Java 6.
- In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
- In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
- On older Android either use desugaring or the Android edition of ThreeTen Backport. It’s called ThreeTenABP. In the latter case make sure you import the date and time classes from
org.threeten.bp
with subpackages.
Links
- Oracle tutorial: Date Time explaining how to use java.time.
- Wikipedia article: ISO 8601
- Java Specification Request (JSR) 310, where
java.time
was first described. - ThreeTen Backport project, the backport of
java.time
to Java 6 and 7 (ThreeTen for JSR-310). - Java 8+ APIs available through desugaring
- ThreeTenABP, Android edition of ThreeTen Backport
- Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.

- 81,772
- 15
- 137
- 161