View Javadoc
1   package pl.matsuo.core.util;
2   
3   import com.fasterxml.jackson.databind.util.ISO8601DateFormat;
4   
5   import java.sql.Time;
6   import java.text.DateFormat;
7   import java.text.FieldPosition;
8   import java.text.Format;
9   import java.text.ParseException;
10  import java.text.ParsePosition;
11  import java.time.Instant;
12  import java.time.LocalDate;
13  import java.time.LocalDateTime;
14  import java.time.ZoneId;
15  import java.time.format.DateTimeFormatter;
16  import java.util.Calendar;
17  import java.util.Date;
18  import java.util.GregorianCalendar;
19  
20  import static java.util.Calendar.*;
21  
22  
23  @SuppressWarnings("deprecation")
24  public class DateUtil {
25  
26  
27    public static final DateFormat isoDateFormat = new ISO8601DateFormat();
28  
29  
30    public static final Format[] formats = new Format[] {
31        new DateFormat() {
32          @Override
33          public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition) {
34            toAppendTo.append("" + date.getTime());
35            return toAppendTo;
36          }
37  
38          @Override
39          public Date parse(String source, ParsePosition pos) {
40            Date date = new Date(Long.valueOf(source));
41            pos.setIndex(source.length() - 1);
42            return date;
43          }
44        },
45        isoDateFormat,
46        DateTimeFormatter.ISO_INSTANT.toFormat()
47    };
48  
49  
50    public static Date date(int year, int month, int date) {
51      return cal(year, month, date).getTime();
52    }
53  
54  
55    public static Date date(String source) {
56      if (source.startsWith("\"")) {
57        source = source.substring(1, source.length() - 1);
58      }
59  
60      for (Format format : formats) {
61        try {
62          return (Date) format.parseObject(source);
63        } catch (ParseException | NumberFormatException e) {
64        }
65      }
66  
67      throw new IllegalArgumentException("Could not parse " + source);
68    }
69  
70  
71    public static Date maybeDate(String source) {
72      for (Format format : formats) {
73        try {
74          return (Date) format.parseObject(source);
75        } catch (ParseException e) {
76        }
77      }
78  
79      return null;
80    }
81  
82  
83    public static Date date(int year, int month, int date, int hour, int minutes) {
84      return cal(year, month, date, hour, minutes).getTime();
85    }
86  
87  
88    public static Date date(Date date, Integer hour, Integer minutes) {
89      return cal(date, hour, minutes).getTime();
90    }
91  
92  
93    public static Date addTime(Date date, int field, int value) {
94      Calendar cal = cal(date);
95      cal.add(field, value);
96      return cal.getTime();
97    }
98  
99  
100   public static Calendar cal(Date date) {
101     if (date == null) {
102       return null;
103     } else {
104       return cal(date.getTime());
105     }
106   }
107 
108 
109   public static Calendar cal(Date date, Integer hour, Integer minutes) {
110     Calendar calendar = Calendar.getInstance();
111     calendar.setTime(date);
112     calendar.set(HOUR_OF_DAY, hour);
113     calendar.set(MINUTE, minutes);
114     calendar.set(SECOND, 0);
115     calendar.set(MILLISECOND, 0);
116     return calendar;
117   }
118 
119 
120   public static Calendar cal(long millis) {
121     Calendar calendar = Calendar.getInstance();
122     calendar.setTimeInMillis(millis);
123     return calendar;
124   }
125 
126 
127   public static Calendar cal(int year, int month, int date) {
128     return new GregorianCalendar(year, month, date);
129   }
130 
131 
132   public static Calendar cal(int year, int month, int date, int hour, int minutes) {
133     return new GregorianCalendar(year, month, date, hour, minutes);
134   }
135 
136 
137   public static Time time(int hour, int minutes) {
138     return new Time(hour, minutes, 0);
139   }
140 
141 
142   public static Date dateAndTime(Date date, Time time) {
143     return date(date, time.getHours(), time.getMinutes());
144   }
145 
146 
147   public static Calendar min(Calendar cal1, Calendar cal2) {
148     return cal1.before(cal2) ? cal1 : cal2;
149   }
150 
151 
152   public static Calendar max(Calendar cal1, Calendar cal2) {
153     return cal1.after(cal2) ? cal1 : cal2;
154   }
155 
156 
157   public static Date min(Date date1, Date date2) {
158     return date1.before(date2) ? date1 : date2;
159   }
160 
161 
162   public static Date max(Date date1, Date date2) {
163     return date1.after(date2) ? date1 : date2;
164   }
165 
166 
167   public static boolean between(Date date, Date from, Date to) {
168     return (from == null || date.after(from)) && (to == null || date.before(to));
169   }
170 
171 
172   public static java.sql.Date sqlDate(Date date) {
173     return new java.sql.Date(date.getTime());
174   }
175 
176 
177   public static java.sql.Date sqlDate(int year, int month, int date) {
178     return sqlDate(cal(year, month, date).getTime());
179   }
180 
181 
182   public static java.sql.Date sqlDate(int year, int month, int date, int hour, int minutes) {
183     return sqlDate(cal(year, month, date, hour, minutes).getTime());
184   }
185 
186 
187   public static java.sql.Date sqlDate(Date date, Integer hour, Integer minutes) {
188     return sqlDate(cal(date, hour, minutes).getTime());
189   }
190 
191 
192   public static String isoFormat(Date date) {
193     return isoDateFormat.format(date);
194   }
195 
196 
197   public static LocalDate localDate(Date date) {
198     return LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault()).toLocalDate();
199   }
200 
201 
202   public static Date getQuaterStart(Date date) {
203     Calendar cal = cal(date, 0, 0);
204     int quater = cal.get(MONTH) / 3;
205     cal.set(MONTH, quater * 3);
206     cal.set(DATE, 1);
207     return cal.getTime();
208   }
209 
210 
211   public static Date getQuaterEnd(Date date) {
212     Calendar cal = cal(date, 0, 0);
213     int quater = cal.get(MONTH) / 3 + 1;
214     cal.set(MONTH, quater * 3 - 1);
215     cal.set(DATE, cal.getActualMaximum(DATE));
216     return cal.getTime();
217   }
218 }
219