1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package ubic.basecode.util;
20
21 import java.text.ParseException;
22 import java.text.SimpleDateFormat;
23 import java.util.Calendar;
24 import java.util.Collection;
25 import java.util.Date;
26 import java.util.regex.Matcher;
27 import java.util.regex.Pattern;
28
29 import org.apache.commons.lang3.time.DateUtils;
30
31
32
33
34
35
36
37
38
39 public class DateUtil {
40
41
42 private static String datePattern = "MM/dd/yyyy";
43
44
45
46
47
48
49
50
51
52
53 public static final String convertDateToString( Date aDate ) {
54 return getDateTime( datePattern, aDate );
55 }
56
57
58
59
60
61
62
63
64
65
66 public static final Date convertStringToDate( String aMask, String strDate ) throws ParseException {
67 SimpleDateFormat df = null;
68 Date date = null;
69 df = new SimpleDateFormat( aMask );
70
71 try {
72 date = df.parse( strDate );
73 } catch ( ParseException pe ) {
74 throw new ParseException( pe.getMessage(), pe.getErrorOffset() );
75 }
76
77 return ( date );
78 }
79
80
81
82
83
84
85
86
87
88 public static final String getDateTime( String aMask, Date aDate ) {
89 SimpleDateFormat df = null;
90 String returnValue = "";
91
92 if ( aDate != null ) {
93 df = new SimpleDateFormat( aMask );
94 returnValue = df.format( aDate );
95 }
96
97 return returnValue;
98 }
99
100
101
102
103
104
105
106
107
108
109
110 public static Date getRelativeDate( Date date, String dateString ) {
111
112 if ( date == null ) throw new IllegalArgumentException( "Null date" );
113
114 Pattern pat = Pattern.compile( "([+-]?[0-9]+)([dmhMy])" );
115
116 Matcher match = pat.matcher( dateString );
117 boolean matches = match.matches();
118
119 if ( !matches ) {
120 throw new IllegalArgumentException( "Couldn't make sense of " + dateString
121 + ", please use something like -7d or -8h" );
122 }
123
124 int amount = Integer.parseInt( match.group( 1 ).replace( "+", "" ) );
125 String unit = match.group( 2 );
126
127 if ( unit.equals( "h" ) ) {
128 return DateUtils.addHours( date, amount );
129 } else if ( unit.equals( "m" ) ) {
130 return DateUtils.addMinutes( date, amount );
131 } else if ( unit.equals( "d" ) ) {
132 return DateUtils.addDays( date, amount );
133 } else if ( unit.equals( "y" ) ) {
134 return DateUtils.addYears( date, amount );
135 } else if ( unit.equals( "M" ) ) {
136 return DateUtils.addMonths( date, amount );
137 } else {
138 throw new IllegalArgumentException( "Couldn't make sense of units in " + dateString
139 + ", please use something like -7d or -8h" );
140 }
141
142 }
143
144
145
146
147
148
149
150 public static String getTodayDate( boolean changeDateformat ) {
151
152 String todayDate = convertDateToString( Calendar.getInstance().getTime() );
153
154
155 if ( changeDateformat ) {
156 todayDate = todayDate.replaceAll( "/", "-" );
157 }
158 return todayDate;
159 }
160
161
162
163
164
165
166
167 public static long numberOfSecondsBetweenDates( Collection<Date> dates ) {
168 if ( dates == null ) throw new IllegalArgumentException();
169 if ( dates.size() < 2 ) return 0;
170
171 Date max = DateUtils.addYears( new Date(), -500 );
172 Date min = DateUtils.addYears( new Date(), 500 );
173 for ( Date d : dates ) {
174 if ( d.before( min ) ) {
175 min = d;
176 }
177
178 if ( d.after( max ) ) {
179 max = d;
180 }
181 }
182 return Math.round( ( max.getTime() - min.getTime() ) / 1000.00 );
183 }
184 }