MinSdk down to 23, replace ZonedDateTime with Date

This commit is contained in:
akaessens 2020-08-30 21:40:14 +02:00
parent 76f56434e8
commit 8b5263db63
4 changed files with 36 additions and 36 deletions

View File

@ -6,7 +6,7 @@ android {
defaultConfig {
applicationId "com.akdev.nofbeventscraper"
minSdkVersion 26
minSdkVersion 23
targetSdkVersion 29
versionCode 7
versionName "0.3.1"

View File

@ -42,15 +42,16 @@ public class ScraperUnitTest {
FbScraper scraper = new FbScraper(null, "");
String exp = "2020-10-23T05:00+02:00";
String in = "2020-10-23T05:00:00+0200";
String act = scraper.toZonedDateTime(in).toString();
String in = "2020-01-01T12:00:00+0100";
String exp = "Mi., 01 Jan. 2020 12:00 MEZ";
String act = FbEvent.dateTimeToString(scraper.parseToDate(in));
assertEquals(exp, act);
exp = null;
exp = "";
in = "";
ZonedDateTime act2 = scraper.toZonedDateTime(in);
String act2 = FbEvent.dateTimeToString(scraper.parseToDate(in));
assertEquals(exp, act2);
}

View File

@ -1,8 +1,8 @@
package com.akdev.nofbeventscraper;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
/**
* Objects of this class store immutable information about
@ -12,14 +12,14 @@ public class FbEvent {
public final String url;
public final String name;
public final ZonedDateTime start_date;
public final ZonedDateTime end_date;
public final Date start_date;
public final Date end_date;
public final String description;
public final String location;
public final String image_url;
public FbEvent(String url, String name, ZonedDateTime start_date, ZonedDateTime end_date,
public FbEvent(String url, String name, Date start_date, Date end_date,
String description, String location, String image_url) {
this.url = url;
this.name = name;
@ -33,28 +33,27 @@ public class FbEvent {
/**
* Converts datetime to epoch.
*
* @param zoned_date_time ZonedDateTime object
* @param date Date object
* @return Event begin time in milliseconds from the epoch for calendar intent or null
*/
static Long dateTimeToEpoch(ZonedDateTime zoned_date_time) {
static Long dateTimeToEpoch(Date date) {
try {
return zoned_date_time.toEpochSecond() * 1000;
return date.getTime();
} catch (Exception e) {
return null;
}
}
/**
* Returns a RFC formatted String representation of a ZonedDateTime
* Returns a locally formatted String representation of a Date
*
* @param zoned_date_time
* @return RFC-1123 formatted String of zoned_date_time or empty String
* @param date
* @return locally formatted String of date or empty String
*/
static String dateTimeToString(ZonedDateTime zoned_date_time) {
static String dateTimeToString(Date date) {
try {
return DateTimeFormatter
.RFC_1123_DATE_TIME
.format(zoned_date_time);
SimpleDateFormat formatter = new SimpleDateFormat("E, dd MMM yyyy HH:mm z", Locale.getDefault());
return formatter.format(date);
} catch (Exception e) {
return "";
}

View File

@ -2,8 +2,6 @@ package com.akdev.nofbeventscraper;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.text.Editable;
import android.text.SpannableStringBuilder;
import androidx.preference.PreferenceManager;
@ -17,8 +15,9 @@ import java.lang.ref.WeakReference;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -122,21 +121,22 @@ public class FbScraper extends AsyncTask<Void, Void, Void> {
}
/**
* Parses a time string from the facebook event.
* Corrects format to ISO date time and parse into ZonedDateTime
* Parses a time string from the facebook event into a Date
*
* @param time_in time string from the event
* @return ZonedDateTime parsed from input or null
* @return Date parsed from input or null
*/
protected ZonedDateTime toZonedDateTime(String time_in) {
protected Date parseToDate(String time_in) {
try {
// time in is missing a : in the timezone offset
Editable editable = new SpannableStringBuilder(time_in);
String time_str = editable.insert(22, ":").toString();
//Editable editable = new SpannableStringBuilder(time_in);
//String time_str = editable.insert(22, ":").toString();
// parse e.g. 2011-12-03T10:15:30+01:00
return ZonedDateTime.parse(time_str, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
// parse e.g. 2011-12-03T10:15:30+0100
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.getDefault());
return sdf.parse(time_in);
} catch (Exception e) {
e.printStackTrace();
@ -207,8 +207,8 @@ public class FbScraper extends AsyncTask<Void, Void, Void> {
String name = readFromJson(reader, "name");
ZonedDateTime start_date = toZonedDateTime(readFromJson(reader, "startDate"));
ZonedDateTime end_date = toZonedDateTime(readFromJson(reader, "endDate"));
Date start_date = parseToDate(readFromJson(reader, "startDate"));
Date end_date = parseToDate(readFromJson(reader, "endDate"));
String description = fixDescriptionLinks(readFromJson(reader, "description"));
String location = fixLocation(readFromJson(reader, "location"));