]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/utils/Globals.java
global method for format dates in ISO format (y-m-d)
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / utils / Globals.java
index e20e75b9df126b5d23674cc5eb8125d69e03678e..56a88ceca9fa70ef8d4f4de91f76c0a16df869eb 100644 (file)
@@ -1,43 +1,54 @@
 /*
  * Copyright © 2019 Damyan Ivanov.
- * This file is part of Mobile-Ledger.
- * Mobile-Ledger is free software: you can distribute it and/or modify it
+ * This file is part of MoLe.
+ * MoLe is free software: you can distribute it and/or modify it
  * under the term of the GNU General Public License as published by
  * the Free Software Foundation, either version 3 of the License, or
  * (at your opinion), any later version.
  *
- * Mobile-Ledger is distributed in the hope that it will be useful,
+ * MoLe is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  * GNU General Public License terms for details.
  *
  * You should have received a copy of the GNU General Public License
- * along with Mobile-Ledger. If not, see <https://www.gnu.org/licenses/>.
+ * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
  */
 
 package net.ktnx.mobileledger.utils;
 
 import android.app.Activity;
 import android.content.Context;
-import android.support.annotation.ColorInt;
 import android.view.View;
 import android.view.inputmethod.InputMethodManager;
 
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
+import java.util.Calendar;
 import java.util.Date;
 import java.util.Locale;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 public final class Globals {
-    @ColorInt
-    public static int tableRowEvenBG;
-    @ColorInt
-    public static int tableRowOddBG;
-    @ColorInt
-    public static int primaryDark, defaultTextColor;
+    private static final ThreadLocal<SimpleDateFormat> dateFormatter =
+            new ThreadLocal<SimpleDateFormat>() {
+                @Override
+                protected SimpleDateFormat initialValue() {
+                    return new SimpleDateFormat("yyyy/MM/dd", Locale.US);
+                }
+            };
+    private static final ThreadLocal<SimpleDateFormat> isoDateFormatter =
+            new ThreadLocal<SimpleDateFormat>() {
+                @Override
+                protected SimpleDateFormat initialValue() {
+                    return new SimpleDateFormat("yyyy-MM-dd", Locale.US);
+                }
+            };
     public static String[] monthNames;
-    private static SimpleDateFormat ledgerDateFormatter =
-            new SimpleDateFormat("yyyy/MM/dd", Locale.US);
+    public static String developerEmail = "dam+mole-crash@ktnx.net";
+    private static Pattern reLedgerDate =
+            Pattern.compile("^(?:(\\d+)/)??(?:(\\d\\d?)/)?(\\d\\d?)$");
     public static void hideSoftKeyboard(Activity act) {
         // hide the keyboard
         View v = act.getCurrentFocus();
@@ -47,15 +58,37 @@ public final class Globals {
             imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
         }
     }
-    public static Date parseLedgerDate(String dateString) {
-        try {
-            return ledgerDateFormatter.parse(dateString);
-        }
-        catch (ParseException e) {
-            throw new RuntimeException(String.format("Error parsing date '%s'", dateString), e);
+    public static Date parseLedgerDate(String dateString) throws ParseException {
+        Matcher m = reLedgerDate.matcher(dateString);
+        if (!m.matches()) throw new ParseException(
+                String.format("'%s' does not match expected pattern '%s'", dateString,
+                        reLedgerDate.toString()), 0);
+
+        String year = m.group(1);
+        String month = m.group(2);
+        String day = m.group(3);
+
+        String toParse;
+        if (year == null) {
+            Calendar now = Calendar.getInstance();
+            int thisYear = now.get(Calendar.YEAR);
+            if (month == null) {
+                int thisMonth = now.get(Calendar.MONTH) + 1;
+                toParse = String.format(Locale.US, "%04d/%02d/%s", thisYear, thisMonth, dateString);
+            }
+            else toParse = String.format(Locale.US, "%04d/%s", thisYear, dateString);
         }
+        else toParse = dateString;
+
+        return dateFormatter.get().parse(toParse);
+    }
+    public static Date parseIsoDate(String dateString) throws ParseException {
+        return isoDateFormatter.get().parse(dateString);
     }
     public static String formatLedgerDate(Date date) {
-        return ledgerDateFormatter.format(date);
+        return dateFormatter.get().format(date);
+    }
+    public static String formatIsoDate(Date date) {
+        return isoDateFormatter.get().format(date);
     }
 }
\ No newline at end of file