java 时间转换工具_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));

}

}

本文同步分享在 博客“cwl_java”(CSDN)。

如有侵权,请联系 support@oschina.cn 删除。

本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值