Question

Hi i'm a java student beginner I can't get how to input by console a date using GregorianCalendar and print it like: Input: 12/10/1994 Output: 12/10/1994

Was it helpful?

Solution

here you go

String format="MM/dd/yyyy";

Scanner sc=new Scanner(System.in);
String input=sc.nextLine();//now the program waits for input
Date d= new SimpleDateFormat("MM/dd/yyyy").parse("12/10/1994");
Calendar cal=Calendar.getInstance();
cal.setTime(d);

String output=new SimpleDateFormat(format).format(cal.getTime());

cal is your gregorian calendar.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top