简体   繁体   中英

How do you display Calendar's in Java in a human-readable way?

I have two array lists consisting of Calendar objects. I need these two lists to contain the same objects and in the same order - but this isn't the case. I ran the program on debugger mode to check the content of the lists but the object descriptions are gibberish. I just want to know if the year, month and day are the same at each step.

This is what my debugger shows:

在此处输入图片说明

When I click the arrow next to each index to reveal more information I still can't find what I need. Is there a way around this? Or perhaps there is another debugging tool that I am unfamiliar with? At this stage my debugging skills are limited to placing breakpoints and clicking the bug icon.

Appreciate any help.

  • Right-click on one of those calendars.
  • Choose Customize data views...
  • Go to the second tab: Java data type renderers
  • Click the + icon and name your renderer (calendar, for example)
  • Type java.util.Calendar in the box "Apply renderer to objects of type..."
  • Check the radio button "Use following expression"
  • Type the following expression (for example): new SimpleDateFormat("yyyy-MM-dd").format(getTime())
  • Click OK.

If you need to deal with TimeZones, you can use this ampliation to JB Nizet solution:

new SimpleDateFormat("dd-MM-yyyy HH:mm:ss.SSS").format(getTime()) + " " + getTimeZone().getDisplayName(false,TimeZone.SHORT) + " " + getTimeZone().getRawOffset()/3600000

Example output:

在此处输入图片说明

Shows the TimeZone name and the offset in hours

Global configuration is also possible, go here to view all active detail formatter and to add new ones: Preferences > Java > Debug > Detail Formatter

Qualified type name: java.util.Calendar

Detail formatter code snippet returns the String you defined, in my case german format of date + time:

this.get(Calendar.DAY_OF_MONTH ) + "." + 
(1 + this.get(Calendar.MONTH ) ) + "." + 
this.get( Calendar.YEAR )

This is what it looks like in preferences: detailFormatterCalendar

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM