How can I take date input from user? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How can I take date input from user?

Need to scan the date in proper format

28th Jan 2017, 12:01 PM
Munal Dhakal
Munal Dhakal - avatar
1 Answer
+ 2
An example would be: // To take the input Scanner scanner = new Scanner(System.in); System.out.println("Enter the Date "); String date = scanner.next(); SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy"); Date date2=null; try { //Parsing the String date2 = dateFormat.parse(date); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(date2);
28th Jan 2017, 12:28 PM
R4xx4r
R4xx4r - avatar