2 * Copyright © 2020 Damyan Ivanov.
3 * This file is part of MoLe.
4 * MoLe is free software: you can distribute it and/or modify it
5 * under the term of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your opinion), any later version.
9 * MoLe is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License terms for details.
14 * You should have received a copy of the GNU General Public License
15 * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
18 package net.ktnx.mobileledger.utils;
20 import android.app.Activity;
21 import android.content.Context;
22 import android.view.View;
23 import android.view.inputmethod.InputMethodManager;
25 import java.text.ParseException;
26 import java.text.SimpleDateFormat;
27 import java.util.Calendar;
28 import java.util.Locale;
29 import java.util.regex.Matcher;
30 import java.util.regex.Pattern;
32 public final class Globals {
33 private static final ThreadLocal<SimpleDateFormat> dateFormatter =
34 new ThreadLocal<SimpleDateFormat>() {
36 protected SimpleDateFormat initialValue() {
37 return new SimpleDateFormat("yyyy/MM/dd", Locale.US);
40 private static final ThreadLocal<SimpleDateFormat> isoDateFormatter =
41 new ThreadLocal<SimpleDateFormat>() {
43 protected SimpleDateFormat initialValue() {
44 return new SimpleDateFormat("yyyy-MM-dd", Locale.US);
47 public static String[] monthNames;
48 public static final String developerEmail = "dam+mole-crash@ktnx.net";
49 private static final Pattern reLedgerDate =
50 Pattern.compile("^(?:(?:(\\d+)/)??(\\d\\d?)/)?(\\d\\d?)$");
51 public static void hideSoftKeyboard(Activity act) {
53 View v = act.getCurrentFocus();
55 InputMethodManager imm =
56 (InputMethodManager) act.getSystemService(Context.INPUT_METHOD_SERVICE);
57 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
60 public static SimpleDate parseLedgerDate(String dateString) throws ParseException {
61 Matcher m = reLedgerDate.matcher(dateString);
62 if (!m.matches()) throw new ParseException(
63 String.format("'%s' does not match expected pattern '%s'", dateString,
64 reLedgerDate.toString()), 0);
66 String yearStr = m.group(1);
67 String monthStr = m.group(2);
68 String dayStr = m.group(3);
73 if (yearStr == null) {
74 SimpleDate today = SimpleDate.today();
76 if (monthStr == null) {
79 else month = Integer.parseInt(monthStr);
82 year = Integer.parseInt(yearStr);
83 assert monthStr != null;
84 month = Integer.parseInt(monthStr);
87 assert dayStr != null;
88 day = Integer.parseInt(dayStr);
90 return new SimpleDate(year, month, day);
92 public static Calendar parseLedgerDateAsCalendar(String dateString) throws ParseException {
93 return parseLedgerDate(dateString).toCalendar();
95 public static SimpleDate parseIsoDate(String dateString) throws ParseException {
96 return SimpleDate.fromDate(isoDateFormatter.get().parse(dateString));
98 public static String formatLedgerDate(SimpleDate date) {
99 return dateFormatter.get().format(date.toDate());
101 public static String formatIsoDate(SimpleDate date) {
102 return isoDateFormatter.get().format(date.toDate());