栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

时间处理工具

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

时间处理工具

java环境jdk8

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


public class DateUtils {
    
    private static final String[] WEEK_EN = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
    
    private static final String[] WEEK_CN = {"周一", "周二", "周三", "周四", "周五", "周六", "周日"};
    
    private static final String EARLY_MORNING = "00:00:00";
    
    private static final String NOonDAY = "12:00:00";
    
    public static String YYYY = "yyyy";
    
    public static String YYYY_MM_DD = "yyyy-MM-dd";
    
    public static String DD = "dd";
    
    public static String HH_MM = "HH:mm";
    
    public static String MM_DD_HH_MM = "MM月dd日 HH:mm";
    
    public static String YYYY_MM_DD_HH_MM = "yyyy年MM月dd日 HH:mm";
    
    public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    
    public static String SPACE = " ";

    
    public static Date getNowDate() {
        return new Date();
    }

    
    public static String format(Date date, String pattern) {
        if (date != null) {
            SimpleDateFormat df = new SimpleDateFormat(pattern);
            return df.format(date);
        }
        return null;
    }

    
    public static Date parseDate(String str) {
        if (str == null) {
            return null;
        }
        SimpleDateFormat formatter = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
        try {
            return formatter.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    
    public static String format(Date date) {
        return processingTime(date);
    }

    
    private static String processingTime(Date date) {
        // 处理后的日期
        String data;
        // 当天中午
        Date noon = parseDate(format(getNowDate(), YYYY_MM_DD).concat(SPACE).concat(NOONDAY));
        // 处理时间下标索引
        int index = 0;
        // 得到需要处理的日期
        String week = date.toString().substring(0, 3);
        for (String s : WEEK_EN) {
            if (s.equals(week)) {
                break;
            }
            index++;
        }
        // 判断是否是同一周
        if (date.getTime() >= getMonTime() && date.getTime() <= getSunTime()) {
            // 判断是否是同一天
            if (format(date, DD).equals(format(noon, DD))) {
                // 判断上下午
                if (date.before(noon)) {
                    data = "上午".concat(SPACE).concat(format(date, HH_MM));
                } else {
                    data = "下午".concat(SPACE).concat(format(date, HH_MM));
                }
            } else {
                data = WEEK_CN[index].concat(SPACE).concat(format(date, HH_MM));
            }
        } else {
            // 判断是否是同一年
            if (format(date, YYYY).equals(format(noon, YYYY))) {
                data = format(date, MM_DD_HH_MM);
            } else {
                data = format(date, YYYY_MM_DD_HH_MM);
            }
        }
        return data;
    }

    
    public static long getMonTime() {
        // 当天凌晨
        Date earlyMorning = parseDate(format(getNowDate(), YYYY_MM_DD).concat(SPACE).concat(EARLY_MORNING));
        // 得到当前日期
        String week = earlyMorning.toString().substring(0, 3);
        // 得到当天日期下标索引
        int index = 0;
        for (String s : WEEK_EN) {
            if (s.equals(week)) {
                break;
            }
            index++;
        }
        // 周一00:00:00时间戳
        return earlyMorning.getTime() - 86400000L * index;
    }

    
    public static long getSunTime() {
        return getMonTime() + 86400000L * 7;
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/686691.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号