Using GregorianCalendar with SimpleDateFormat

后端 未结 5 1941
渐次进展
渐次进展 2020-12-03 04:45

So, I\'ve been racking my brain over this (should-be) simple exercise to make the program turn a date string into a GregorianCalendar object, format it, and ret

5条回答
  •  天命终不由人
    2020-12-03 05:19

    SimpleDateFormat.format() method takes a Date as a parameter. You can get a Date from a Calendar by calling its getTime() method:

    public static String format(GregorianCalendar calendar) {
        SimpleDateFormat fmt = new SimpleDateFormat("dd-MMM-yyyy");
        fmt.setCalendar(calendar);
        String dateFormatted = fmt.format(calendar.getTime());
    
        return dateFormatted;
    }
    

    Also note that the months start at 0, so you probably meant:

    int month = Integer.parseInt(splitDate[1]) - 1;
    

提交回复
热议问题