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
| import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List;
public class Time {
public static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); public static SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public static void main(String[] args) throws Exception { List<Date> list = addDates("2019-02-01", "2019-02-05"); }
public static List<Date> addDates(String cntDateBeg, String cntDateEnd) { List<Date> list = new ArrayList<>(); String[] dateBegs = cntDateBeg.split("-"); String[] dateEnds = cntDateEnd.split("-"); Calendar start = Calendar.getInstance(); start.set(Integer.valueOf(dateBegs[0]), Integer.valueOf(dateBegs[1]) - 1, Integer.valueOf(dateBegs[2])); Long startTIme = start.getTimeInMillis(); Calendar end = Calendar.getInstance(); end.set(Integer.valueOf(dateEnds[0]), Integer.valueOf(dateEnds[1]) - 1, Integer.valueOf(dateEnds[2])); Long endTime = end.getTimeInMillis(); Long oneDay = 1000 * 60 * 60 * 24l; Long time = startTIme; while (time <= endTime) { list.add(new Date(time)); time += oneDay; } return list; }
public static Date getFinallyDate(Date date) { String temp = format.format(date); temp += " 23:59:59";
try { return format1.parse(temp); } catch (Exception e) { return null; } }
public static Date getStartDate(Date date) { String temp = format.format(date); temp += " 00:00:00";
try { return format1.parse(temp); } catch (Exception e) { return null; } } }
|