In this tutorial, we will learn how to take date from user and and display output in Java.
I have the time and date system working for today, tomorrow and for a set date. Now I create some input data for date as a pattern by SimpleDateFormat class.
Then read date for the system so that you can input any date
enter
Example 1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Scanner; public class JavaOrnekler { public static void main(String[] args) throws ParseException { Date date1=null; Scanner input = new Scanner(System.in); SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy"); System.out.println("Enter check-in date (gg/aa/yy):"); String cinput = input.nextLine(); if(null != cinput && cinput.trim().length() > 0){ date1 = format.parse(cinput); } System.out.print(date1); } } |
This Java code defines a class named JavaOrnekler
with a main
method that prompts the user to enter a date in the format “dd/MM/yyyy” using a Scanner
object. It then uses a SimpleDateFormat
object to parse the input string into a Date
object, which is then printed to the console using the println
method.
The main
method includes a throws ParseException
clause, which means that it can throw a ParseException
if there is an error parsing the input string. This exception will need to be handled if the code is going to be used in a larger program.
The if
statement is used to check if the input string is not null
and not empty before attempting to parse it into a Date
object. This is to prevent a NullPointerException
from being thrown if the user enters an empty string.
Output:
Example 2:
To take a date input from a user and save it as a date in Java, you can use a Scanner
object to read the input as a string and then use a SimpleDateFormat
object to parse the string into a Date
object.
Here is an example of how this can be done:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Scanner; public class DateInputExample { public static void main(String[] args) throws ParseException { // Create a Scanner object for reading input Scanner input = new Scanner(System.in); // Prompt the user to enter a date System.out.println("Enter a date (dd/MM/yyyy):"); String dateString = input.nextLine(); // Create a SimpleDateFormat object for parsing the date string SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy"); // Parse the date string into a Date object Date date = format.parse(dateString); // Print the Date object to the console System.out.println(date); } } |
This code prompts the user to enter a date in the format “dd/MM/yyyy” and reads the input as a string using the nextLine
method of the Scanner
object. It then creates a SimpleDateFormat
object and uses the parse
method to parse the input string into a Date
object. Finally, it prints the Date
object to the console using the println
method.
Note that the parse
method can throw a ParseException
if there is an error parsing the input string, so the main
method includes a throws ParseException
clause. This exception will need to be handled if the code is going to be used in a larger program.