签到以一个时间段 为周期,取周期内连续签到日期及签到状态 经典算法

 不习惯废话,直接上干货。

第一种方法: 

/**
 *
 * 获取周期内连续日期
 *
 * @param days 连续周期内第几天(或 连续天数)
 * @param count 周期天数
 * @param date 日期
 *
 * @return List<Map<String, Object>>
 *
 */
private static List<Map<String, Object>> getCompDate(Integer days, Integer count, String date) {
    String nowDate = DateTimeUtil.getNowTime("yyyy-MM-dd");
    List<Map<String, Object>> reList = new ArrayList<Map<String,Object>>();
    days= days== 0 ? 1 : days;
	days= days % count == 0 ? count : (days % count) ;
	days--;
    for (int i = 1; i <= count; i++) {
        Map<String, Object> reMap  = new HashMap<String, Object>();
        String tempDate = DateTimeUtil.getBeforeOrAfterDate(date, -count, 1);
        reMap.put("today", nowDate.equals(tempDate) ? true : false); // true:今天
        reMap.put("date", tempDate);
        reMap.put("flag", days >= 0 ? 0 : 1); // flag = 0: 当前日期之前(包括当前日期)
        --count ;
		reList.add(reMap);
    }
    return reList;
}


/**
 *
 *  获取当前时间
 *
 * @param dateformat
 * 
 * @return String 
 *
 */
private static String getNowTime(String dateformat) {
    Date nowDate = new Date();
    SimpleDateFormat dateFormat = new SimpleDateFormat(dateformat);
    String nowime= dateFormat.format(nowDate );
    return nowTime;
}

/**
 *
 * 获取date前(或者后)第几天的日期
 * 
 * @param date 日期
 * @param day 负数: date前第几天日期, 正数: date 后第几天日期
 * @param dateformat
 *
 * @return String 
 *
 */
public static String getBeforeOrAfterDate(String date, Integer day, String dateformat) {
    try {
        SimpleDateFormat sdf = new SimpleDateFormat(dateformat);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(sdf.parse(date));
			
        calendar.add(Calendar.DAY_OF_MONTH, day); 
        Date dBefore = calendar.getTime();
        String str=sdf.format(dBefore);
        return str;
    } catch (Exception e) {
        return null;
    }
}

 测试:


public static void main(String[] args) {
    //10天为周期,当前日期为连续第1天
    List list0 = getCompDate1(1, 10, "2018-12-26");
    System.out.println("//10天为周期,当前日期为连续第1天 结果");
    for (int i = 0; i < list0.size(); i++) {
        Map map = (Map)list0.get(i);
        System.out.println(map.toString());
    }

    //10天为周期,当前日期为连续第8天
    List list1 = getCompDate1(8, 10, "2018-12-26");
    System.out.println("//10天为周期,当前日期为连续第8天 结果");
    for (int i = 0; i < list1.size(); i++) {
        Map map = (Map)list1.get(i);
        System.out.println(map.toString());
    }

    //10天为周期,当前日期为连续第14天
    List list2 = getCompDate1(14, 10, "2018-12-26");
    System.out.println("//10天为周期,当前日期为连续第14天 结果");
    for (int i = 0; i < list2.size(); i++) {
        Map map = (Map)list2.get(i);
        System.out.println(map.toString());
    }

    //10天为周期,当前日期为连续第25天
    List list3 = getCompDate1(25, 10, "2018-12-26");
    System.out.println("//10天为周期,当前日期为连续第25天 结果");
    for (int i = 0; i < list3.size(); i++) {
        Map map = (Map)list3.get(i);
        System.out.println(map.toString());
    }
}

 

测试结果:

//10天为周期,当前日期为连续第1天 结果
{flag=0, today=true, date=2018-12-26}
{flag=1, today=false, date=2018-12-27}
{flag=1, today=false, date=2018-12-28}
{flag=1, today=false, date=2018-12-29}
{flag=1, today=false, date=2018-12-30}
{flag=1, today=false, date=2018-12-31}
{flag=1, today=false, date=2019-01-01}
{flag=1, today=false, date=2019-01-02}
{flag=1, today=false, date=2019-01-03}
{flag=1, today=false, date=2019-01-04}
//10天为周期,当前日期为连续第8天 结果
{flag=0, today=false, date=2018-12-19}
{flag=0, today=false, date=2018-12-20}
{flag=0, today=false, date=2018-12-21}
{flag=0, today=false, date=2018-12-22}
{flag=0, today=false, date=2018-12-23}
{flag=0, today=false, date=2018-12-24}
{flag=0, today=false, date=2018-12-25}
{flag=0, today=true, date=2018-12-26}
{flag=1, today=false, date=2018-12-27}
{flag=1, today=false, date=2018-12-28}
//10天为周期,当前日期为连续第14天 结果
{flag=0, today=false, date=2018-12-23}
{flag=0, today=false, date=2018-12-24}
{flag=0, today=false, date=2018-12-25}
{flag=0, today=true, date=2018-12-26}
{flag=1, today=false, date=2018-12-27}
{flag=1, today=false, date=2018-12-28}
{flag=1, today=false, date=2018-12-29}
{flag=1, today=false, date=2018-12-30}
{flag=1, today=false, date=2018-12-31}
{flag=1, today=false, date=2019-01-01}
//10天为周期,当前日期为连续第25天 结果
{flag=0, today=false, date=2018-12-22}
{flag=0, today=false, date=2018-12-23}
{flag=0, today=false, date=2018-12-24}
{flag=0, today=false, date=2018-12-25}
{flag=0, today=true, date=2018-12-26}
{flag=1, today=false, date=2018-12-27}
{flag=1, today=false, date=2018-12-28}
{flag=1, today=false, date=2018-12-29}
{flag=1, today=false, date=2018-12-30}
{flag=1, today=false, date=2018-12-31}

第二种方法:

@org.junit.Test
public void test(){
    List<String> days = getDays(new Date(),3,7); // 7天为周期,今天是第三天
    for (String day : days) {
        System.out.println(day);
    }
}
	    
public List<String> getDays(Date date, int current, int cycle) {
    List<String> days = new ArrayList<String>();
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    Calendar calendar = getCalendar(date, current, cycle);
    for (int i = 0; i < cycle; i++) {
        days.add(format.format(calendar.getTime()));
        calendar.add(Calendar.DAY_OF_MONTH, 1);//增加一天
    }
    return days;
}
	    
public Calendar getCalendar(Date date, int current, int cycle) {
    if(current <= 0 || cycle <= 0 || date == null){
        throw new IllegalArgumentException("参数异常");
    }
	    	
    Calendar now = Calendar.getInstance();
    Calendar calendarNow = new GregorianCalendar();
    calendarNow.setTime(date);
	        
    int index = 1;
    current = current % cycle == 0 ? cycle : (current % cycle);
    index = index - current;

    calendarNow.add(Calendar.DAY_OF_MONTH, index);
    now.set(calendarNow.get(Calendar.YEAR), calendarNow.get(Calendar.MONTH), calendarNow.get(Calendar.DATE));
	        
    Calendar calendar = Calendar.getInstance();
    calendar.setFirstDayOfWeek(Calendar.MONDAY);
    calendar.set(calendarNow.get(Calendar.YEAR), calendarNow.get(Calendar.MONTH), calendarNow.get(Calendar.DATE));
    return calendar;
}

测试结果:

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值