如何在 Java 中正确显示 GregorianCalendar 实例?

How do I properly display GregorianCalendar instances in Java?

我有几个名为 'budGitt' 和 'budFrist' 的 GregorianCalendar 变量,但是当我将它们添加到我的 toString 方法时

public String toString() {
    return budGitt + "\t" + budFrist + "\t" +.......
}

每个实例的输出如下所示

java.util.GregorianCalendar[time=1454203447510,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Europe/Oslo",offset=3600000,dstSavings=3600000,useDaylight=true,transitions=141,lastRule=java.util.SimpleTimeZone[id=Europe/Oslo,offset=3600000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=2,startMonth=2,startDay=-1,startDayOfWeek=1,startTime=3600000,startTimeMode=2,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=3600000,endTimeMode=2]],firstDayOfWeek=2,minimalDaysInFirstWeek=4,ERA=1,YEAR=2016,MONTH=0,WEEK_OF_YEAR=4,WEEK_OF_MONTH=4,DAY_OF_MONTH=31,DAY_OF_YEAR=31,DAY_OF_WEEK=1,DAY_OF_WEEK_IN_MONTH=5,AM_PM=0,HOUR=2,HOUR_OF_DAY=2,MINUTE=24,SECOND=7,MILLISECOND=510,ZONE_OFFSET=3600000,DST_OFFSET=0] java.util.GregorianCalendar[time=1454246647509,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Europe/Oslo",offset=3600000,dstSavings=3600000,useDaylight=true,transitions=141,lastRule=java.util.SimpleTimeZone[id=Europe/Oslo,offset=3600000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=2,startMonth=2,startDay=-1,startDayOfWeek=1,startTime=3600000,startTimeMode=2,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=3600000,endTimeMode=2]],firstDayOfWeek=2,minimalDaysInFirstWeek=4,ERA=1,YEAR=2016,MONTH=0,WEEK_OF_YEAR=4,WEEK_OF_MONTH=4,DAY_OF_MONTH=31,DAY_OF_YEAR=31,DAY_OF_WEEK=1,DAY_OF_WEEK_IN_MONTH=5,AM_PM=1,HOUR=2,HOUR_OF_DAY=14,MINUTE=24,SECOND=7,MILLISECOND=509,ZONE_OFFSET=3600000,DST_OFFSET=0]

如何格式化它以使其可读?

我不确定您要查找的确切输出,但您应该使用 SimpleDateFormat 以使其更友好。这是一个例子。

public String getFriendlyCalendar(GregorianCalendar calendar){
    SimpleDateFormat dateTimeFormat= new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
    dateTimeFormat.setCalendar(calendar);
    return dateTimeFormat.format(calendar.getTime());
}