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?
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.
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.
2
missing? It should have been2019-01-04T18:30:00.000Z
? Approximately two and a half years ago. - Ole V.V.00:00:00
(hardly any coincidence). - Ole V.V.