Skip to content

Commit 8cbdec6

Browse files
authored
Merge pull request #417 from Isira-Seneviratne/Use_Java_8_Date_Time_API
Use the Java 8 Date/Time API.
2 parents 6cc50b5 + fcdfe7d commit 8cbdec6

16 files changed

Lines changed: 168 additions & 216 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ If you're using Gradle, you could add NewPipe Extractor as a dependency with the
1313
1. Add `maven { url 'https://jitpack.io' }` to the `repositories` in your `build.gradle`.
1414
2. Add `implementation 'com.github.TeamNewPipe:NewPipeExtractor:v0.20.1'`the `dependencies` in your `build.gradle`. Replace `v0.20.1` with the latest release.
1515

16+
**Note:** To use NewPipe Extractor in projects with a `minSdkVersion` below 26, [API desugaring](https://developer.android.com/studio/write/java8-support#library-desugaring) is required.
17+
1618
### Testing changes
1719

1820
To test changes quickly you can build the library locally. A good approach would be to add something like the following to your `settings.gradle`:

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ allprojects {
22
apply plugin: 'java-library'
33
apply plugin: 'maven'
44

5-
sourceCompatibility = 1.7
6-
targetCompatibility = 1.7
5+
sourceCompatibility = 1.8
6+
targetCompatibility = 1.8
77

88
version 'v0.20.2'
99
group 'com.github.TeamNewPipe'

extractor/src/main/java/org/schabi/newpipe/extractor/localization/DateWrapper.java

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,61 @@
33
import edu.umd.cs.findbugs.annotations.NonNull;
44

55
import java.io.Serializable;
6+
import java.time.OffsetDateTime;
7+
import java.time.ZoneOffset;
68
import java.util.Calendar;
9+
import java.util.GregorianCalendar;
710

811
/**
9-
* A wrapper class that provides a field to describe if the date is precise or just an approximation.
12+
* A wrapper class that provides a field to describe if the date/time is precise or just an approximation.
1013
*/
1114
public class DateWrapper implements Serializable {
12-
@NonNull private final Calendar date;
15+
@NonNull private final OffsetDateTime offsetDateTime;
1316
private final boolean isApproximation;
1417

15-
public DateWrapper(@NonNull Calendar date) {
16-
this(date, false);
18+
/**
19+
* @deprecated Use {@link #DateWrapper(OffsetDateTime)} instead.
20+
*/
21+
@Deprecated
22+
public DateWrapper(@NonNull Calendar calendar) {
23+
this(calendar, false);
24+
}
25+
26+
/**
27+
* @deprecated Use {@link #DateWrapper(OffsetDateTime, boolean)} instead.
28+
*/
29+
@Deprecated
30+
public DateWrapper(@NonNull Calendar calendar, boolean isApproximation) {
31+
offsetDateTime = OffsetDateTime.ofInstant(calendar.toInstant(), ZoneOffset.UTC);
32+
this.isApproximation = isApproximation;
1733
}
1834

19-
public DateWrapper(@NonNull Calendar date, boolean isApproximation) {
20-
this.date = date;
35+
public DateWrapper(@NonNull OffsetDateTime offsetDateTime) {
36+
this(offsetDateTime, false);
37+
}
38+
39+
public DateWrapper(@NonNull OffsetDateTime offsetDateTime, boolean isApproximation) {
40+
this.offsetDateTime = offsetDateTime.withOffsetSameInstant(ZoneOffset.UTC);
2141
this.isApproximation = isApproximation;
2242
}
2343

2444
/**
25-
* @return the wrapped date.
45+
* @return the wrapped date/time as a {@link Calendar}.
46+
*
47+
* @deprecated use {@link #offsetDateTime()} instead.
2648
*/
49+
@Deprecated
2750
@NonNull
2851
public Calendar date() {
29-
return date;
52+
return GregorianCalendar.from(offsetDateTime.toZonedDateTime());
53+
}
54+
55+
/**
56+
* @return the wrapped date/time.
57+
*/
58+
@NonNull
59+
public OffsetDateTime offsetDateTime() {
60+
return offsetDateTime;
3061
}
3162

3263
/**

extractor/src/main/java/org/schabi/newpipe/extractor/localization/TimeAgoParser.java

Lines changed: 23 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
import org.schabi.newpipe.extractor.exceptions.ParsingException;
44
import org.schabi.newpipe.extractor.timeago.PatternsHolder;
5-
import org.schabi.newpipe.extractor.timeago.TimeAgoUnit;
65
import org.schabi.newpipe.extractor.utils.Parser;
76

8-
import java.util.Calendar;
7+
import java.time.OffsetDateTime;
8+
import java.time.ZoneOffset;
9+
import java.time.temporal.ChronoUnit;
910
import java.util.Collection;
1011
import java.util.Map;
1112
import java.util.regex.Pattern;
@@ -16,7 +17,7 @@
1617
*/
1718
public class TimeAgoParser {
1819
private final PatternsHolder patternsHolder;
19-
private final Calendar consistentNow;
20+
private final OffsetDateTime now;
2021

2122
/**
2223
* Creates a helper to parse upload dates in the format '2 days ago'.
@@ -28,7 +29,7 @@ public class TimeAgoParser {
2829
*/
2930
public TimeAgoParser(PatternsHolder patternsHolder) {
3031
this.patternsHolder = patternsHolder;
31-
consistentNow = Calendar.getInstance();
32+
now = OffsetDateTime.now(ZoneOffset.UTC);
3233
}
3334

3435
/**
@@ -42,14 +43,14 @@ public TimeAgoParser(PatternsHolder patternsHolder) {
4243
* @throws ParsingException if the time unit could not be recognized
4344
*/
4445
public DateWrapper parse(String textualDate) throws ParsingException {
45-
for (Map.Entry<TimeAgoUnit, Map<String, Integer>> caseUnitEntry : patternsHolder.specialCases().entrySet()) {
46-
final TimeAgoUnit timeAgoUnit = caseUnitEntry.getKey();
46+
for (Map.Entry<ChronoUnit, Map<String, Integer>> caseUnitEntry : patternsHolder.specialCases().entrySet()) {
47+
final ChronoUnit chronoUnit = caseUnitEntry.getKey();
4748
for (Map.Entry<String, Integer> caseMapToAmountEntry : caseUnitEntry.getValue().entrySet()) {
4849
final String caseText = caseMapToAmountEntry.getKey();
4950
final Integer caseAmount = caseMapToAmountEntry.getValue();
5051

5152
if (textualDateMatches(textualDate, caseText)) {
52-
return getResultFor(caseAmount, timeAgoUnit);
53+
return getResultFor(caseAmount, chronoUnit);
5354
}
5455
}
5556
}
@@ -63,22 +64,22 @@ public DateWrapper parse(String textualDate) throws ParsingException {
6364
timeAgoAmount = 1;
6465
}
6566

66-
final TimeAgoUnit timeAgoUnit = parseTimeAgoUnit(textualDate);
67-
return getResultFor(timeAgoAmount, timeAgoUnit);
67+
final ChronoUnit chronoUnit = parseChronoUnit(textualDate);
68+
return getResultFor(timeAgoAmount, chronoUnit);
6869
}
6970

7071
private int parseTimeAgoAmount(String textualDate) throws NumberFormatException {
7172
String timeValueStr = textualDate.replaceAll("\\D+", "");
7273
return Integer.parseInt(timeValueStr);
7374
}
7475

75-
private TimeAgoUnit parseTimeAgoUnit(String textualDate) throws ParsingException {
76-
for (Map.Entry<TimeAgoUnit, Collection<String>> entry : patternsHolder.asMap().entrySet()) {
77-
final TimeAgoUnit timeAgoUnit = entry.getKey();
76+
private ChronoUnit parseChronoUnit(String textualDate) throws ParsingException {
77+
for (Map.Entry<ChronoUnit, Collection<String>> entry : patternsHolder.asMap().entrySet()) {
78+
final ChronoUnit chronoUnit = entry.getKey();
7879

7980
for (String agoPhrase : entry.getValue()) {
8081
if (textualDateMatches(textualDate, agoPhrase)) {
81-
return timeAgoUnit;
82+
return chronoUnit;
8283
}
8384
}
8485
}
@@ -112,65 +113,35 @@ private boolean textualDateMatches(String textualDate, String agoPhrase) {
112113
}
113114
}
114115

115-
private DateWrapper getResultFor(int timeAgoAmount, TimeAgoUnit timeAgoUnit) {
116-
final Calendar calendarTime = getNow();
116+
private DateWrapper getResultFor(int timeAgoAmount, ChronoUnit chronoUnit) {
117+
OffsetDateTime offsetDateTime = now;
117118
boolean isApproximation = false;
118119

119-
switch (timeAgoUnit) {
120+
switch (chronoUnit) {
120121
case SECONDS:
121-
calendarTime.add(Calendar.SECOND, -timeAgoAmount);
122-
break;
123-
124122
case MINUTES:
125-
calendarTime.add(Calendar.MINUTE, -timeAgoAmount);
126-
break;
127-
128123
case HOURS:
129-
calendarTime.add(Calendar.HOUR_OF_DAY, -timeAgoAmount);
124+
offsetDateTime = offsetDateTime.minus(timeAgoAmount, chronoUnit);
130125
break;
131126

132127
case DAYS:
133-
calendarTime.add(Calendar.DAY_OF_MONTH, -timeAgoAmount);
134-
isApproximation = true;
135-
break;
136-
137128
case WEEKS:
138-
calendarTime.add(Calendar.WEEK_OF_YEAR, -timeAgoAmount);
139-
isApproximation = true;
140-
break;
141-
142129
case MONTHS:
143-
calendarTime.add(Calendar.MONTH, -timeAgoAmount);
130+
offsetDateTime = offsetDateTime.minus(timeAgoAmount, chronoUnit);
144131
isApproximation = true;
145132
break;
146133

147134
case YEARS:
148-
calendarTime.add(Calendar.YEAR, -timeAgoAmount);
149-
// Prevent `PrettyTime` from showing '12 months ago'.
150-
calendarTime.add(Calendar.DAY_OF_MONTH, -1);
135+
// minusDays is needed to prevent `PrettyTime` from showing '12 months ago'.
136+
offsetDateTime = offsetDateTime.minusYears(timeAgoAmount).minusDays(1);
151137
isApproximation = true;
152138
break;
153139
}
154140

155141
if (isApproximation) {
156-
markApproximatedTime(calendarTime);
142+
offsetDateTime = offsetDateTime.truncatedTo(ChronoUnit.HOURS);
157143
}
158144

159-
return new DateWrapper(calendarTime, isApproximation);
160-
}
161-
162-
private Calendar getNow() {
163-
return (Calendar) consistentNow.clone();
164-
}
165-
166-
/**
167-
* Marks the time as approximated by setting minutes, seconds and milliseconds to 0.
168-
*
169-
* @param calendarTime Time to be marked as approximated
170-
*/
171-
private void markApproximatedTime(Calendar calendarTime) {
172-
calendarTime.set(Calendar.MINUTE, 0);
173-
calendarTime.set(Calendar.SECOND, 0);
174-
calendarTime.set(Calendar.MILLISECOND, 0);
145+
return new DateWrapper(offsetDateTime, isApproximation);
175146
}
176147
}

extractor/src/main/java/org/schabi/newpipe/extractor/services/media_ccc/extractors/MediaCCCParsingHelper.java

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,17 @@
22

33
import org.schabi.newpipe.extractor.exceptions.ParsingException;
44

5-
import java.text.ParseException;
6-
import java.text.SimpleDateFormat;
7-
import java.util.Calendar;
8-
import java.util.Date;
9-
import java.util.TimeZone;
5+
import java.time.OffsetDateTime;
6+
import java.time.format.DateTimeParseException;
107

118
public final class MediaCCCParsingHelper {
129
private MediaCCCParsingHelper() { }
1310

14-
public static Calendar parseDateFrom(final String textualUploadDate) throws ParsingException {
15-
final Date date;
11+
public static OffsetDateTime parseDateFrom(final String textualUploadDate) throws ParsingException {
1612
try {
17-
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
18-
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
19-
date = sdf.parse(textualUploadDate);
20-
} catch (ParseException e) {
13+
return OffsetDateTime.parse(textualUploadDate);
14+
} catch (DateTimeParseException e) {
2115
throw new ParsingException("Could not parse date: \"" + textualUploadDate + "\"", e);
2216
}
23-
24-
final Calendar uploadDate = Calendar.getInstance();
25-
uploadDate.setTime(date);
26-
return uploadDate;
2717
}
28-
2918
}

extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/PeertubeParsingHelper.java

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.grack.nanojson.JsonArray;
44
import com.grack.nanojson.JsonObject;
5-
65
import org.schabi.newpipe.extractor.InfoItemsCollector;
76
import org.schabi.newpipe.extractor.Page;
87
import org.schabi.newpipe.extractor.exceptions.ContentNotAvailableException;
@@ -12,11 +11,10 @@
1211
import org.schabi.newpipe.extractor.utils.Parser;
1312
import org.schabi.newpipe.extractor.utils.Utils;
1413

15-
import java.text.ParseException;
16-
import java.text.SimpleDateFormat;
17-
import java.util.Calendar;
18-
import java.util.Date;
19-
import java.util.TimeZone;
14+
import java.time.Instant;
15+
import java.time.OffsetDateTime;
16+
import java.time.ZoneOffset;
17+
import java.time.format.DateTimeParseException;
2018

2119
public class PeertubeParsingHelper {
2220
public static final String START_KEY = "start";
@@ -34,19 +32,12 @@ public static void validate(final JsonObject json) throws ContentNotAvailableExc
3432
}
3533
}
3634

37-
public static Calendar parseDateFrom(final String textualUploadDate) throws ParsingException {
38-
final Date date;
35+
public static OffsetDateTime parseDateFrom(final String textualUploadDate) throws ParsingException {
3936
try {
40-
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.S'Z'");
41-
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
42-
date = sdf.parse(textualUploadDate);
43-
} catch (ParseException e) {
37+
return OffsetDateTime.ofInstant(Instant.parse(textualUploadDate), ZoneOffset.UTC);
38+
} catch (DateTimeParseException e) {
4439
throw new ParsingException("Could not parse date: \"" + textualUploadDate + "\"", e);
4540
}
46-
47-
final Calendar uploadDate = Calendar.getInstance();
48-
uploadDate.setTime(date);
49-
return uploadDate;
5041
}
5142

5243
public static Page getNextPage(final String prevPageUrl, final long total) {

extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudParsingHelper.java

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,18 @@
2121
import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector;
2222
import org.schabi.newpipe.extractor.utils.Parser;
2323
import org.schabi.newpipe.extractor.utils.Parser.RegexException;
24-
import org.schabi.newpipe.extractor.utils.Utils;
2524

2625
import javax.annotation.Nonnull;
2726
import java.io.IOException;
2827
import java.net.MalformedURLException;
2928
import java.net.URL;
3029
import java.net.URLEncoder;
31-
import java.text.ParseException;
32-
import java.text.SimpleDateFormat;
33-
import java.util.*;
30+
import java.time.OffsetDateTime;
31+
import java.time.format.DateTimeFormatter;
32+
import java.time.format.DateTimeParseException;
33+
import java.util.Collections;
34+
import java.util.HashMap;
35+
import java.util.List;
3436

3537
import static java.util.Collections.singletonList;
3638
import static org.schabi.newpipe.extractor.ServiceList.SoundCloud;
@@ -95,23 +97,16 @@ static boolean checkIfHardcodedClientIdIsValid() {
9597
}
9698
}
9799

98-
public static Calendar parseDateFrom(String textualUploadDate) throws ParsingException {
99-
Date date;
100+
public static OffsetDateTime parseDateFrom(String textualUploadDate) throws ParsingException {
100101
try {
101-
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
102-
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
103-
date = sdf.parse(textualUploadDate);
104-
} catch (ParseException e1) {
102+
return OffsetDateTime.parse(textualUploadDate);
103+
} catch (DateTimeParseException e1) {
105104
try {
106-
date = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss +0000").parse(textualUploadDate);
107-
} catch (ParseException e2) {
105+
return OffsetDateTime.parse(textualUploadDate, DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss +0000"));
106+
} catch (DateTimeParseException e2) {
108107
throw new ParsingException("Could not parse date: \"" + textualUploadDate + "\"" + ", " + e1.getMessage(), e2);
109108
}
110109
}
111-
112-
final Calendar uploadDate = Calendar.getInstance();
113-
uploadDate.setTime(date);
114-
return uploadDate;
115110
}
116111

117112
/**

0 commit comments

Comments
 (0)