JAVA通過時間戳判斷一個日期對象是否在二個日期對象之間,是否在開始時間和結束時間之間

我們有時候需要做日期搜索的時候,需要在數據庫尋找創建時間在搜索日期之內的數據,那麼久需要判斷是否在這個日期之內了,date類型dateTime類型都是可以通過simpleDateFormat進行轉換爲yyyy-MM-dd類型的。 


    /**
     * @Description 傳入yyyy-MM-dd的String類型的date 2019-06-28
     * @Date 18:13 2019/6/28
     * @Param [m, st, ed] m=判斷時間  st開始時間 ed結束日期時間 時間都是yyyy-MM-dd格式
     * @return boolean true 表示這個日期在這二個日期之
     **/
    public static boolean inTheTwoDate(String m ,String st,String ed){
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            int startDay = 0;
            int endDay = 0;
            int mDay = 0;
            try {
                Date dateStart = format.parse(st);
                Date datEnd = format.parse(ed);
                Date mDate = format.parse(m);

                startDay = (int) (dateStart.getTime() / 1000);
                endDay = (int) (datEnd.getTime() / 1000);
                mDay = (int) (mDate.getTime() / 1000);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if(startDay <= mDay && mDay <=endDay){
                return true;
            } else {
                return false;
            }
    }

    public static void main(String[] args) {
        boolean b =inTheTwoDate("2019-06-10","2019-06-11","2019-08-21");
        System.out.println(b);
        boolean b2 =inTheTwoDate("2019-06-15","2019-06-11","2019-08-21");
        System.out.println(b2);


    }

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章