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;
    }

author:su1573

猜你喜欢

转载自blog.csdn.net/su1573/article/details/102782775
今日推荐