如何使用Netbeans变量格式化程序?

如何使用Netbeans变量格式化程序?,netbeans,Netbeans,默认情况下,在Netbeans中查看对象的监视/变量时,它显示其地址而不是值。这很烦人,因为我必须展开变量以查看其实际值(例如,对于Double、Integer、Date等)。事实证明,Netbeans有“变量格式化程序”,但我几乎找不到任何关于它的文档 如何在Watches/Variables(手表/变量)窗口中以人类可读的格式显示一个简单的日期变量?我不完全理解“编辑变量格式化程序”对话框 通过使用以下代码段,我能够正确地对Double和Integer执行此操作: toString() 因此

默认情况下,在Netbeans中查看对象的监视/变量时,它显示其地址而不是值。这很烦人,因为我必须展开变量以查看其实际值(例如,对于Double、Integer、Date等)。事实证明,Netbeans有“变量格式化程序”,但我几乎找不到任何关于它的文档

如何在Watches/Variables(手表/变量)窗口中以人类可读的格式显示一个简单的日期变量?我不完全理解“编辑变量格式化程序”对话框

通过使用以下代码段,我能够正确地对Double和Integer执行此操作:
toString()

因此,代码似乎在Double/Integer类的上下文中运行。如果我需要执行更高级的操作,例如:

return DateHelpers.formatDate(dateVariableName??, "yyyy-MM-dd");

在variables视图中,您有一个小的$图标(在左上角),工具提示显示:“将变量值显示为toString()或格式化值”

只要点击它,它就会显示这些变量的“值”

编辑:如果您想添加一个变量格式化程序,它非常简单。在变量格式化程序视图上,只需单击“添加…”按钮,然后:

  • 在“格式化程序名称”中输入格式化程序的名称,例如“我的日期格式化程序”
  • “类类型”填写完整的类名,例如java.util.Date
  • 选择“作为代码段结果格式化的值”,然后键入要应用的代码。例如:
toString()

但是如果你想操纵数据或显示其他东西,你可以。例如:

toString() + " (" + getTime() + ")"
它将以人类可读的格式显示时间加上长时间


不要忘记选择视图上的$图标来应用格式化程序。

只要返回字符串,甚至可以在变量格式化程序中放入更复杂的代码。例如,如果我有一个包含两个字符串成员、名称和姓氏的类,我可以将以下代码粘贴到“值格式…”框中:


这将在变量调试窗口中以子节点的形式显示两个节点,名称和姓氏为

我的答案不会解决问题。它将与前面的答案相呼应。首先,我在NetBeans的变量面板中没有看到$符号。在当前版本中,它似乎被上下文菜单所取代

我还没有在“变量格式化程序”对话框中找到实际问题的答案,比如如何引用要调试的变量。像“这个”或“1美元”这样的东西肯定不起作用。此外,该机构似乎不了解标准的JavaJRE类,如SimpleDataFormatter

因此,在调试Java JRE类时,我想您必须接受它们提供的公共方法

如果您一直使用低于版本8的JDK(就像我一样),那么这里有一个特别用户友好的Date类的解决方法。只需通过在NetBeans中创建一个新的变量格式化程序

Tools > Options > Java > Variable Formatters > Add
然后在“类别类型”编辑字段中输入:

java.util.Date
在“作为代码段结果格式化的值”下,使用下一个代码段

// German format - "dd.MM.yyyy hh:mm"
((getDate() < 10) ? ("0" + getDate()) : getDate()) + "." + ((getMonth() < 9) ? ("0" + (getMonth() + 1)) : (getMonth() + 1) ) + "." + (getYear() + 1900) + " " + ((getHours() < 10) ? "0" + getHours() : getHours()) + ":" + ((getMinutes() < 10) ? "0" + getMinutes() : getMinutes()) + ":" + ((getSeconds() < 10) ? "0" + getSeconds() : getSeconds())

// US format - "MM/dd/yyyy hh:mm"
((getMonth() < 9) ? ("0" + (getMonth() + 1)) : (getMonth() + 1) ) + "/" + ((getDate() < 10) ? ("0" + getDate()) : getDate()) + "/" + (getYear() + 1900) + " " + ((getHours() < 10) ? "0" + getHours() : getHours()) + ":" + ((getMinutes() < 10) ? "0" + getMinutes() : getMinutes()) + ":" + ((getSeconds() < 10) ? "0" + getSeconds() : getSeconds())

// ISO-8601 - "yyyy-MM-dd hh:mm"
(getYear() + 1900) + "-" + ((getMonth() < 9) ? ("0" + (getMonth() + 1)) : (getMonth() + 1) ) + "-" + ((getDate() < 10) ? ("0" + getDate()) : getDate()) + " " + ((getHours() < 10) ? ("0" + getHours()) : getHours()) + ":" + ((getMinutes() < 10) ? ("0" + getMinutes()) : getMinutes()) + ":" + ((getSeconds() < 10) ? ("0" + getSeconds()) : getSeconds())
//德语格式-“dd.MM.yyyy hh:MM”
((getDate()<10)?((0“+getDate()):getDate())+”+((getMonth()<9)?((0“+(getMonth()+1)):(getMonth()+1))+“+”+((getYear()+1900)++((getHours()<10)?“0”+getHours():getHours())+:“+((getMinutes()<10)?“0”+getMinutes():getMinutes())+”+((getSeconds()<10)?:“+):((getSeconds()):getSeconds())
//美国格式-“MM/dd/yyyy hh:MM”
((getMonth()<9)?((0+(getMonth()+1)):(getMonth()+1))+“/”+((getDate()<10)?((0“+getDate()):getDate())+“/”+(getYear()+1900)++((getHours()<10)?“0”+getHours():getHours()+“+((getMinutes()<10)?“0”+getMinutes():getMinutes())+“+((getMinutes()):”):+((getSeconds()<10)?“+):((getSeconds())):((getSeconds()))
//ISO-8601-“yyyy-MM-dd-hh:MM”
(getYear()+1900)+“-”+((getMonth()<9)?((0+(getMonth()+1)):(getMonth()+1))+“-”+((getDate()<10)?((0+getDate()):getDate())+“+((getHours()<10)+”:“+((getMinutes()<10)?((0+getMinutes()):getMinutes())+):“+((getMinutes()):getMinutes())+):”+((getSeconds()<10):((0+getSeconds()):getSeconds())
当您在调试中丢失java.util.Calendar实例的调试输出时,下一个代码片段也会派上用场:

// German format - "dd.MM.yyyy hh:mm"
((get(5) < 10) ? ("0" + get(5)) : get(5)) + "." + ((get(2) < 9) ? ("0" + (get(2) + 1)) : (get(2) + 1) ) + "." + (get(1)) + " " + ((get(10) < 10) ? "0" + get(10) : get(10)) + ":" + ((get(12) < 10) ? "0" + get(12) : get(12)) + ":" + ((get(13) < 10) ? "0" + get(13) : get(13))
//德语格式-“dd.MM.yyyy hh:MM”
(get(5)<10);(get(5);(get(5);(get(2)<9);(get(2)+1);(get(2)+1);(get(1))+)+((get(10)<10);(get(10);(0)+get(10);(get(12)<10);(0)+get(12):get(12))+:“+((get(13)<10);“0”+get(13):get(13))

该变量本身可以被
引用(至少它在Netbeans 8.1中有效)。
因此,假设我们希望
集合的
identityHashCode
在其大小之后:

“size=“+size()+”#“+System.identityHashCode(此)

我怎么会错过这个!一定是不小心把它关掉了。谢谢是否可以将变量作为参数传递给对话框中的方法(该方法负责格式化)?它有一些默认名称吗?很抱歉,我也想知道,但我没有。在netbeans 8上,$sign功能似乎可以直接作为表模式的变量监视视图中的一个额外列使用,明白了吗。谢谢然而,对于上面有关Calendar类的示例,您需要一个如图所示的变量格式化程序,以便在该列中放入一个有意义的值。要再次提及原始问题,您尝试调试的实例的类必须始终提供一个方法,例如getValue(),以便在变量格式化程序中使用它。
// German format - "dd.MM.yyyy hh:mm"
((getDate() < 10) ? ("0" + getDate()) : getDate()) + "." + ((getMonth() < 9) ? ("0" + (getMonth() + 1)) : (getMonth() + 1) ) + "." + (getYear() + 1900) + " " + ((getHours() < 10) ? "0" + getHours() : getHours()) + ":" + ((getMinutes() < 10) ? "0" + getMinutes() : getMinutes()) + ":" + ((getSeconds() < 10) ? "0" + getSeconds() : getSeconds())

// US format - "MM/dd/yyyy hh:mm"
((getMonth() < 9) ? ("0" + (getMonth() + 1)) : (getMonth() + 1) ) + "/" + ((getDate() < 10) ? ("0" + getDate()) : getDate()) + "/" + (getYear() + 1900) + " " + ((getHours() < 10) ? "0" + getHours() : getHours()) + ":" + ((getMinutes() < 10) ? "0" + getMinutes() : getMinutes()) + ":" + ((getSeconds() < 10) ? "0" + getSeconds() : getSeconds())

// ISO-8601 - "yyyy-MM-dd hh:mm"
(getYear() + 1900) + "-" + ((getMonth() < 9) ? ("0" + (getMonth() + 1)) : (getMonth() + 1) ) + "-" + ((getDate() < 10) ? ("0" + getDate()) : getDate()) + " " + ((getHours() < 10) ? ("0" + getHours()) : getHours()) + ":" + ((getMinutes() < 10) ? ("0" + getMinutes()) : getMinutes()) + ":" + ((getSeconds() < 10) ? ("0" + getSeconds()) : getSeconds())
// German format - "dd.MM.yyyy hh:mm"
((get(5) < 10) ? ("0" + get(5)) : get(5)) + "." + ((get(2) < 9) ? ("0" + (get(2) + 1)) : (get(2) + 1) ) + "." + (get(1)) + " " + ((get(10) < 10) ? "0" + get(10) : get(10)) + ":" + ((get(12) < 10) ? "0" + get(12) : get(12)) + ":" + ((get(13) < 10) ? "0" + get(13) : get(13))