From 38396f1d663378ca8b2426b6a53729a491f30a5c Mon Sep 17 00:00:00 2001 From: Ezerous Date: Thu, 3 Oct 2019 21:48:16 +0300 Subject: [PATCH] DateTime parsing fixes --- .../activities/main/recent/RecentAdapter.java | 2 +- .../utils/parsing/ThmmyDateTimeParser.java | 22 +++++++++++++++---- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentAdapter.java b/app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentAdapter.java index 8d66209e..ecfcf64c 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentAdapter.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentAdapter.java @@ -50,7 +50,7 @@ class RecentAdapter extends RecyclerView.Adapter { holder.mDateTimeView.setReferenceTime(Long.valueOf(dateTimeString)); } catch(NumberFormatException e){ - Timber.e(e, "Invalid number format."); + Timber.e(e, "Invalid number format: %s", dateTimeString); holder.mDateTimeView.setText(dateTimeString); } else diff --git a/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ThmmyDateTimeParser.java b/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ThmmyDateTimeParser.java index 9524e5fe..f5f1a0aa 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ThmmyDateTimeParser.java +++ b/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ThmmyDateTimeParser.java @@ -13,6 +13,8 @@ import org.joda.time.format.DateTimeFormatterBuilder; import org.joda.time.format.DateTimeParser; import java.util.Locale; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import gr.thmmy.mthmmy.base.BaseApplication; import timber.log.Timber; @@ -21,9 +23,9 @@ import timber.log.Timber; public class ThmmyDateTimeParser { private static final DateTimeParser[] parsers = { DateTimeFormat.forPattern("HH:mm:ss").getParser(), - DateTimeFormat.forPattern("HH:mm:ss a").getParser(), + DateTimeFormat.forPattern("KK:mm:ss a").getParser(), DateTimeFormat.forPattern("MMMM d, Y, HH:mm:ss").getParser(), - DateTimeFormat.forPattern("MMMM d, Y, HH:mm:ss a").getParser() + DateTimeFormat.forPattern("MMMM d, Y, KK:mm:ss a").getParser() }; private static final DateTimeFormatter formatter = new DateTimeFormatterBuilder() @@ -33,6 +35,8 @@ public class ThmmyDateTimeParser { private static final Locale greekLocale = Locale.forLanguageTag("el-GR"); private static final Locale englishLocale = Locale.forLanguageTag("en-US"); + private static final Pattern pattern = Pattern.compile("\\s(1[2-9]|2[0-3]:)"); + public static String convertToTimestamp(String thmmyDateTime){ DateTimeZone dtz; if(!BaseApplication.getInstance().getSessionManager().isLoggedIn()) @@ -41,16 +45,26 @@ public class ThmmyDateTimeParser { dtz = DateTimeZone.getDefault(); //Add today's date for the first two cases - if(Character.isDigit(thmmyDateTime.charAt(0))) + if(thmmyDateTime.charAt(2)==':') thmmyDateTime = (new DateTime()).toString("MMMM d, Y, ") + thmmyDateTime; + // For the stupid format 23:54:12 pm + Matcher matcher = pattern.matcher(thmmyDateTime); + if (matcher.find()) + thmmyDateTime = thmmyDateTime.replaceAll("\\s(am|pm|π.μ.|α.μ.)",""); + + DateTime dateTime; try{ + thmmyDateTime = thmmyDateTime.replace("am","π.μ."); + thmmyDateTime = thmmyDateTime.replace("pm","μ.μ."); dateTime=formatter.withZone(dtz).withLocale(greekLocale).parseDateTime(thmmyDateTime); } catch (IllegalArgumentException e1){ - Timber.i("Parsing DateTime using Greek Locale failed."); + Timber.d("Parsing DateTime using Greek Locale failed."); try{ + thmmyDateTime = thmmyDateTime.replace("π.μ.","am"); + thmmyDateTime = thmmyDateTime.replace("μ.μ.","pm"); dateTime=formatter.withZone(dtz).withLocale(englishLocale).parseDateTime(thmmyDateTime); } catch (IllegalArgumentException e2){