1
+ package org .android1liner .data ;
2
+
3
+
4
+ import java .text .SimpleDateFormat ;
5
+ import java .util .Calendar ;
6
+ import java .util .Date ;
7
+ import java .util .GregorianCalendar ;
8
+
9
+ public class DateUtils {
10
+ private DateUtils () {}
11
+
12
+ public static boolean isToday (long timeSinceEpoc ) {
13
+ return isToday (new Date (timeSinceEpoc ));
14
+ }
15
+
16
+ public static boolean isToday (Date date ) {
17
+ Date today = dateOnly (new Date ());
18
+ Date compare = dateOnly (date );
19
+ return (today .equals (compare ));
20
+ }
21
+
22
+ public static int year (Date date ) {
23
+ Calendar c = GregorianCalendar .getInstance ();
24
+ c .setTime (date );
25
+ return c .get (Calendar .YEAR );
26
+ }
27
+
28
+ public static int month (Date date ) {
29
+ Calendar c = GregorianCalendar .getInstance ();
30
+ c .setTime (date );
31
+ return c .get (Calendar .MONTH );
32
+ }
33
+
34
+ public static Date dateOnly (Date date ) {
35
+ Calendar c = GregorianCalendar .getInstance ();
36
+ c .setTime (date );
37
+ c .set (Calendar .HOUR , 0 );
38
+ c .set (Calendar .MINUTE , 0 );
39
+ c .set (Calendar .SECOND , 0 );
40
+ c .set (Calendar .MILLISECOND , 0 );
41
+ return c .getTime ();
42
+ }
43
+
44
+ public static Date endOfDay (Date date ) {
45
+ Calendar c = GregorianCalendar .getInstance ();
46
+ c .setTime (date );
47
+ c .set (Calendar .HOUR , 23 );
48
+ c .set (Calendar .MINUTE , 59 );
49
+ c .set (Calendar .SECOND , 59 );
50
+ c .set (Calendar .MILLISECOND , 9999 );
51
+ return c .getTime ();
52
+ }
53
+
54
+ public static SimpleDateFormat DDMMYYYY () {
55
+ return new SimpleDateFormat ("dd MMM yyyy" );
56
+ }
57
+
58
+ public static SimpleDateFormat DDMMYY_HHMM () {
59
+ return new SimpleDateFormat ("dd/MM/yy hh:mm a" );
60
+ }
61
+
62
+ /**
63
+ * Parse a date using SimpleDateFormat. Returns null instead of exception.
64
+ * @param format
65
+ * @param date
66
+ * @return
67
+ */
68
+ public static Date safeParse (SimpleDateFormat format , String date ) {
69
+ try {
70
+ return format .parse (date );
71
+ }
72
+ catch (Exception e ) {
73
+ return null ;
74
+ }
75
+ }
76
+
77
+ public static Date smartParse (String date ) {
78
+ if (date == null ) return null ;
79
+ Date d = null ;
80
+ d = safeParse (DDMMYY_HHMM (), date ); if (d != null ) return d ;
81
+ d = safeParse (DDMMYYYY (), date ); if (d != null ) return d ;
82
+ // TOOD: more parsing options
83
+ return null ;
84
+ }
85
+ }
0 commit comments