Question:
How to read in a date from a Scanner?
Answer:
You can read in a String and then convert it to a Date.
Code:
import java.util.*; import java.text.*; public class ScanDate { public static void main(String[] args) { SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy"); Scanner sc = new Scanner(System.in); System.out.println("Eample: 12-25-2103"); System.out.print("Enter date: "); String str = sc.nextLine(); try { Date date = sdf.parse(str); sdf = new SimpleDateFormat("EEE, d MMM yyyy"); System.out.println("Date: " + sdf.format(date)); } catch (ParseException e) { System.out.println("Parse Exception"); } } }
Output:
$ java ScanDate Eample: 12-25-2103 Enter date: 02-12-2014 Date: Wed, 12 Feb 2014