java日期的比较 计算

做财务 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值