简单的根据输入时间戳把月份划分为周

利用Java8 时间API https://gitee.com/silenceone/codes/t9oycqzrhue0bfmgw3x8n94

 

package com.parent.common.utils;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.temporal.TemporalAdjusters;
import java.time.temporal.TemporalField;
import java.time.temporal.WeekFields;
import java.util.*;

/**
 * @ClassName: Java8DateUtil
 * @Description: 日期处理的工具类
 * @date: 2016年11月1日 下午6:47:33
 */
public class Java8DateUtil {

    private final static ZoneOffset zoneOffset = ZoneOffset.of("+8");

    /**
     * 把月份划分为周
     *
     * @param date
     * @return
     */
    public static Map<Integer, Set<Integer>> getWeekSMonth(int date) {
        Map<Integer, Set<Integer>> map = new HashMap<>();
        Set<Integer> set = new TreeSet<>();
        int first = getFirstDayOfMonth(date);
        int last = getLastDayOfWeek(first);
        set.add(first);
        set.add(last);
        map.put(1, set);
        //下周就是 last+1
        int i = 2;
        int next = last + 1;
        int lastDayOfMoth = getLastDayOfMonth(date);
        while (next <= lastDayOfMoth) {
            set = new TreeSet<>();
            set.add(next);
            int nextLastWeek = getLastDayOfWeek(next);
            if (nextLastWeek > lastDayOfMoth) {
                set.add(lastDayOfMoth);
            } else {
                set.add(nextLastWeek);
            }
            map.put(i, set);
            i++;
            next = nextLastWeek + 1;
        }
        return map;
    }

    /**
     * 本月第一天时间戳
     *
     * @param date
     * @return
     */
    public static int getFirstDayOfMonth(int date) {
        LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(date, 0, zoneOffset).with(TemporalAdjusters.firstDayOfMonth());
        localDateTime = localDateTime.toLocalDate().atStartOfDay();
        return (int) localDateTime.toEpochSecond(zoneOffset);
    }

    /**
     * 本月最后一天
     *
     * @param date
     * @return
     */
    public static int getLastDayOfMonth(int date) {
        LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(date, 0, zoneOffset).with(TemporalAdjusters.lastDayOfMonth());
        localDateTime = localDateTime.toLocalDate().atStartOfDay().plusDays(1).minusSeconds(1);
        return (int) localDateTime.toEpochSecond(zoneOffset);
    }

    /**
     * 计算给定日期的星期的第一天(暂时没用到)
     *
     * @param date
     * @return
     */
    public static int getFirstDayOfWeek(int date) {
        TemporalField fieldISO = WeekFields.of(Locale.CHINA).dayOfWeek();
        LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(date, 0, zoneOffset).with(fieldISO, 1);
        localDateTime = localDateTime.toLocalDate().atStartOfDay().plusDays(1);
        return (int) localDateTime.toEpochSecond(zoneOffset);
    }

    /**
     * 计算给定日期的星期的最后一天
     *
     * @param date
     * @return
     */
    public static int getLastDayOfWeek(int date) {
        LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(date, 0, zoneOffset);
        long minusDay = 7 - localDateTime.getDayOfWeek().getValue();
        localDateTime = localDateTime.plusDays(minusDay).plusHours(23).plusMinutes(59).plusSeconds(59);
        return (int) localDateTime.toEpochSecond(zoneOffset);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值