Calendar常见工具类

一、Calendar根据当前(指定)日期取出本周一本周日和下周一下周日时间

根据Calendar.java中定义的DAY_OF_WEEK来看

/**
     * Field number for <code>get</code> and <code>set</code> indicating the day
     * of the week.  This field takes values <code>SUNDAY</code>,
     * <code>MONDAY</code>, <code>TUESDAY</code>, <code>WEDNESDAY</code>,
     * <code>THURSDAY</code>, <code>FRIDAY</code>, and <code>SATURDAY</code>.
     *
     * @see #SUNDAY
     * @see #MONDAY
     * @see #TUESDAY
     * @see #WEDNESDAY
     * @see #THURSDAY
     * @see #FRIDAY
     * @see #SATURDAY
     */
    public final static int DAY_OF_WEEK = 7;

可以看出,周日作为一周的第一天,周六为最后一天
但是按照中国习惯,一周的周一应该为第一天,周日为最后一天
因此通过以下代码可以实现

根据当前(指定)日期,取出本周周一和周日,下周周一和周日

package com.aaa.test.entity;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @program: 
 * @Date: 2019/10/27 15:35
 * @Author: Mr.SU
 * @Description:
 */
public class Test {
    public static void main(String[] args) throws Exception{
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String currDate = sdf.format(date);

        //按中国习惯,周一为第一天,周日为最后一天
        //取出周一和周日,
        Date time0 = new SimpleDateFormat("yyyy-MM-dd").parse("2019-11-04");
        Calendar cal = Calendar.getInstance();
        cal.setTime(time0);
        String mon = "";
        String sun = "";
        String nextmon = "";
        String nextsun = "";
        Date monday = null;
        Calendar calSun = Calendar.getInstance();
        calSun.setTime(time0);
        //判断当前日期是否是周日
        if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {  //是周日,取出当前时间,往前减六天,取出当前周 周一时间
            
            calSun.set(Calendar.DATE, calSun.get(Calendar.DATE) - 6);
            monday = calSun.getTime();
            mon = sdf.format(monday);
            sun = sdf.format(time0);
            System.out.println(mon);  //本周一日期
            System.out.println(sun);  //本周日日期
            
        } else {  //不是周日,取出当前周 周一时间,往后加6天,去周日时间

            //关于DAY_OF_WEEK的说明
            //Field number for get and set indicating
            //the day of the week. This field takes values
            //SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY,
            //and SATURDAY
            calSun.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
            monday = calSun.getTime();    //本周一

            Calendar cal2 = Calendar.getInstance();
            cal2.setTime(monday);
            cal2.set(Calendar.DATE, cal2.get(Calendar.DATE) + 6);
            Date sunday = cal2.getTime();
            mon = sdf.format(monday);
            sun = sdf.format(sunday);
            System.out.println(mon);  //本周一日期
            System.out.println(sun);  //本周日日期
        }

        //根据当前日期取下周一和下周日
        //取下周一下周日
        Calendar nextCal = Calendar.getInstance();
        nextCal.setTime(monday);
        nextCal.set(Calendar.DATE, nextCal.get(Calendar.DATE) + 7);
        Date nextMonday = nextCal.getTime();  //下周一
        nextCal.set(Calendar.DATE, nextCal.get(Calendar.DATE) + 6);
        Date nextSunday = nextCal.getTime();  //下周日
        nextmon = sdf.format(nextMonday);
        nextsun = sdf.format(nextSunday);
        System.out.println(nextmon);  //下周一日期
        System.out.println(nextsun);  //下周日日期
    }
}

二、 Calendar根据当前(指定)日期取本月第一天和最后一天

		//查询本月第一天和最后一天
        Calendar cal3 = Calendar.getInstance();
        cal3.set(Calendar.DAY_OF_MONTH,1);
        System.out.println("本月第一天:"+cal3.getTime());
        cal3.roll(Calendar.DATE,-1);
        System.out.println("本月最后一天:"+cal3.getTime());

三、 Calendar根据当前(指定)日期取上月月末和上上月末

 		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cale = Calendar.getInstance();
        cale.set(Calendar.DAY_OF_MONTH, 0);//上月月末
        LastDay = format.format(cale.getTime());
        logger.info("------------LastDay:" + LastDay);
        cale.set(Calendar.DAY_OF_MONTH, 0);//上上月月末
        LLastday= format.format(cale.getTime());
        logger.info("------------LLastday:" + LLastday);

四、 Calendar根据当前(指定)日期取前一天凌晨0点和当天凌晨0点


	/**
    * @Description: 获取前一天凌晨0点和当天凌晨0点
    * @param: "[page]"
    * @Return: com.sinosoft.entity.Page
    * @Author: supenghui
    * @Date: 2019/12/13 3:10
    */
    public Page getDate(Page page){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.ssssss");
        //获取前一天的凌晨和当天凌晨0点
        Calendar ca = Calendar.getInstance();
        ca.set(Calendar.HOUR_OF_DAY,0);
        ca.set(Calendar.MINUTE,0);
        ca.set(Calendar.SECOND,0);
        Date cd = ca.getTime();
        String currDate = sdf.format(cd);
        ca.add(Calendar.DAY_OF_MONTH,-1);
        Date fd = ca.getTime();
        String frontDate = sdf.format(fd);

        page.getPd().put("frontDate",frontDate);   //昨天凌晨0点
        page.getPd().put("currDate",currDate);      //当天凌晨零点

        return page;
    }

五、java获取上个月第一天和最后一天时间

/**
     * 获取上一个月1号0点0分0秒的时间
     */
    private  String getBeforeFirstMonthdate()throws Exception{
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar=Calendar.getInstance();
        calendar.add(Calendar.MONTH, -1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        //将小时至00
        calendar.set(Calendar.HOUR_OF_DAY, 00);
        //将分钟至00
        calendar.set(Calendar.MINUTE, 00);
        //将秒至00
        calendar.set(Calendar.SECOND,00);
        String format1 = format.format(calendar.getTime());
        return format1;
    }

/**
     * 获取上个月的最后一天23点59分59秒的时间
     */
    private  String getBeforeLastMonthdate()throws Exception{
        SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar=Calendar.getInstance();
        int month=calendar.get(Calendar.MONTH);
        calendar.set(Calendar.MONTH, month-1);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        //将小时至23
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        //将分钟至59
        calendar.set(Calendar.MINUTE, 59);
        //将秒至59
        calendar.set(Calendar.SECOND,59);
        String format = sf.format(calendar.getTime());
        return format;
    }

打印出来结果是:
上个月第一天:2022-06-01 00:00:00
上个月最后一天:2022-06-30 23:59:59

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package com.hexiang.utils; import java.text.SimpleDateFormat; import java.util.*; public class CalendarUtil { public static void main(String args[]) { System.out.println("First day of week is : " + new SimpleDateFormat("yyyy-MM-dd") .format(getFirstDateByWeek(new Date()))); System.out.println("Last day of week is : " + new SimpleDateFormat("yyyy-MM-dd") .format(getLastDateByWeek(new Date()))); System.out.println("First day of month is : " + new SimpleDateFormat("yyyy-MM-dd") .format(getFirstDateByMonth(new Date()))); System.out.println("Last day of month is : " + new SimpleDateFormat("yyyy-MM-dd") .format(getLastDateByMonth(new Date()))); } /** * 获得所在星期的第一天 */ public static Date getFirstDateByWeek(Date date) { Calendar now = Calendar.getInstance(); now.setTime(date); int today = now.get(Calendar.DAY_OF_WEEK); int first_day_of_week = now.get(Calendar.DATE) + 2 - today; // 星期一 now.set(Calendar.DATE, first_day_of_week); return now.getTime(); } /** * 获得所在星期的最后一天 */ public static Date getLastDateByWeek(Date date) { Calendar now = Calendar.getInstance(); now.setTime(date); int today = now.get(Calendar.DAY_OF_WEEK); int first_day_of_week = now.get(Calendar.DATE) + 2 - today; // 星期一 int last_day_of_week = first_day_of_week + 6; // 星期日 now.set(Calendar.DATE, last_day_of_week); return now.getTime(); } /** * 获得所在月份的最后一天 * @param 当前月份所在的时间 * @return 月份的最后一天 */ public static Date getLastDateByMonth(Date date) { Calendar now = Calendar.getInstance(); now.setTime(date); now.set(Calendar.MONTH, now.get(Calendar.MONTH) + 1); now.set(Calendar.DATE, 1); now.set(Calendar.DATE, now.get(Calendar.DATE) - 1); now.set(Calendar.HOUR, 11); now.set(Calendar.MINUTE, 59); now.set(Calendar.SECOND, 59); return now.getTime(); } /** * 获得所在月份的第一天 * @param 当前月份所在的时间 * @return 月份的第一天 */ public static Date getFirstDateByMonth(Date date) { Calendar now = Calendar.getInstance(); now.setTime(date); now.set(Calendar.DATE, 0); now.set(Calendar.HOUR, 12); now.set(Calendar.MINUTE, 0); now.set(Calendar.SECOND, 0); return now.getTime(); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值