205k views
5 votes
Use the web to learn how to use the LocalDate Boolean methods isBefore(), isAfter(), and equals(). Use your knowledge to write a program that prompts a user for a month, day, and year, and then displays a message specifying whether the entered day is in the past, is today (the current date), or is in the future.

import java.util.*;
import java.time.LocalDate;
public class PastPresentFuture2
{
public static void main(String args[])
{
int mo,da,yr;
LocalDate today=LocalDate.now();
System.out.println("Program to find if the given date is in past, present or future::");
Scanner input=new Scanner(System.in);
//taking inputs from console and storing them in three variables.
System.out.print("Enter month::");
mo=input.nextInt();
System.out.print("Enter day::");
da=input.nextInt();
System.out.print("Enter year::");
yr=input.nextInt();
//creating a LocalDate object and initializing it to null;
LocalDate inputDate=null;
try
{
/*we are using the 3 variables and converting it into a date object to compare it with today's date */
inputDate = LocalDate.of(yr,mo,da);
}
catch(Exception ex)
{
/*if the entered day,month & year are not converted to a date object because of invalid entry of any values, we are stopping the program.*/
System.out.println("You have made invalid entries, please try again !!");
System.exit(0);
}
/*if the date object is created after proper entries, we are using the built-in functions to compare it the today's date object*/
System.out.print("The date you entered is ");
if(inputDate.isBefore(today))
System.out.println("in the past.");
else
if(inputDate.isAfter(today))
System.out.println("in the future.");
else
if(inputDate.equals(today))
System.out.println("the current date.");
}
}

1 Answer

5 votes

Answer:

Step-by-step explanation:

The code provided is already taking in the input and comparing it to the current date using the methods isBefore(), isAfter(), and equals(). It works perfectly and did not need any changes to the code. As you can see from the attached pictures below, every scenario works as intended. If the format is not correct and the date object is not able to be created then it prompts an error message and exits the program as intended.

import java.util.*;

import java.time.LocalDate;

class PastPresentFuture2

{

public static void main(String args[])

{

int mo,da,yr;

LocalDate today=LocalDate.now();

System.out.println("Program to find if the given date is in past, present or future::");

Scanner input=new Scanner(System.in);

//taking inputs from console and storing them in three variables.

System.out.print("Enter month::");

mo=input.nextInt();

System.out.print("Enter day::");

da=input.nextInt();

System.out.print("Enter year::");

yr=input.nextInt();

//creating a LocalDate object and initializing it to null;

LocalDate inputDate=null;

try

{

/*we are using the 3 variables and converting it into a date object to compare it with today's date */

inputDate = LocalDate.of(yr,mo,da);

}

catch(Exception ex)

{

/*if the entered day,month & year are not converted to a date object because of invalid entry of any values, we are stopping the program.*/

System.out.println("You have made invalid entries, please try again !!");

System.exit(0);

}

/*if the date object is created after proper entries, we are using the built-in functions to compare it the today's date object*/

System.out.print("The date you entered is ");

if(inputDate.isBefore(today))

System.out.println("in the past.");

else

if(inputDate.isAfter(today))

System.out.println("in the future.");

else

if(inputDate.equals(today))

System.out.println("the current date.");

}

}

Use the web to learn how to use the LocalDate Boolean methods isBefore(), isAfter-example-1
Use the web to learn how to use the LocalDate Boolean methods isBefore(), isAfter-example-2
Use the web to learn how to use the LocalDate Boolean methods isBefore(), isAfter-example-3
User Roland W
by
4.3k points