Search code examples
javagregorian-calendar

Java Console input and print a GregorianCalendar


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


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.