在贾斯珀报告中,我正在尝试以下操作
new SimpleDateFormat("dd.MM.yyyy HH:mm").setTimeZone(TimeZone.getTimeZone("UTC")).format( new Date(1483337940000L));
在文本字段表达式中。
但我收到错误
Cannot invoke format(Date) on the primitive type void
是不是和java中下面的代码是一样的?
1483337940000L is 02.01.2107 07:19
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
String test = sdf.format(new Date(1483337940000L)); // Here I am getting 06:19
String test2 = new SimpleDateFormat("dd.MM.yyyy HH:mm").setTimeZone(TimeZone.getTimeZone("UTC")).format( new Date(1483337940000L)); // Here I am getting the error Cannot invoke format(Date) on the primitive type void
最佳答案
方法setTimeZone()来自 DateFormat 类;并且不返回值;因为它是无效!
当你写作时
sdf.setTimeZone(TimeZone.getTimeZone("UTC")).format(new Date(1483337940000L));
您尝试对 setTimeZone() 的结果调用 format()
。
但是由于该方法没有结果;那一定会失败!
这里真正的要点是:不要假设方法正在做什么。是的,一些 API 被编写为允许“流畅调用”,通过返回调用方法的对象;但不是全部!因此,如有疑问:请检查 javadoc。首先。
关于java - 无法对原始类型 void 调用 format(Date),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43093319/