• 【Java】这个案例帮你搞懂Date类型


    如果你想了解Date类,就需要了解一些概念:

    1. 计算机没办法知道时间,所以把时间都表示成为了自1900年1月1日0时0分0秒0毫秒到现在的毫秒值。用龙来储存,这是基础。
    2. Date就是对计算机存储的毫秒值进行一个格式化,并展示。
    3. 在Date之上还有一个Calendar的封装,让Date操作更方便。

    需求

    一个公司有一个活动需要分批次发放红包,这个活动有开始时间(yyyy-MM-dd HH:mm:ss)和结束时间(yyyy-MM-dd HH:mm:ss),以及红包发放时间(HH:mm:ss)。从活动开始时间到结束时间,每到发放时间就会发放红包。请把发放机会以list的形式存储在RedPackage类中。

    public class RedPackage {
    
        //当日红包开始发放时间
        private String startTime;
        //当日红包结束发放时间
        private String endTime;
    
    }
    
    public class DateTest {
    
        private static String startTime = "2020-11-16 08:15:00";
        private static String endTime = "2020-11-22 09:07:08";
        private static String sendPerTime = "07:02:02";
    
        public static void main(String[] args) {
            List<RedPackage> redPackageList = convertTime(startTime, endTime, sendPerTime);
            redPackageList.forEach(System.out::println);
        }
    
    }
    
    

    实例

        private static String startTime = "2020-11-16 08:15:00";
        private static String endTime = "2020-11-22 09:07:08";
        private static String sendPerTime = "07:02:02";
    
    输出:
    
    RedPackage(startTime=2020-11-17 07:02:02, endTime=2020-11-18 00:00:00)
    RedPackage(startTime=2020-11-18 07:02:02, endTime=2020-11-19 00:00:00)
    RedPackage(startTime=2020-11-19 07:02:02, endTime=2020-11-20 00:00:00)
    RedPackage(startTime=2020-11-20 07:02:02, endTime=2020-11-21 00:00:00)
    RedPackage(startTime=2020-11-21 07:02:02, endTime=2020-11-22 00:00:00)
    RedPackage(startTime=2020-11-22 07:02:02, endTime=2020-11-22 09:07:08)
    
        private static String startTime = "2020-11-16 08:15:00";
        private static String endTime = "2020-11-22 09:07:08";
        private static String sendPerTime = "10:02:02";
    
    输出:
    RedPackage(startTime=2020-11-16 10:02:02, endTime=2020-11-17 00:00:00)
    RedPackage(startTime=2020-11-17 10:02:02, endTime=2020-11-18 00:00:00)
    RedPackage(startTime=2020-11-18 10:02:02, endTime=2020-11-19 00:00:00)
    RedPackage(startTime=2020-11-19 10:02:02, endTime=2020-11-20 00:00:00)
    RedPackage(startTime=2020-11-20 10:02:02, endTime=2020-11-21 00:00:00)
    RedPackage(startTime=2020-11-21 10:02:02, endTime=2020-11-22 00:00:00)
    
        private static String startTime = "2020-11-16 08:15:00";
        private static String endTime = "2020-11-22 19:07:08";
        private static String sendPerTime = "10:02:02";
    
    输出:
    RedPackage(startTime=2020-11-16 10:02:02, endTime=2020-11-17 00:00:00)
    RedPackage(startTime=2020-11-17 10:02:02, endTime=2020-11-18 00:00:00)
    RedPackage(startTime=2020-11-18 10:02:02, endTime=2020-11-19 00:00:00)
    RedPackage(startTime=2020-11-19 10:02:02, endTime=2020-11-20 00:00:00)
    RedPackage(startTime=2020-11-20 10:02:02, endTime=2020-11-21 00:00:00)
    RedPackage(startTime=2020-11-21 10:02:02, endTime=2020-11-22 00:00:00)
    RedPackage(startTime=2020-11-22 10:02:02, endTime=2020-11-22 19:07:08)
    

    package datetest;
    
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.List;
    
    public class DateTest {
    
        private static String startTime = "2020-11-16 08:15:00";
        private static String endTime = "2020-11-22 09:07:08";
        private static String sendPerTime = "07:02:02";
    
        public static void main(String[] args) {
            List<RedPackage> redPackageList = convertTime(startTime, endTime, sendPerTime);
            redPackageList.forEach(System.out::println);
        }
    
        private static List<RedPackage> convertTime(String sourcesStartTime, String sourcesEndTime, String sourcesSendPerTime) {
            List<RedPackage> redPackageList = new ArrayList<>();
    
            SimpleDateFormat dateFormatWithDay = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            SimpleDateFormat dateFormatWithOutDay = new SimpleDateFormat("HH:mm:ss");
            Date startTime = new Date();
            Date endTime = new Date();
            Date sendPerTime = new Date();
    
            if ((sourcesStartTime == null || sourcesStartTime == "")
                    || (sourcesEndTime == null || sourcesEndTime == "")
                    || (sourcesSendPerTime == null || sourcesSendPerTime == "")) {
                //报错
                return null;
            }
    
            try {
                startTime = dateFormatWithDay.parse(sourcesStartTime);
                endTime = dateFormatWithDay.parse(sourcesEndTime);
                sendPerTime = dateFormatWithOutDay.parse(sourcesSendPerTime);
            } catch (ParseException e) {
                e.printStackTrace();
            }
    
            //判断数据是否为空
            if (startTime == null || endTime == null || sendPerTime == null) {
                //报错
                return null;
            }
    
            Calendar startTimeCalendar = Calendar.getInstance();
            startTimeCalendar.setTime(startTime);
            Calendar endTimeCalendar = Calendar.getInstance();
            endTimeCalendar.setTime(endTime);
            Calendar sendPerTimeCalendarWithoutYear = Calendar.getInstance();
            sendPerTimeCalendarWithoutYear.setTime(sendPerTime);
            Calendar sendPerTimeCalendar = buildDateWithYear(sendPerTimeCalendarWithoutYear, startTimeCalendar);
    
            //开始时间晚于结束时间
            if (startTimeCalendar.after(endTimeCalendar)) {
                //报错
                return null;
            }
            //开始时间和结束时间在一天
            if (validateIsOneDay(startTimeCalendar, endTimeCalendar)) {
            //放时间要晚于开始时间&&早于结束时间
                if (sendPerTimeCalendar.after(endTimeCalendar) || sendPerTimeCalendar.before(startTimeCalendar)) {
                    //报错
                    return null;
                } else {
                    //add sendPerTime - endTime
                    convertDateToRedPackage(redPackageList, startTimeCalendar, endTimeCalendar);
                }
            } else {
                //开始时间和结束时间不在一天
                //发放时间大于开始时间
                if (sendPerTimeCalendar.after(startTimeCalendar)) {
                    //是 add sendPerTime - 24:00:00
                    Calendar startCalender = buildDateWithYear(sendPerTimeCalendar, startTimeCalendar);
                    Calendar endCalender = buildNextDayFirstTime(startTimeCalendar);
                    convertDateToRedPackage(redPackageList, startCalender, endCalender);
                }else {
                    //下一天
                    buildNextDayFirstTime(startTimeCalendar);
                    buildDateWithYear(sendPerTimeCalendar, startTimeCalendar);
                }
                //是不是结束当天
                //否 add sendPerTime - 24:00:00 循环
                while (!validateIsOneDay(startTimeCalendar, endTimeCalendar)) {
                    Calendar startCalender = buildDateWithYear(sendPerTimeCalendar, startTimeCalendar);
                    Calendar endCalender = buildNextDayFirstTime(startTimeCalendar);
                    convertDateToRedPackage(redPackageList, startCalender, endCalender);
                }
                //是结束当天
                Calendar startCalender = buildDateWithYear(sendPerTimeCalendar, startTimeCalendar);
                //发放时间早于结束时间 add sendPerTime - endTime
                if (startCalender.before(endTimeCalendar)) {
                    convertDateToRedPackage(redPackageList, startCalender, endTimeCalendar);
                }
            }
    
            return redPackageList;
        }
    
    
        /**
         * @param targetDateCalendar
         * @return targetDateCalendar的下一天的00:00:00
         */
        private static Calendar buildNextDayFirstTime(Calendar targetDateCalendar) {
            int year = targetDateCalendar.get(Calendar.YEAR);
            int month = targetDateCalendar.get(Calendar.MONTH);
            int day = targetDateCalendar.get(Calendar.DAY_OF_MONTH);
            targetDateCalendar.set(year, month, day + 1, 0, 0, 0);
            return targetDateCalendar;
        }
    
        /**
         * 因为传入的sendPerTime只有时分秒,在加入数据库中需要添加年月日
         *
         * @param targetDateCalendar   目标事件
         * @param resourceDateCalendar 标记时间
         * @return 将目标时间的年月日标记成标记时间返回
         */
        private static Calendar buildDateWithYear(Calendar targetDateCalendar, Calendar resourceDateCalendar) {
            targetDateCalendar.set(Calendar.YEAR, resourceDateCalendar.get(Calendar.YEAR));
            targetDateCalendar.set(Calendar.MONTH, resourceDateCalendar.get(Calendar.MONTH));
            targetDateCalendar.set(Calendar.DAY_OF_MONTH, resourceDateCalendar.get(Calendar.DAY_OF_MONTH));
            return targetDateCalendar;
        }
    
        private static void convertDateToRedPackage(List<RedPackage> redPackageList, Calendar startTimeCalendar, Calendar endTimeCalendar) {
            RedPackage redPackage = new RedPackage();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            redPackage.setStartTime(sdf.format(startTimeCalendar.getTime()));
            redPackage.setEndTime(sdf.format(endTimeCalendar.getTime()));
            redPackageList.add(redPackage);
        }
    
        /**
         * 判断两个日期是否相同
         *
         * @param oneDateCalender
         * @param anotherDateCalender
         * @return
         */
        private static Boolean validateIsOneDay(Calendar oneDateCalender, Calendar anotherDateCalender) {
            int oneDateCalenderYear = oneDateCalender.get(Calendar.YEAR);
            int oneDateCalenderMonth = oneDateCalender.get(Calendar.MONTH);
            int oneDateCalenderDay = oneDateCalender.get(Calendar.DAY_OF_MONTH);
            int anotherDateCalenderYear = anotherDateCalender.get(Calendar.YEAR);
            int anotherDateCalenderMonth = anotherDateCalender.get(Calendar.MONTH);
            int anotherDateCalenderDay = anotherDateCalender.get(Calendar.DAY_OF_MONTH);
            if (oneDateCalenderYear == anotherDateCalenderYear
                    && oneDateCalenderMonth == anotherDateCalenderMonth
                    && oneDateCalenderDay == anotherDateCalenderDay) {
                return true;
            } else {
                return false;
            }
        }
    
        private static String convert(Calendar calendar) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return sdf.format(calendar.getTime());
        }
    
    }
    
    

    工具类

    市面上有很多开源的工具类,非常的方便,虽然不提倡反复的造轮子,但是再造轮子的过程中,可以让自己提升,下面的链接时笔者自己开发的一个工具类,非常简单,也做了很多注释。希望可以帮到大家。
     强大的开源Date工具类

  • 相关阅读:
    ss
    Linux vmstat命令实战详解
    【好书摘要】性能优化中CPU、内存、磁盘IO、网络性能的依赖
    mysql 数据库授权(给某个用户授权某个数据库)
    windows下mysql安装包安装
    053(五十五)
    053(五十四)
    053(五十三)
    053(五十二)
    053(五十一)
  • 原文地址:https://www.cnblogs.com/zllk/p/14006648.html
Copyright © 2020-2023  润新知