I had help setting up a function to take two strings and merge them into a date object.
Java - Take two strings and combine into a single date object
This has been working fine until it tries to parse 1st June, then i get the below error
Exception in thread "AWT-EventQueue-0" java.time.format.DateTimeParseException: Text '1st June' could not be parsed, unparsed text found at index 7
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1952)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
at java.time.LocalDate.parse(LocalDate.java:400)
at Timetable.ClassManager.parseDate(ClassManager.java:201)
at Timetable.GoogleAPI.loadClasses(GoogleAPI.java:133)
at Timetable.ClassManager.loadClasses(ClassManager.java:58)
The code for the function is
public LocalDateTime parseDate(String strDate, String strTime) {
strTime = strTime + ":00";
System.out.println("Date: " + strDate);
System.out.println("Time: " + strTime);
LocalDate date = LocalDate.now();
DateTimeFormatter dtfForDate = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.parseDefaulting(ChronoField.YEAR, date.getYear())
.appendPattern("d['th']['st']['rd']['nd'] MMM")
.toFormatter(Locale.ENGLISH);
DateTimeFormatter dtfForTime = DateTimeFormatter.ofPattern("H:m:s", Locale.ENGLISH);
LocalDateTime ldt = LocalDate.parse(strDate, dtfForDate)
.atTime(LocalTime.parse(strTime, dtfForTime));
System.out.println("Local Date Time: " + ldt);
return ldt;
}
The two prints give me
Date: 1st June
Time: 9:15:00
I would need to be able to handle both full month names and month abbreviations, i.e., March was set as Mar, April as Apr.
Use the following pattern which will cater to both, the three-letter abbreviated month names as well as the full month names:
.appendPattern("d['th']['st']['rd']['nd'] [MMMM][MMM]")
Demo:
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
System.out.println(parseDate("1st June", "9:15:00"));
System.out.println(parseDate("1st Jun", "9:15:00"));
}
public static LocalDateTime parseDate(String strDate, String strTime) {
LocalDate date = LocalDate.now();
DateTimeFormatter dtfForDate = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.parseDefaulting(ChronoField.YEAR, date.getYear())
.appendPattern("d['th']['st']['rd']['nd'] [MMMM][MMM]")
.toFormatter(Locale.ENGLISH);
DateTimeFormatter dtfForTime = DateTimeFormatter.ofPattern("H:m:s", Locale.ENGLISH);
LocalDateTime ldt = LocalDate.parse(strDate, dtfForDate).atTime(LocalTime.parse(strTime, dtfForTime));
return ldt;
}
}
Output:
2021-06-01T09:15
2021-06-01T09:15
Update
This update addresses the following concern raised by Meno Hochschild:
Personally, I don't like the misuse of optional sections here.
Negative example: "1th Jun" or "2st Jun" would also be successfully
parsed but should not.
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
public class Main {
public static void main(String[] args) {
System.out.println(parseDate("1st June", "9:15:00"));
System.out.println(parseDate("1st Jun", "9:15:00"));
}
public static LocalDateTime parseDate(String strDate, String strTime) {
LocalDate date = LocalDate.now();
DateTimeFormatter dtfForDate = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.parseDefaulting(ChronoField.YEAR, date.getYear())
.appendText(ChronoField.DAY_OF_MONTH, ordinalMap())
.appendPattern(" [MMMM][MMM]")
.toFormatter(Locale.ENGLISH);
DateTimeFormatter dtfForTime = DateTimeFormatter.ofPattern("H:m:s", Locale.ENGLISH);
LocalDateTime ldt = LocalDate.parse(strDate, dtfForDate).atTime(LocalTime.parse(strTime, dtfForTime));
return ldt;
}
static Map<Long, String> ordinalMap() {
String[] suffix = { "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th" };
Map<Long, String> map = new HashMap<>();
for (int i = 1; i <= 31; i++)
map.put((long)i, String.valueOf(i) + suffix[(i > 3 && i < 21) ? 0 : (i % 10)]);
return map;
}
}
Output:
2021-06-01T09:15
2021-06-01T09:15
Courtesy: The logic to build the Map is based on this excellent answer.
Related
I am getting an Assertion error " Body Content Expected child but was null when asserting the andExpect XML. If I input as as a String "2020-10-01-5:00" it works fine but if I concatenate the date into a string like:
LocalDate startDate = LocalDate.now().minusDays(90);
String startDateLine = "<start-date>" + startDate + "-5:00</start-date>\n";
It throws the AssertionError. I have verified that the XML is correct before the call so I am unsure what about getting the date and converting to a string causes the test to fail.
Update
Do not add the offset string to the LocalDate string in order to convert it into an OffsetDateTime string. Shown below is the idiomatic way to convert a LocalDate to OffsetDateTime
LocalDate.of(2020, 10, 1)
.atStartOfDay()
.atOffset(ZoneOffset.of("-05:00"));
Demo:
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
public class Main {
public static void main(String[] args) {
LocalDate date = LocalDate.of(2020, 10, 1);
LocalDateTime ldt = date.atStartOfDay();
OffsetDateTime odt = ldt.atOffset(ZoneOffset.of("-05:00"));
System.out.println(odt);
}
}
Output:
2020-10-01T00:00-05:00
ONLINE DEMO
You can get the String representation of an OffsetDateTime using the function OffsetDateTime#toString e.g.
String strOdt = odt.toString();
Original answer
Change your input to have the timezone offset in the format HH:mm e.g. -05:00 so that it conforms to ISO 8601 standards.
Use DateTimeFormatterBuilder with .parseDefaulting(ChronoField.HOUR_OF_DAY, 0) to default the hour-of-day to 0.
Parse the given string to OffsetDateTime as it has timezone offset and OffsetDateTime is the best fit to represent Date-Time with timezone offset.
Demo:
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
DateTimeFormatter dtf =new DateTimeFormatterBuilder()
.appendPattern("u-M-d[H:m:s]XXX")
.parseDefaulting(ChronoField.HOUR_OF_DAY, 0)
.toFormatter(Locale.ENGLISH);
OffsetDateTime odt = OffsetDateTime.parse("2020-10-01-05:00", dtf);
System.out.println(odt);
}
}
Output:
2020-10-01T00:00-05:00
ONLINE DEMO
Notice the optional pattern inside a square bracket.
Learn more about the modern Date-Time API* from Trail: Date Time.
**I am trying to write the code for getting the date in required format , I have got the dates but how to add the required time with it ,
here I have
startDate - 1/08/2021 00:00:00 ,
EndDate - 20/08/2021 23:59:59 ,
increment days: 10
and the Expected output is :
05/08/2021 00:00:00 to 10/08/2021 23:59:59 , 11/08/2021 00:00:00 to 15/08/2021 23:59:59 ,
This is the Code which I was trying to write , any help is appreciated
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
public class DateTest {
public static List<LocalDate> getDaysBetweenDates(LocalDate startDate, LocalDate endDate, int interval) {
List<LocalDate> dates = new ArrayList<>();
while (endDate.isAfter(startDate)) {
dates.add(startDate);
startDate = startDate.plusDays(interval-1);
dates.add(startDate);
startDate = startDate.plusDays(1);
}
return dates;
}
public static void main(String[] args) {
int interval = 5;
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss",Locale.US);
List<LocalDate> daysBetweenDates = getDaysBetweenDates(LocalDate.parse("01-08-2021 00:00:00", formatter),
LocalDate.parse("20-08-2021 23:59:59", formatter), interval);
System.out.println(daysBetweenDates);
}
}
Here's an alternative that uses LocalDates only (OK, and LocalDateTimes internally):
public static void printDaysInPeriod(LocalDate start, LocalDate end, int interval) {
// provide some data structure that
Map<LocalDate, LocalDate> intervals = new TreeMap<LocalDate, LocalDate>();
// loop through the dates in the defined period
while (start.isBefore(end)) {
// use the interval as step
LocalDate intervalEnd = start.plusDays(interval);
// store the sub-interval in the data structure
intervals.put(start, intervalEnd);
// and rearrange "start" to be the day after the last sub-interval
start = intervalEnd.plusDays(1);
}
// provide a formatter that produces the desired output per datetime
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(
"dd/MM/uuuu HH:mm:ss"
);
// provide a data structure for the output parts (Strings here)
List<String> intervalOutput = new ArrayList<>();
// stream the sub-intervals
intervals.entrySet().forEach(e ->
// then produce the desired output per sub-interval and store it
intervalOutput.add(e.getKey().atStartOfDay()
.format(formatter)
+ " to "
+ e.getValue()
.atTime(LocalTime.MAX)
.format(formatter)));
// finally output the sub-interval Strings comma-separated
System.out.println(String.join(" , ", intervalOutput));
}
Using this method in a main, like this
public static void main(String[] args) {
// example dates defining an interval
String startInterval = "05/08/2021";
String endInterval = "15/08/2021";
// provide a parser that handles the format
DateTimeFormatter dateParser = DateTimeFormatter.ofPattern("dd/MM/uuuu");
// then parse the dates to LocalDates
LocalDate start = LocalDate.parse(startInterval, dateParser);
LocalDate end = LocalDate.parse(endInterval, dateParser);
// and use the method
printDaysInPeriod(start, end, 5);
}
produces the following output:
05/08/2021 00:00:00 to 10/08/2021 23:59:59 , 11/08/2021 00:00:00 to 16/08/2021 23:59:59
You changed your questions a few times and in the first reading, I thought that you have start and end Date-Times as String. Based on this understanding, I wrote this answer. However, the very next minute, deHaar posted this correct answer. I am leaving this answer here for someone who will be looking for a solution to this kind of requirement (i.e. with Date-Time as String).
You can do it in the following two simple steps:
Define separate DateTimeFormatter for the input and the output strings
Loop through the parse range of Date-Time.
Demo
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
String strStartDateTime = "1/08/2021 00:00:00";
String strEndDateTime = "20/08/2021 23:59:59";
DateTimeFormatter dtfInput = DateTimeFormatter.ofPattern("d/M/u H:m:s", Locale.ENGLISH);
LocalDateTime startDateTime = LocalDateTime.parse(strStartDateTime, dtfInput);
LocalDateTime endDateTime = LocalDateTime.parse(strEndDateTime, dtfInput);
DateTimeFormatter dtfOutput = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss", Locale.ENGLISH);
for (LocalDateTime ldt = startDateTime, nextDateTime = ldt.plusDays(10).minusSeconds(1); !ldt
.isAfter(endDateTime); ldt = ldt.plusDays(10), nextDateTime = ldt.plusDays(10).minusSeconds(1))
System.out.println(dtfOutput.format(ldt) + " - " + nextDateTime);
}
}
Output:
2021-08-01 00:00:00 - 2021-08-10T23:59:59
2021-08-11 00:00:00 - 2021-08-20T23:59:59
ONLINE DEMO
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.
Use the date-time API.
(The code should be self-explanatory.)
import java.time.LocalDateTime;
import java.time.Period;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
public class DateTest {
public static List<ZonedDateTime> getDaysBetweenDates(ZonedDateTime startDate, ZonedDateTime endDate, int interval) {
List<ZonedDateTime> dates = new ArrayList<>();
while (!startDate.isAfter(endDate)) {
dates.add(startDate);
if (Period.between(startDate.toLocalDate(), endDate.toLocalDate()).getDays() < interval) {
startDate = endDate;
}
else {
startDate = startDate.plusDays(interval);
}
dates.add(startDate);
startDate = startDate.plusDays(1);
}
return dates;
}
public static void main(String[] args) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss", Locale.ENGLISH);
List<ZonedDateTime> dates = getDaysBetweenDates(ZonedDateTime.of(LocalDateTime.parse("05/08/2021 00:00:00", formatter), ZoneId.systemDefault()),
ZonedDateTime.of(LocalDateTime.parse("15/08/2021 23:59:59", formatter), ZoneId.systemDefault()),
5);
for (int i = 0; i < dates.size(); i+=2) {
System.out.printf("%s to %s , ",
dates.get(i).format(formatter),
dates.get(i + 1).format(formatter));
}
}
}
Output when running above code as follows:
05/08/2021 00:00:00 to 10/08/2021 00:00:00 , 11/08/2021 00:00:00 to 15/08/2021 23:59:59 ,
So am parsing json and sometimes the string I receive which contains the date comes full(dd-mm-yyyy) , and sometimes I only receive yyyy which I dont seem to able to convert to date ,so if anyone can help
As per your business requirement, you can default the month and the day-of-month to the required value using DateTimeFormatterBuilder#parseDefaulting e.g. in the following code, I have defaulted the month and the day-of-month to that of today:
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.util.Locale;
class Main {
public static void main(String[] args) {
// Test
System.out.println(parseToDate("10-10-2020"));
System.out.println(parseToDate("2020"));
}
static LocalDate parseToDate(String str) {
LocalDate today = LocalDate.now();
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.appendPattern("[dd-MM-uuuu][uuuu]")
.parseDefaulting(ChronoField.MONTH_OF_YEAR, today.getMonthValue())
.parseDefaulting(ChronoField.DAY_OF_MONTH, today.getDayOfMonth())
.toFormatter(Locale.ENGLISH);
return LocalDate.parse(str, formatter);
}
}
Output:
2020-10-10
2020-12-12
Note: The pattern, [dd-MM-uuuu][uuuu] has two optional patterns, dd-MM-uuuu and uuuu.
I am trying to get a list of string dates in Scala for a given range. Is there a direct/shorter way to achieve this?
val format = "yyyMMdd"
val startDate = "20200101"
val endDate = "20200131"
Expected Output = List(2020101,20200102, ....., 20200131)
Ya...if you add dashes in the format you gave you can use LocalData to parse the date without using a date formatter to complicate it. e.g. yyyy-MM-dd
val startDate = LocalDate.parse("2020-01-01")
val endDate = LocalDate.parse("2020-01-31")
Then you can use java.time.LocalDate.datesUntil to generate the dates for you. After that there is some collection manipulation to do if you really want a List and some String manipulation to do for a date without dashes. This gets you most of the way.
startDate.datesUntil(endDate).collect(Collectors.toList()).asScala.map(date => date.toString)
List[String] = ArrayBuffer(2020-01-01, 2020-01-02, 2020-01-03, ...).toList
You can do it as follows:
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
// Tests
System.out.println(getDateList("20200101", "20200110"));
System.out.println(getDateList("20200101", "20200131"));
}
static List<String> getDateList(String strStartDate, String strEndDate) {
// List to be populated with the desired strings
List<String> result = new ArrayList<>();
// Formatter for the desired pattern
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
// Parse strings to LocalDate instances
LocalDate startDate = LocalDate.parse(strStartDate, formatter);
LocalDate endDate = LocalDate.parse(strEndDate, formatter);
// Loop starting with start date until end date with a step of one day
for (LocalDate date = startDate; !date.isAfter(endDate); date = date.plusDays(1)) {
result.add(date.format(formatter));
}
// Return the populated list
return result;
}
}
Output:
[20200101, 20200102, 20200103,..., 20200110]
[20200101, 20200102, 20200103,..., 20200131]
Solution using Java Stream API:
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
// Tests
System.out.println(getDateList("20200101", "20200110"));
System.out.println(getDateList("20200101", "20200131"));
}
static List<String> getDateList(String strStartDate, String strEndDate) {
// Formatter for the input and desired pattern
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
// Parse strings to LocalDate instances
LocalDate startDate = LocalDate.parse(strStartDate, formatter);
LocalDate endDate = LocalDate.parse(strEndDate, formatter);
return Stream.iterate(startDate, date -> date.plusDays(1))
.limit(ChronoUnit.DAYS.between(startDate, endDate.plusDays(1)))
.map(date -> date.format(formatter))
.collect(Collectors.toList());
}
}
I am trying to convert date 06-12-2015 02:10:10 PM from default zone to UTC using ZonedDateTime.
LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
ZonedDateTime utc = ZonedDateTime.of(localDateTime, ZoneOffset.UTC);
but utc returns 2015-12-06T14:10:10Z instead of 06-12-2015 09:10:10 AM
How can I convert date from default zone to UTC? The answer given here convert current time to UTC.
You can use ZonedDateTime.ofInstant(Instant, ZoneId) where the second parameter is UTC (the instant knows the local offset). Something like,
String source = "06-12-2015 02:10:10 PM";
String pattern = "MM-dd-yyyy hh:mm:ss a";
DateFormat sdf = new SimpleDateFormat(pattern);
try {
Date date = sdf.parse(source);
ZonedDateTime zdt = ZonedDateTime.ofInstant(date.toInstant(), ZoneId.of("UTC"));
System.out.println(zdt.format(DateTimeFormatter.ofPattern(pattern)));
} catch (ParseException e) {
e.printStackTrace();
}
And I get (corresponding to my local zone offset)
06-12-2015 06:10:10 PM
06-12-2015 02:10:10 PM in Pakistan = 06-12-2015 09:10:10 AM in UTC
There are many ways to do it.
Parse to LocalDateTime ➡️ Combine it with your timezone to get ZonedDateTime ➡️ Convert to Instant ➡️ Convert to ZonedDateTime using Instant#atZone and UTC timezone.
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
String strDateTime = "06-12-2015 02:10:10 PM";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MM-dd-uuuu hh:mm:ss a", Locale.ENGLISH);
LocalDateTime ldt = LocalDateTime.parse(strDateTime, dtf);
// Using ZoneId.of("Asia/Karachi") for the demo. Change it to
// ZoneId.systemDefault()
Instant instant = ldt.atZone(ZoneId.of("Asia/Karachi")).toInstant();
ZonedDateTime zdtUtc = instant.atZone(ZoneId.of("Etc/UTC"));
System.out.println(zdtUtc.format(dtf)); // 06-12-2015 09:10:10 AM
}
}
Parse to LocalDateTime ➡️ Combine it with your timezone to get ZonedDateTime ➡️ Convert to Instant ➡️ Convert to ZonedDateTime using ZonedDateTime#ofInstant and UTC timezone.
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
String strDateTime = "06-12-2015 02:10:10 PM";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MM-dd-uuuu hh:mm:ss a", Locale.ENGLISH);
LocalDateTime ldt = LocalDateTime.parse(strDateTime, dtf);
// Using ZoneId.of("Asia/Karachi") for the demo. Change it to
// ZoneId.systemDefault()
Instant instant = ldt.atZone(ZoneId.of("Asia/Karachi")).toInstant();
ZonedDateTime zdtUtc = ZonedDateTime.ofInstant(instant, ZoneId.of("Etc/UTC"));
System.out.println(zdtUtc.format(dtf)); // 06-12-2015 09:10:10 AM
}
}
Using ZonedDateTime#withZoneSameInstant:
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
String strDateTime = "06-12-2015 02:10:10 PM";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MM-dd-uuuu hh:mm:ss a", Locale.ENGLISH);
LocalDateTime ldt = LocalDateTime.parse(strDateTime, dtf);
// Using ZoneId.of("Asia/Karachi") for the demo. Change it to
// ZoneId.systemDefault()
ZonedDateTime zdtPak = ldt.atZone(ZoneId.of("Asia/Karachi"));
ZonedDateTime zdtUtc = zdtPak.withZoneSameInstant(ZoneId.of("Etc/UTC"));
System.out.println(zdtUtc.format(dtf)); // 06-12-2015 09:10:10 AM
}
}
Using DateTimeFormatter#withZone and ZonedDateTime#withZoneSameInstant:
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
String strDateTime = "06-12-2015 02:10:10 PM";
// Using ZoneId.of("Asia/Karachi") for the demo. Change it to
// ZoneId.systemDefault()
DateTimeFormatter dtfInput = DateTimeFormatter.ofPattern("M-d-u h:m:s a", Locale.ENGLISH)
.withZone(ZoneId.of("Asia/Karachi"));
ZonedDateTime zdtPak = ZonedDateTime.parse(strDateTime, dtfInput);
ZonedDateTime zdtUtc = zdtPak.withZoneSameInstant(ZoneId.of("Etc/UTC"));
DateTimeFormatter dtfOutput = DateTimeFormatter.ofPattern("MM-dd-uuuu hh:mm:ss a", Locale.ENGLISH);
System.out.println(zdtUtc.format(dtfOutput)); // 06-12-2015 09:10:10 AM
}
}
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.