java日期的比较 计算

来源:互联网 发布:ipad1下载软件 编辑:程序博客网 时间:2024/05/22 17:30

做财务 erp之类的系统会经常用到很多时间上的比较和计算,这里我总结了一下我用过的东西

用到 的朋友改改就可以用了

public static Date StringToDate2(String str){if(str==null||str.trim().equals("")) return null;Date date = new Date();SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");try {date = sf.parse(str);} catch (ParseException e) {e.printStackTrace();date = null;}return date;}/* * 获取该周*/public static Date[] getWeekDays(Date date) {Calendar cale = Calendar.getInstance();cale.setTime(date);cale.setFirstDayOfWeek(Calendar.SUNDAY);int days = 7;int today = cale.get(Calendar.DAY_OF_WEEK);long millis = cale.getTimeInMillis();Date dates[] = new Date[days];for (int i = 1; i <= days; i++) {long sub = (today - i) * 24 * 60 * 60 * 1000L;dates[i - 1] = new Date(millis - sub);}cale = null;return dates;}/* * 获取该月*/public static Date[] getMonthDays(Date date) {Calendar cale = Calendar.getInstance();cale.setTime(date);int today = cale.get(Calendar.DAY_OF_MONTH);int days = cale.getActualMaximum(Calendar.DAY_OF_MONTH);long millis = cale.getTimeInMillis();Date dates[] = new Date[days];for (int i = 1; i <= days; i++) {long sub = (today - i) * 24 * 60 * 60 * 1000L;dates[i - 1] = new Date(millis - sub);}cale = null;return dates;}public static Date getMonthStartDays() {return getMonthDays(new Date())[0];}public static Date getMonthEndDays() {return getMonthDays(new Date())[(getMonthDays(new Date()).length)-1];}/* * 获取该年*/public static Date[] getYearDays(Date date) {Calendar cale = Calendar.getInstance();cale.setTime(date);int today = cale.get(Calendar.DAY_OF_YEAR);int days = cale.getActualMaximum(Calendar.DAY_OF_YEAR);long millis = cale.getTimeInMillis();Date dates[] = new Date[days];for (int i = 1; i <= days; i++) {long sub = (today - i) * 24 * 60 * 60 * 1000L;dates[i - 1] = new Date(millis - sub);}cale = null;return dates;}/**  * 比较两个日期之间的大小  *   * @param d1  * @param d2  * @return 前者大于后者返回true 反之false  */ public static boolean compareDate(Date d1, Date d2) {      Calendar c1 = Calendar.getInstance();      Calendar c2 = Calendar.getInstance();      c1.setTime(d1);      c2.setTime(d2);        int result = c1.compareTo(c2);      if (result >= 0)          return true;      else          return false;  } /**     * 返回指定年月的月的最后一天     *     * @param year     * @param month     * @return     */    public static Date getLastDayOfMonth(Integer year, Integer month) {        Calendar calendar = Calendar.getInstance();        if (year == null) {            year = calendar.get(Calendar.YEAR);        }        if (month == null) {            month = calendar.get(Calendar.MONTH);        }        calendar.set(year, month,-1);        calendar.roll(Calendar.DAY_OF_MONTH, -1);        return calendar.getTime();    }    /**     * 返回指定年月的月的第一天     *     * @param year     * @param month     * @return     */    public static Date getFirstDayOfMonth(Integer year, Integer month) {        Calendar calendar = Calendar.getInstance();        if (year == null) {            year = calendar.get(Calendar.YEAR);        }        if (month == null) {            month = calendar.get(Calendar.MONTH);        }        calendar.set(year, month, 1);        return calendar.getTime();    }


0 0