How can I utilize SimpleDateFormat with Calendar?

后端 未结 4 1645
长发绾君心
长发绾君心 2020-12-07 22:46

I\'ve got GregorianCalendar instances and need to use SimpleDateFormat (or maybe something that can be used with calendar but that provides required #fromat() feature) to ge

相关标签:
4条回答
  • 2020-12-07 22:50

    Try this:

    Calendar cal = new GregorianCalendar();
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
    dateFormat.setTimeZone(cal.getTimeZone());
    System.out.println(dateFormat.format(cal.getTime()));
    
    0 讨论(0)
  • 2020-12-07 23:02

    Simply call calendar.getTime() and pass the resulting Date object to the format method.

    0 讨论(0)
  • 2020-12-07 23:04

    eQui's answer is missing a step

    Calendar cal = new GregorianCalendar();
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
    #---- This uses the provided calendar for the output -----
    dateFormat.setCalendar(cal); 
    System.out.println(dateFormat.format(cal.getTime()));
    
    0 讨论(0)
  • 2020-12-07 23:14

    Calendar.getTime() returns a Date which can be used with SimpleDateFormat.

    0 讨论(0)
提交回复
热议问题