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

时间:2016-01-31 01:35:12

标签: java formatting gregorian-calendar

我有几个名为' budGitt'的GregorianCalendar变量。和' budFrist',但当我将它们添加到我的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]

如何对其进行格式化以使其可读?

1 个答案:

答案 0 :(得分: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());
}