1
votes
        String businessDate = null;
        String CUTOVER_TIME = "22:30";
        Date dateOfReport = new Date();
        Date businessDateyyyymmdd = null;
        SimpleDateFormat dateOnly = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat sdfDateTime = new SimpleDateFormat("yyyyMMdd hh:mm");
        businessDate = dateOnly.format(dateOfReport);
         
        businessDate = dateOnly.format(dateOfReport);
            Date cutoverTime = sdfDateTime.parse(businessDate + CUTOVER_TIME);
            businessDateyyyymmdd = dateOnly.parse(businessDate);
            
            if (cutoverTime.after(dateOfReport)) {  
                Calendar c = Calendar.getInstance();
                c.setTime(businessDateyyyymmdd);
                c.add(Calendar.DATE, -1);
                businessDateyyyymmdd = c.getTime();
                businessDate = dateOnly.format(businessDateyyyymmdd); 
            } else {
                if (cutoverTime.before(dateOfReport)) {
                    System.out.println("no cutover");
                }
            }

Error appeared as Exception in thread "main" java.text.ParseException: Unparseable date: "2022071822:30" at java.base/java.text.DateFormat.parse(DateFormat.java:395) at com.anz.test.Employee.main(Employee.java:81)

hh means 12-hour hour-of-day. I think you mean HH. I'd also strongly recommend abandoning java.util.Date and moving to the java.time classes. - Jon Skeet
Compare the date "2022071822:30" and the format "yyyyMMdd hh:mm". See the difference? One would be that you'd need to use HH:mm, the other would be that your format requires a space between date and time and your passed in timestamp doesn't seem to have any. - Thomas
Yes it should be HH but when i have given a space as follow - Date cutoverTime = sdfDateTime.parse(businessDate + " " + CUTOVER_TIME); The output is displayed as follows Mon Jul 18 22:30:00 IST 2022. I wanted to check basically calculate business date - has to be completed date - cutover is 22.30 if time > 22.30, date is today if time < 22.30 date is yesterday - Jayesh Rajvir
I strongly recommend that you use none of old classes Date, SimpleDateFormat and Calendar since they are so troublesome. Their modern replacement is java.time, which I recommend using instead. It's so much nicer to work with and also lends itself much better to your particular logic. - Ole V.V.
"Yes it should be HH" - so why did you post it with hh? That's what's failing to parse 22:30, which is what you asked about. If you want to know something different, you should ask a different question. Also note that in the code you've posted, you don't have a space between the date and the time, but there is one in the pattern. Finally, again, I would advise you not to use these classes. - Jon Skeet