日期工具:判断是否同一天、选择的日期是周几、获取本周一、本周日 、上周一、上周日,日期的下一天、当前时间几分钟之后的时间

//判断是否同一天
  public static boolean isSameDay1(Calendar cal1, Calendar cal2) {
        if(cal1 != null && cal2 != null) {
            return cal1.get(0) == cal2.get(0) && cal1.get(1) == cal2.get(1) && cal1.get(6) == cal2.get(6);
        } else {
            throw new IllegalArgumentException("The date must not be null");
        }
    }
//判断选择的日期是周几
 public static String getWeekOfDate(Date date) {
        String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[w];
    }
//获取本周一、本周日 、上周一、上周日的日期 n=0 本周 n=-1上周
  public static String getDate(String week, int n) {
        Calendar cal = Calendar.getInstance();
        String day;
        cal.add(Calendar.DATE, n*7);
        if("monday".equals(week)){
            cal.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
        }else if("sunday".equals(week)){
            cal.set(Calendar.DAY_OF_WEEK,Calendar.SUNDAY);
        }
        day = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(cal.getTime());
        log.info("日期:"+day);
        return day;
    }
//某個日期的下一天
 public static String getNextDate(String time) {
        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
        String nextDay="";
        try {
            Date temp = dft.parse(time);
            Calendar cld = Calendar.getInstance();
            cld.setTime(temp);
            cld.add(Calendar.DATE, 1);
            temp = cld.getTime();
            //获得下一天日期字符串
            nextDay = dft.format(temp);

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return nextDay;
    }
//获取当前时间几分钟之后的时间
 public static String getNextTime(Integer length) {
        SimpleDateFormat dft = new SimpleDateFormat("HH:mm:ss");
        String nextDay="";
        Date temp = new Date();
        Calendar cld = Calendar.getInstance();
        cld.setTime(temp);
        cld.add(Calendar.MINUTE, length);
        temp = cld.getTime();
        //获得下一天日期字符串
        nextDay = dft.format(temp);
        return nextDay;
    }
   /**
	 * 获取当月1号的开始时间
	 * @return
	 */
	public static Date getCurrentMonthFirstDayStartTime() {
		Calendar c = Calendar.getInstance();
		SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), 1,0, 0, 0);    //按格式输出
		String gtime2 = sdf2.format(c.getTime());
		Date date  = null;
		try {
			date = sdf2.parse(gtime2);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}
	/**
	 * 获取当月的结束时间
	 * @return
	 */
	public static Date getCurrentMonthEndTime() {
		Calendar c = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		int lastMonthMaxDay = c.getActualMaximum(Calendar.DAY_OF_MONTH);
		c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), lastMonthMaxDay, 23, 59, 59);    //按格式输出
		String gtime = sdf.format(c.getTime()); //上月最后一天
		Date date = null;
		try {
			date = sdf.parse(gtime);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}
 public static void main(String[] args) {
        System.out.println(getNextTime(10));
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值