Skip to content

Commit 7acc87a

Browse files
committedFeb 24, 2019
Improves DateTimeUtils
1 parent f7dec1c commit 7acc87a

File tree

3 files changed

+207
-18
lines changed

3 files changed

+207
-18
lines changed
 

‎.idea/caches/gradle_models.ser

9.56 KB
Binary file not shown.

‎app/src/main/java/com/honzar/adtutils/sampleApp/MainActivity.java

+8-5
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
import android.os.Bundle;
44
import android.support.v7.app.AppCompatActivity;
5+
import android.widget.Toast;
56

6-
import com.honzar.adtutils.library.IntentUtils;
7+
import com.honzar.adtutils.library.DateTimeUtils;
78

89
public class MainActivity extends AppCompatActivity {
910

@@ -23,12 +24,12 @@ protected void onCreate(Bundle savedInstanceState) {
2324
// LogUtils.d(com.honzar.adtutils.sampleApp.BuildConfig.DEBUG, MainActivity.class.getName(), "test navigation bar height: " + ViewUtils.getSystemNavigationBarHeight(MainActivity.this));
2425
// LogUtils.d(com.honzar.adtutils.sampleApp.BuildConfig.DEBUG, MainActivity.class.getName(), "test status bar height: " + ViewUtils.getSystemStatusBarHeight(MainActivity.this));
2526
//
26-
// LogUtils.d(true, "test", DateTimeUtils.getFormattedDateAndTimeInWordsWithSystemLocale(MainActivity.this, new Date()));
27+
// LogUtils.d(true, "test", DateTimeUtils.getFormattedDateAndTimeInWords(MainActivity.this, new Date()));
2728
//
2829
// ViewUtils.keepScreenOn(MainActivity.this, true);
2930
//
30-
// LogUtils.d(true, "TEST DATE RANGE", DateTimeUtils.getFormattedDateAndTimeIntervalWithSystemLocale(MainActivity.this, new Date(1509959820000L), new Date(1510136220000L)));
31-
// LogUtils.d(true, "TEST DATE RANGE", DateTimeUtils.getFormattedDateAndTimeIntervalWithSystemLocale(MainActivity.this, new Date(1509963420000L), new Date(1509992820000L)));
31+
// LogUtils.d(true, "TEST DATE RANGE", DateTimeUtils.getFormattedDateAndTimeInterval(MainActivity.this, new Date(1509959820000L), new Date(1510136220000L)));
32+
// LogUtils.d(true, "TEST DATE RANGE", DateTimeUtils.getFormattedDateAndTimeInterval(MainActivity.this, new Date(1509963420000L), new Date(1509992820000L)));
3233
//
3334
// LogUtils.d(true, "TEST TIME DURATION", DateTimeUtils.getFormattedTimeDuration(MainActivity.this, 1510515505193L, true));
3435
// LogUtils.d(true, "TEST TIME DURATION", DateTimeUtils.getFormattedTimeDuration(MainActivity.this, 1510515505193L, false));
@@ -57,7 +58,9 @@ protected void onCreate(Bundle savedInstanceState) {
5758
// File file;
5859

5960

60-
IntentUtils.openAppPlayStoreSubscriptions(MainActivity.this);
61+
//IntentUtils.openAppPlayStoreSubscriptions(MainActivity.this);
62+
63+
Toast.makeText(this, DateTimeUtils.getFormattedJustDateDayAndMonth(this, System.currentTimeMillis()), Toast.LENGTH_SHORT).show();
6164

6265
}
6366

‎library/src/main/java/com/honzar/adtutils/library/DateTimeUtils.java

+199-13
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class DateTimeUtils extends Utils {
2222
private static SimpleDateFormat simpleISO8601DateWithMillisFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX", Locale.getDefault());
2323

2424

25-
// PARSE
25+
// DECODE & ENCODE
2626

2727
/**
2828
* Returns Date object parsed from date string or current date in case of error.
@@ -64,6 +64,42 @@ public static Date decodeDateWithMillisFromIsoString(String dateStr)
6464
}
6565
}
6666

67+
/**
68+
* Returns ISO 8601 string formatted from time
69+
*
70+
* @param millis
71+
*
72+
* @return ISO 8601 String or current date ISO String in case of error
73+
*/
74+
public static String encodeMillisToIsoString(long millis)
75+
{
76+
if (millis < 0) {
77+
return simpleISO8601DateFormatter.format(System.currentTimeMillis());
78+
}
79+
80+
try {
81+
return simpleISO8601DateFormatter.format(millis);
82+
} catch(RuntimeException rte) {
83+
return simpleISO8601DateFormatter.format(System.currentTimeMillis());
84+
}
85+
}
86+
87+
/**
88+
* Returns ISO 8601 string formatted from time
89+
*
90+
* @param date
91+
*
92+
* @return ISO 8601 String or current date ISO String in case of error
93+
*/
94+
public static String encodeMillisToIsoString(Date date)
95+
{
96+
if (date == null) {
97+
return simpleISO8601DateFormatter.format(System.currentTimeMillis());
98+
}
99+
100+
return encodeMillisToIsoString(date.getTime());
101+
}
102+
67103
// DATE AND TIME
68104

69105
/**
@@ -74,7 +110,7 @@ public static Date decodeDateWithMillisFromIsoString(String dateStr)
74110
*
75111
* @return formatted date and time or empty string in case of failure
76112
*/
77-
public static String getFormattedDateAndTimeWithSystemLocale(Context context, Date date)
113+
public static String getFormattedDateAndTime(Context context, Date date)
78114
{
79115
if (checkNull(context) || checkNull(date)) {
80116
return "";
@@ -83,6 +119,40 @@ public static String getFormattedDateAndTimeWithSystemLocale(Context context, Da
83119
return DateUtils.formatDateTime(context, date.getTime(), DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_NUMERIC_DATE | DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_24HOUR);
84120
}
85121

122+
/**
123+
* Returns formatted date and time according to system locale
124+
*
125+
* @param context
126+
* @param millis
127+
*
128+
* @return formatted date and time or empty string in case of failure
129+
*/
130+
public static String getFormattedDateAndTime(Context context, long millis)
131+
{
132+
if (checkNull(context) || millis < 0) {
133+
return "";
134+
}
135+
136+
return DateUtils.formatDateTime(context, millis, DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_NUMERIC_DATE | DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_24HOUR);
137+
}
138+
139+
/**
140+
* Returns formatted date and time with date in words and according to system locale
141+
*
142+
* @param context
143+
* @param millis
144+
*
145+
* @return formatted date and time or empty string in case of failure
146+
*/
147+
public static String getFormattedDateAndTimeInWords(Context context, long millis)
148+
{
149+
if (checkNull(context) || millis < 0) {
150+
return "";
151+
}
152+
153+
return DateUtils.formatDateTime(context, millis, DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_24HOUR);
154+
}
155+
86156
/**
87157
* Returns formatted date and time with date in words and according to system locale
88158
*
@@ -91,7 +161,7 @@ public static String getFormattedDateAndTimeWithSystemLocale(Context context, Da
91161
*
92162
* @return formatted date and time or empty string in case of failure
93163
*/
94-
public static String getFormattedDateAndTimeInWordsWithSystemLocale(Context context, Date date)
164+
public static String getFormattedDateAndTimeInWords(Context context, Date date)
95165
{
96166
if (checkNull(context) || checkNull(date)) {
97167
return "";
@@ -108,7 +178,7 @@ public static String getFormattedDateAndTimeInWordsWithSystemLocale(Context cont
108178
*
109179
* @return formatted time or empty string in case of failure
110180
*/
111-
public static String getFormattedJustTimeWithSystemLocale(Context context, Date date)
181+
public static String getFormattedJustTime(Context context, Date date)
112182
{
113183
if (checkNull(context) || checkNull(date)) {
114184
return "";
@@ -117,6 +187,23 @@ public static String getFormattedJustTimeWithSystemLocale(Context context, Date
117187
return DateUtils.formatDateTime(context, date.getTime(), DateUtils.FORMAT_SHOW_TIME);
118188
}
119189

190+
/**
191+
* Returns formatted time according to system locale
192+
*
193+
* @param context
194+
* @param millis
195+
*
196+
* @return formatted time or empty string in case of failure
197+
*/
198+
public static String getFormattedJustTime(Context context, long millis)
199+
{
200+
if (checkNull(context) || millis < 0) {
201+
return "";
202+
}
203+
204+
return DateUtils.formatDateTime(context, millis, DateUtils.FORMAT_SHOW_TIME);
205+
}
206+
120207
/**
121208
* Returns formatted date according to system locale
122209
*
@@ -125,7 +212,7 @@ public static String getFormattedJustTimeWithSystemLocale(Context context, Date
125212
*
126213
* @return formatted date or empty string in case of failure
127214
*/
128-
public static String getFormattedJustDateWithSystemLocale(Context context, Date date)
215+
public static String getFormattedJustDate(Context context, Date date)
129216
{
130217
if (checkNull(context) || checkNull(date)) {
131218
return "";
@@ -134,6 +221,57 @@ public static String getFormattedJustDateWithSystemLocale(Context context, Date
134221
return DateUtils.formatDateTime(context, date.getTime(), DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_NUMERIC_DATE);
135222
}
136223

224+
/**
225+
* Returns formatted date according to system locale
226+
*
227+
* @param context
228+
* @param millis
229+
*
230+
* @return formatted date or empty string in case of failure
231+
*/
232+
public static String getFormattedJustDate(Context context, long millis)
233+
{
234+
if (checkNull(context) || millis < 0) {
235+
return "";
236+
}
237+
238+
return DateUtils.formatDateTime(context, millis, DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_NUMERIC_DATE);
239+
}
240+
241+
/**
242+
* Returns formatted date (just day and month) according to system locale
243+
*
244+
* @param context
245+
* @param date
246+
*
247+
* @return formatted date or empty string in case of failure
248+
*/
249+
public static String getFormattedJustDateDayAndMonth(Context context, Date date)
250+
{
251+
if (checkNull(context) || checkNull(date)) {
252+
return "";
253+
}
254+
255+
return DateUtils.formatDateTime(context, date.getTime(), DateUtils.FORMAT_SHOW_DATE);
256+
}
257+
258+
/**
259+
* Returns formatted date (just day and month) according to system locale
260+
*
261+
* @param context
262+
* @param millis
263+
*
264+
* @return formatted date or empty string in case of failure
265+
*/
266+
public static String getFormattedJustDateDayAndMonth(Context context, long millis)
267+
{
268+
if (checkNull(context) || millis < 0) {
269+
return "";
270+
}
271+
272+
return DateUtils.formatDateTime(context, millis, DateUtils.FORMAT_SHOW_DATE);
273+
}
274+
137275
/**
138276
* Returns formatted date in words according to system locale
139277
*
@@ -142,7 +280,7 @@ public static String getFormattedJustDateWithSystemLocale(Context context, Date
142280
*
143281
* @return formatted date or empty string in case of failure
144282
*/
145-
public static String getFormattedJustDateInWordsWithSystemLocale(Context context, Date date)
283+
public static String getFormattedJustDateInWords(Context context, Date date)
146284
{
147285
if (checkNull(context) || checkNull(date)) {
148286
return "";
@@ -151,6 +289,23 @@ public static String getFormattedJustDateInWordsWithSystemLocale(Context context
151289
return DateUtils.formatDateTime(context, date.getTime(), DateUtils.FORMAT_SHOW_YEAR);
152290
}
153291

292+
/**
293+
* Returns formatted date in words according to system locale
294+
*
295+
* @param context
296+
* @param millis
297+
*
298+
* @return formatted date or empty string in case of failure
299+
*/
300+
public static String getFormattedJustDateInWords(Context context, long millis)
301+
{
302+
if (checkNull(context) || millis < 0) {
303+
return "";
304+
}
305+
306+
return DateUtils.formatDateTime(context, millis, DateUtils.FORMAT_SHOW_YEAR);
307+
}
308+
154309
/**
155310
* Returns formatted date in words according to system locale
156311
*
@@ -160,7 +315,7 @@ public static String getFormattedJustDateInWordsWithSystemLocale(Context context
160315
*
161316
* @return formatted date and time interval or empty string in case of failure
162317
*/
163-
public static String getFormattedDateAndTimeIntervalWithSystemLocale(Context context, Date dateStart, Date dateEnd)
318+
public static String getFormattedDateAndTimeInterval(Context context, Date dateStart, Date dateEnd)
164319
{
165320
if (checkNull(context) || checkNull(dateStart) || checkNull(dateEnd)) {
166321
return "";
@@ -184,20 +339,34 @@ public static String getFormattedDateAndTimeIntervalWithSystemLocale(Context con
184339

185340
String resultDate = "";
186341
if (startDateInMillis == endDateInMillis) {
187-
resultDate += getFormattedJustDateWithSystemLocale(context, dateStart);
342+
resultDate += getFormattedJustDate(context, dateStart);
188343
resultDate += " ";
189-
resultDate += getFormattedJustTimeWithSystemLocale(context, dateStart);
344+
resultDate += getFormattedJustTime(context, dateStart);
190345
resultDate += " - ";
191-
resultDate += getFormattedJustTimeWithSystemLocale(context, dateEnd);
346+
resultDate += getFormattedJustTime(context, dateEnd);
192347
} else {
193-
resultDate += getFormattedDateAndTimeWithSystemLocale(context, dateStart);
348+
resultDate += getFormattedDateAndTime(context, dateStart);
194349
resultDate += " - ";
195-
resultDate += getFormattedDateAndTimeWithSystemLocale(context, dateEnd);
350+
resultDate += getFormattedDateAndTime(context, dateEnd);
196351
}
197352

198353
return resultDate;
199354
}
200355

356+
/**
357+
* Returns formatted date in words according to system locale
358+
*
359+
* @param context
360+
* @param millisStart
361+
* @param millisEnd
362+
*
363+
* @return formatted date and time interval or empty string in case of failure
364+
*/
365+
public static String getFormattedDateAndTimeInterval(Context context, long millisStart, long millisEnd)
366+
{
367+
return getFormattedDateAndTimeInterval(context, millisStart, millisEnd);
368+
}
369+
201370
/**
202371
* Returns formatted time duration
203372
*
@@ -212,7 +381,7 @@ public static String getFormattedTimeDuration(Context context, long milliseconds
212381
return "";
213382
}
214383

215-
StringBuilder builder = new StringBuilder("");
384+
StringBuilder builder = new StringBuilder();
216385
final long hr = TimeUnit.MILLISECONDS.toHours(milliseconds);
217386
final long min = TimeUnit.MILLISECONDS.toMinutes(milliseconds) % 60;
218387
final long sec = TimeUnit.MILLISECONDS.toSeconds(milliseconds) % 60;
@@ -236,4 +405,21 @@ public static String getFormattedTimeDuration(Context context, long milliseconds
236405

237406
return builder.toString();
238407
}
408+
409+
/**
410+
* Returns formatted time duration
411+
*
412+
* @param context
413+
* @param time
414+
*
415+
* @return formatted time duration or empty string in case of failure
416+
*/
417+
public static String getFormattedTimeDuration(Context context, Date time, boolean withMilliseconds)
418+
{
419+
if (checkNull(context) || checkNull(time)) {
420+
return "";
421+
}
422+
423+
return getFormattedTimeDuration(context, time.getTime(), withMilliseconds);
424+
}
239425
}

0 commit comments

Comments
 (0)
Please sign in to comment.