1

I have a date like this :: String d1="27-May-2017"; I need to compare if this date is earlier than today or not. I have tried the below code but not able to parse LocalDateTime.now(). The below line is showing compilation error as it is getting parsed.

--> Date date2 = sdf.parse(LocalDateTime.now());

Can anyone plz suggest the correct way

package com.example.demo; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Objects; import java.util.stream.Collectors; import java.util.stream.Stream; import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.LocalDateTime; import java.util.Date; public class Main2 { public static void main(String[] args) { try{ SimpleDateFormat sdf = new SimpleDateFormat("d-MMM-yyyy"); String d1="27-May-2017"; Date date1 = sdf.parse(d1); Date date2 = sdf.parse(LocalDateTime.now()); if(date1.after(date2)){ System.out.println("Date1 is after Date2"); } if(date1.before(date2)){ System.out.println("Date1 is before Date2"); } if(date1.equals(date2)){ System.out.println("Date1 is equal Date2"); } System.out.println(); } catch(ParseException ex){ ex.printStackTrace(); } } } 
4
  • 2
    LocalDateTime.now() returns a LocalDateTime (javadoc). Don't parse it. Commented Jun 30, 2021 at 12:59
  • Date date2 = LocalDate.now();-->tried like this also but not worked Commented Jun 30, 2021 at 13:01
  • 1
    It obviously can't LocalDate is a different class than Date. Either only work with Date (and SimpleDateFormatter) or switch to the newer and better LocalDate and DateTimeFormatter. Commented Jun 30, 2021 at 13:03
  • (1) Since you can use java.time, the modern Java date and time API, avoid also mixing in the outdated SimeplDateFormat and Date classes. Mixing them in will bring you nothing but trouble and over-complication. (2) If you are interested in the date and not the time of day, use LocalDate, not LocalDateTime. Commented Jun 30, 2021 at 14:35

2 Answers 2

4

Don't use the old API e.g. SimpleDateFormat and java.util.Date but if possible use java.time all the way:

//note that you should pass a Locale to be sure the right one is used to parse the month name DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d-MMM-yyyy", Locale.ENGLISH); LocalDate date1 = LocalDate.parse("27-May-2017", formatter ); LocalDate date2 = LocalDate.now()); if(date1.isAfter(date2)) { System.out.println("Date1 is after Date2"); } ... 
Sign up to request clarification or add additional context in comments.

Comments

1

Exactly the same advice as in the answer given by @Thomas, just a different way of using that API:

Use java.time:

public static boolean isBeforeToday(String date) { return LocalDate.parse(date, DateTimeFormatter.ofPattern("dd-MMM-uuuu", Locale.ENGLISH)) .isBefore(LocalDate.now()); } 

You can use this method like this:

public static void main(String[] args) { String d1 = "27-May-2017"; String r = isBeforeToday(d1) ? " is " : " is not "; System.out.println(d1 + r + "before today"); } 

and receive the result

27-May-2017 is before today 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.