Java工具集-日期转换工具类

代码示例


import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @program: simple_tools
 * @description: 日期转换工具类
 * @author: Mr.chen
 * @create: 2020-05-18 11:15
 **/
public class DayConvertUtil {

    // 通用周下标
    public static final int[] COMMON_INDEX = new int[] { 2, 3, 4, 5, 6, 7, 1 };
    // 周下标
    private static final int[] INDEXS = new int[] { 1, 2, 3, 4, 5, 6, 7 };
    // 每天下标快照
    private static final String FULL_INDEXS_SNAP = "1234567";
    private static final String FULL_INDEXS_SNAP_NAME = "每天";

    // 工作日下标快照
    private static final String WORK_DAY_INDEXS_SNAP = "23456";
    private static final String WORK_DAY_INDEXS_SNAP_NAME = "工作日";

    // 周末下标快照
    private static final String WEEKEND_DAY_INDEXS_SNAP = "17";
    private static final String WEEKEND_DAY_INDEXS_SNAP_NAME = "周末";

    // 周名称
    private static final String[] NAMES = new String[] { "周日", "周一", "周二", "周三", "周四", "周五", "周六" };
    // 每天名称快照
    private static final String FULL_NAMES_SNAP = "日一二三四五六";
    // 周几前缀
    private static final String NAME_PREFIX = "周";
    // 周几名称分割符
    public static final String SEPARATOR_COMMA = ",", SEPARATOR_BLANK_SPACE = " ", SEPARATOR_SLIGHT_PAUSE = "、";

    /**
     * 根据日期下标获取日期名称
     *
     * @param dayIndex
     * @return
     */
    public static String getDayName(int dayIndex) {
        int tempIndex = FULL_INDEXS_SNAP.indexOf(dayIndex + "");
        if (tempIndex < 0) {
            return null;
        }
        return NAMES[tempIndex];
    }

    /**
     * 根据日期名称获取日期下标
     *
     * @param dayName
     * @return
     */
    public static Integer getDayIndex(String dayName) {
        if (StringUtils.isEmpty(dayName) || (dayName.length() != 2) || !dayName.startsWith(NAME_PREFIX)) {
            return null;
        }
        int tempIndex = FULL_NAMES_SNAP.indexOf(dayName.charAt(1));
        if (tempIndex < 0) {
            return null;
        }
        return INDEXS[tempIndex];
    }

    /**
     * 规范工作日、每天、周末、周几拼接输出
     *
     * @param dayIndexs
     * @param separator
     * @return
     */
    public static String getDayNames(List dayIndexs, String separator) {
        if (CollectionUtils.isEmpty(dayIndexs)) {
            return null;
        }

        Collections.sort(dayIndexs);
        String snap = StringUtils.join(dayIndexs, "");
        if (FULL_INDEXS_SNAP.equals(snap)) {
            return FULL_INDEXS_SNAP_NAME;
        } else if (WORK_DAY_INDEXS_SNAP.equals(snap)) {
            return WORK_DAY_INDEXS_SNAP_NAME;
        } else if (WEEKEND_DAY_INDEXS_SNAP.equals(snap)) {
            return WEEKEND_DAY_INDEXS_SNAP_NAME;
        } else {
            StringBuffer names = new StringBuffer();
            boolean contain7Index = false;
            int tempIndex = -1;
            for (Integer index : dayIndexs) {
                if (index == null) {
                    continue;
                }
                if (index == 1) {
                    contain7Index = true;
                    continue;
                }
                tempIndex = FULL_INDEXS_SNAP.indexOf(index.toString());
                if ((tempIndex < 0) || (tempIndex > FULL_INDEXS_SNAP.length())) {
                    continue;
                }
                names.append(NAMES[tempIndex]).append(separator);
            }

            if (names.length() > 0) {
                if (contain7Index) {
                    names.append(NAMES[0]);
                    return names.toString();
                }
                return names.substring(0, names.length() - 1);
            } else {
                if (contain7Index) {
                    return NAMES[0];
                }
                return null;
            }
        }
    }

    public static void main(String[] args) {
        List days = new ArrayList();
        days.add(7);
        days.add(2);
        days.add(4);
        days.add(3);
        days.add(5);
        days.add(6);
        System.out.println(getDayNames(days, SEPARATOR_BLANK_SPACE));
    }
}

你可能感兴趣的:(Java工具集,java,date)