Home > Mobile >  java parsing date cannot compare before or after date. error: Undeclared method:after(java.lang.Stri
java parsing date cannot compare before or after date. error: Undeclared method:after(java.lang.Stri

Time:11-13

this is not the full project. i only include the part where it is not working.

I'm trying to display list of food that expires in october 2016 along with other details

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;

public class Main{

  public static void main(String[] args) throws ParseException {
        Scanner sc = new Scanner (System.in);
    
        String proCode = "";
        int quantity = 0;
        String expirationDate = "";
        int capacity = 0;
        String type = "";
        int count =0;
        int date =0;
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyy");
        sdf.setLenient(false);


        
    System.out.println("Enter expiration date (dd-mm-yyyy): ");
        expirationDate = sc.nextLine();
        //arrProduct[i] = new Foods(proCode, quantity, expirationDate);
        //System.out.println("\n" arrProduct[i] "\n");
        if (expirationDate.equals("31-10-2016")){
            System.out.println("\n is inside loop above\n");
            System.out.println(expirationDate);}
  }

}

it works if i use expirationDate.equals() but shows error Undeclared method:after(java.lang.String) if i change to .after()

        if (expirationDate.after("31-10-2016")){
            System.out.println("\n is inside loop above\n");
            System.out.println(expirationDate);}

thanks in advance.

CodePudding user response:

To get a java.util.Date from a String, you have to parse it first:

Date date = sdf.parse(expirationDate);
  • Related