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
Java Code:
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); } } |
Output:
How to get date as input using scanner class in Java.