Calendar Constructor Java toString

前端 未结 5 1401
天命终不由人
天命终不由人 2021-01-21 01:30

What I\'m trying to do is pass a date into the Calendar so that it will format the date ready for use with another constructor. So that i can make use of it later using the func

5条回答
  •  花落未央
    2021-01-21 02:13

    First you don't need to use toString() method explicitly while printing your instances. It will be called automatically.

    Also, you should use SimpleDateFormat to format your Date to required string format: -

    Calendar cal = Calendar.getInstance();
    SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
    String date = format.format(cal.getTime());
    
    System.out.println(date);
    

    OUTPUT: -

    2012/10/20
    

提交回复
热议问题