-
Notifications
You must be signed in to change notification settings - Fork 80
/
DateUtil.java
317 lines (292 loc) · 10.5 KB
/
DateUtil.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
* This file is part of Grocy Android.
*
* Grocy Android is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Grocy Android 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 for more details.
*
* You should have received a copy of the GNU General Public License
* along with Grocy Android. If not, see http://www.gnu.org/licenses/.
*
* Copyright (c) 2020-2023 by Patrick Zedler and Dominic Zedler
*/
package xyz.zedler.patrick.grocy.util;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.WeekFields;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.concurrent.TimeUnit;
import xyz.zedler.patrick.grocy.Constants;
import xyz.zedler.patrick.grocy.Constants.DATE;
import xyz.zedler.patrick.grocy.Constants.PREF;
import xyz.zedler.patrick.grocy.R;
public class DateUtil {
private static final String TAG = DateUtil.class.getSimpleName();
public static final int FORMAT_LONG = 2;
public static final int FORMAT_MEDIUM = 1;
public static final int FORMAT_SHORT = 0;
public static final int FORMAT_SHORT_WITH_TIME = 3;
public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat(
"yyyy-MM-dd", Locale.ENGLISH
);
private static final SimpleDateFormat DATE_FORMAT_WITH_TIME = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss", Locale.ENGLISH
);
private static final SimpleDateFormat TIME_FORMAT = new SimpleDateFormat(
"HH:mm:ss", Locale.ENGLISH
);
private final Context context;
public DateUtil(Context context) {
this.context = context;
}
public static Date getDate(String dateString) {
if (dateString == null || dateString.isEmpty()) {
return null;
}
Date date = null;
try {
date = dateString.split(" ").length == 2
? DATE_FORMAT_WITH_TIME.parse(dateString)
: DATE_FORMAT.parse(dateString);
} catch (ParseException e) {
Log.e(TAG, "getDate: ");
}
return date;
}
public static String getDateStringToday() {
return DATE_FORMAT.format(getCurrentDate());
}
public static int getDaysFromNow(String dateString) {
Date date = getDate(dateString);
if (date == null) return 0;
long diff = date.getTime() - getCurrentDate().getTime();
return (int) TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS);
}
public static String getTodayWithDaysAdded(int daysToAdd) {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_MONTH, daysToAdd);
return DATE_FORMAT.format(calendar.getTime());
}
public static String getDateWithDaysAdded(String dateString, int daysToAdd) {
Date date = getDate(dateString);
if (date == null) return dateString;
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_MONTH, daysToAdd);
return DATE_FORMAT.format(calendar.getTime());
}
private static Date getCurrentDate() {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
}
public static Date getCurrentDateWithTime() {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
}
public String getCurrentDateWithTimeStr() {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.MILLISECOND, 0);
return DATE_FORMAT_WITH_TIME.format(cal.getTime());
}
public String getCurrentDateWithoutTimeStr() {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.MILLISECOND, 0);
return DATE_FORMAT.format(cal.getTime());
}
public boolean isTimeLessThanOneMinuteAway(String dateWithTimeStr) {
if (dateWithTimeStr == null) {
return true;
}
Date currentDateWithTime = getCurrentDateWithTime();
Date askedDateWithTime = null;
try {
askedDateWithTime = DATE_FORMAT_WITH_TIME.parse(dateWithTimeStr);
} catch (ParseException e) {
Log.e(TAG, "isTimeMoreThanOneMinuteAway: " + e);
}
if (askedDateWithTime == null) {
return true;
}
long diff = currentDateWithTime.getTime() - askedDateWithTime.getTime();
long secondsDiff = TimeUnit.SECONDS.convert(diff, TimeUnit.MILLISECONDS);
return Math.abs(secondsDiff) < 60;
}
public String getLocalizedDate(String dateString, int format) {
if (dateString == null || dateString.isEmpty()) {
return context.getString(R.string.date_unknown);
}
if (dateString.equals(DATE.NEVER_OVERDUE)) {
return context.getString(R.string.subtitle_never_overdue);
}
Date date = null;
try {
date = dateString.split(" ").length == 2
? DATE_FORMAT_WITH_TIME.parse(dateString)
: DATE_FORMAT.parse(dateString);
} catch (ParseException e) {
Log.e(TAG, "getLocalizedDate: " + e);
}
if (date == null) {
return "";
}
String localized;
if (format == FORMAT_LONG) {
localized = android.text.format.DateFormat.getLongDateFormat(context).format(date);
} else if (format == FORMAT_MEDIUM) {
localized = android.text.format.DateFormat.getMediumDateFormat(context).format(date);
} else if (format == FORMAT_SHORT) {
localized = android.text.format.DateFormat.getDateFormat(context).format(date);
} else {
localized = android.text.format.DateFormat.getDateFormat(context).format(date)
+ " " + android.text.format.DateFormat.getTimeFormat(context).format(date);
}
return localized;
}
public String getLocalizedDate(String dateString) {
return getLocalizedDate(dateString, FORMAT_LONG);
}
public String getLocalizedTime(String timeString) { // format is 00:00:00
if (timeString == null || timeString.isEmpty()) {
return context.getString(R.string.date_unknown);
}
Date date = null;
try {
date = TIME_FORMAT.parse(timeString);
} catch (ParseException e) {
Log.e(TAG, "getLocalizedTime: " + e);
}
if (date == null) {
return "";
}
return android.text.format.DateFormat.getTimeFormat(context).format(date);
}
public String getHumanForDaysFromNow(String dateString) {
if (dateString == null || dateString.isEmpty()) {
return context.getString(R.string.date_unknown);
} else if (dateString.split(" ").length == 2
? dateString.equals(DATE.NEVER_OVERDUE_WITH_TIME)
: dateString.equals(Constants.DATE.NEVER_OVERDUE)
) {
return context.getString(R.string.date_never);
} else {
return getHumanFromToday(getDaysFromNow(dateString));
}
}
public String getHumanFromToday(int days) {
if (days == 0) {
return context.getString(R.string.date_today);
} else if (days > 0) {
if (days < 30) {
return context.getResources().getQuantityString(
R.plurals.date_days_from_now,
days, days
);
} else if (days < 365) {
return context.getResources().getQuantityString(
R.plurals.date_months_from_now,
days / 30, days / 30
);
} else {
return context.getResources().getQuantityString(
R.plurals.date_years_from_now,
days / 365, days / 365
);
}
} else {
if (days > -30) {
return context.getResources().getQuantityString(
R.plurals.date_days_ago,
days * -1, days * -1
);
}
if (days > -365) {
return context.getResources().getQuantityString(
R.plurals.date_months_ago,
days * -1 / 30, days * -1 / 30
);
} else {
return context.getResources().getQuantityString(
R.plurals.date_years_ago,
days * -1 / 365, days * -1 / 365
);
}
}
}
public String getHumanDuration(int days) {
if (days == 0) {
return context.getString(R.string.date_never);
} else if (days > 0) {
if (days < 30) {
return context.getResources().getQuantityString(R.plurals.date_days, days, days);
}
if (days < 365) {
int months = Math.round((float) days / 30);
return context.getResources().getQuantityString(
R.plurals.date_months, months, months
);
} else {
// Check if days are about the same as to the never expiring date
Calendar calendarNever = Calendar.getInstance();
try {
Date dateNever = DATE_FORMAT.parse(Constants.DATE.NEVER_OVERDUE);
if (dateNever != null) {
calendarNever.setTime(dateNever);
}
} catch (ParseException e) {
Log.i(TAG, "getHumanDuration: " + e);
}
long msDiff = calendarNever.getTime().getTime() - getCurrentDate().getTime();
long daysToNever = TimeUnit.DAYS.convert(msDiff, TimeUnit.MILLISECONDS);
if (days >= daysToNever - 100) {
// deviation in server calculation possible
return context.getString(R.string.date_unlimited);
} else {
int years = Math.round((float) days / 365);
return context.getResources().getQuantityString(
R.plurals.date_years, years, years
);
}
}
} else {
return context.getString(R.string.date_unknown);
}
}
public static DayOfWeek getCalendarFirstDayOfWeek(SharedPreferences sharedPrefs) {
String pref = sharedPrefs.getString(PREF.CALENDAR_FIRST_DAY_OF_WEEK, "");
if (NumUtil.isStringNum(pref)) {
int grocyDayInt = Integer.parseInt(pref);
return DayOfWeek.of(grocyDayInt > 0 ? grocyDayInt : 7);
} else {
return WeekFields.of(Locale.getDefault()).getFirstDayOfWeek();
}
}
public static DayOfWeek getMealPlanFirstDayOfWeek(SharedPreferences sharedPrefs) {
String pref = sharedPrefs.getString(PREF.MEAL_PLAN_FIRST_DAY_OF_WEEK, "");
if (NumUtil.isStringNum(pref)) {
int grocyDayInt = Integer.parseInt(pref);
return grocyDayInt != -1
? DayOfWeek.of(grocyDayInt > 0 ? grocyDayInt : 7)
: DayOfWeek.of(LocalDate.now().getDayOfWeek().getValue());
} else {
return getCalendarFirstDayOfWeek(sharedPrefs);
}
}
}