How to determine whether two times are on the same day in Java, how to determine whether it is the time of the day in Java

There is a personal public account at the bottom of the article: Xiao Zheng, who loves technology. Mainly sharing development knowledge, learning materials, graduation design guidance, etc. Those who are interested can pay attention. Why share? There is no need for others to step on the pits that have been stepped on, and replaying the game by yourself can also deepen the memory. Benefiting oneself and benefiting others is the so-called win-win situation.

                                                                              Xiao Zheng who loves technology

Preface

You will encounter such a scenario during development. Generally, there will be a flow of transaction sending. The flow of water generally adopts the form of increment. If the flow of water is not processed, the flow of water will become larger and larger as time accumulates. In order to avoid excessive flow, it is necessary to reset the flow on a new day [the flow can be spliced ​​according to certain rules].

To determine whether two times are on the same day, that is, to determine whether their year, month, and day are the same, you can use the date class in Java to achieve this. Here are two methods.

Get current date

    /**
     * 获取当前时间  日期:20230912  年:2023  月:9  日:12
     */
    public static void testData02(){
    
    
        Date currentDate = new Date();
        //int currentYear = currentDate.getYear(); //该方法已经过时
        SimpleDateFormat ft = new SimpleDateFormat("yyyyMMdd");
        Calendar calender = Calendar.getInstance();
        calender.setTime(currentDate);
        String currentTime = ft.format(calender.getTime());
        int currentYear = calender.get(Calendar.YEAR);
        int currentMonth = calender.get(Calendar.MONTH)+1;
        int currentDay = calender.get(Calendar.DATE);
        System.out.println("日期:" + currentTime +" " + " 年:" + currentYear + " "
        + " 月:" + currentMonth + " " +" 日:" + currentDay);

    }

Method 1 [Use Calendar class]

We compare by obtaining the year, month, and day information of two time instances. If the three information are the same, the two times are considered to be the same day.

    /**
     * 方法一:使用Calendar类
     * 判断两个时间是否为同一天
     * @param date1 时间1
     * @param date2 时间2
     * @return 是否为同一天
     */
    public static boolean isSameDay1(Date date1, Date date2) {
    
    
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);

        return cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
                && cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH)
                && cal1.get(Calendar.DAY_OF_MONTH) == cal2.get(Calendar.DAY_OF_MONTH);
    }

Method 2 [Use SimpleDateFormat class]

We use the SimpleDateFormat class to format the date into the "yyyyMMdd" format, and then compare the string forms of the two times to see if they are equal to determine whether the two times are the same day.

    /**
     * 方法二:使用SimpleDateFormat类
     * 判断两个时间是否为同一天
     * @param date1 时间1
     * @param date2 时间2
     * @return 是否为同一天
     */
    public static boolean isSameDay2(Date date1, Date date2) {
    
    
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        return sdf.format(date1).equals(sdf.format(date2));
    }

test

    public static void main(String[] args) throws ParseException {
    
    
        CompareTimeisEquall.testData02();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");

        Date date1 = sdf.parse("2021-07-01");
        Date date2 = sdf.parse("2021-07-01");
        Date date3 = sdf.parse("2021-07-02");
        Date date4 = sdf1.parse("20230912");
        Date date5 = sdf1.parse("20230912");
        Date date6 = sdf1.parse("20230911");


        // 方法一示例
        System.out.println(isSameDay1(date1, date2)); // true
        System.out.println(isSameDay1(date1, date3)); // false

        // 测试 20230912 类型的日期
        System.out.println(isSameDay1(date4, date5)); // true
        System.out.println(isSameDay1(date5, date6)); // false

        // 方法二示例
        System.out.println(isSameDay2(date1, date2)); // true
        System.out.println(isSameDay2(date1, date3)); // false
    }

Guess you like

Origin blog.csdn.net/weixin_43304253/article/details/132841776