X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Futils%2FGlobals.java;h=0dfe3252bd33ad02ca33764519cbefb626f3d516;hb=ecca90e33d5334dd3a6750cb92f626bcff10a439;hp=e32a4321247261207e8286ae20ae309b49e55ee5;hpb=0bbdc409d82da31324c031f36607510f17d992e6;p=mobile-ledger.git diff --git a/app/src/main/java/net/ktnx/mobileledger/utils/Globals.java b/app/src/main/java/net/ktnx/mobileledger/utils/Globals.java index e32a4321..0dfe3252 100644 --- a/app/src/main/java/net/ktnx/mobileledger/utils/Globals.java +++ b/app/src/main/java/net/ktnx/mobileledger/utils/Globals.java @@ -1,5 +1,5 @@ /* - * Copyright © 2018 Damyan Ivanov. + * Copyright © 2019 Damyan Ivanov. * This file is part of Mobile-Ledger. * Mobile-Ledger is free software: you can distribute it and/or modify it * under the term of the GNU General Public License as published by @@ -23,13 +23,26 @@ 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 table_row_even_bg; + public static int tableRowLightBG; @ColorInt - public static int table_row_odd_bg; + public static int tableRowDarkBG; @ColorInt public static int primaryDark, defaultTextColor; + public static String[] monthNames; + private static SimpleDateFormat ledgerDateFormatter = + new SimpleDateFormat("yyyy/MM/dd", Locale.US); + private static Pattern reLedgerDate = + Pattern.compile("^(?:(\\d+)/)??(?:(\\d\\d?)/)?(\\d\\d?)$"); public static void hideSoftKeyboard(Activity act) { // hide the keyboard View v = act.getCurrentFocus(); @@ -39,5 +52,30 @@ public final class Globals { imm.hideSoftInputFromWindow(v.getWindowToken(), 0); } } + public static Date parseLedgerDate(String dateString) throws ParseException { + Matcher m = reLedgerDate.matcher(dateString); + if (!m.matches()) throw new ParseException(dateString, 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; +// Log.d("globals", String.format("Parsing date '%s'", toParse)); + return ledgerDateFormatter.parse(toParse); + } + public static String formatLedgerDate(Date date) { + return ledgerDateFormatter.format(date); + } } \ No newline at end of file