Java: how do I check if a Date is within a certain range?

后端 未结 12 521
耶瑟儿~
耶瑟儿~ 2020-11-22 16:39

I have a series of ranges with start dates and end dates. I want to check to see if a date is within that range.

Date.before() and Date.after() seem to be a little a

12条回答
  •  囚心锁ツ
    2020-11-22 16:54

    your logic would work fine . As u mentioned the dates ur getting from the database are in timestamp , You just need to convert timestamp to date first and then use this logic.

    Also dont forget to check for null dates.

    here m sharing a bit to convert from Timestamp to date.

    public static Date convertTimeStamptoDate(String val) throws Exception {

        DateFormat df = null;
        Date date = null;
    
        try {
            df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            date = df.parse(val);
            // System.out.println("Date Converted..");
            return date;
        } catch (Exception ex) {
            System.out.println(ex);
            return convertDate2(val);
        } finally {
            df = null;
            date = null;
        }
    }
    

提交回复
热议问题