getDay(),getMonth(),getYear()
我有一个名为 $F{Fecha} 的时间戳文本字段,我想从中获取日、月和年。我创建了 3 个变量 var1、var2、var3,并在它们的表达式中添加了以下 $F{Fecha}.getDay()、$F{Fecha}.getMonth()、$F{Fecha}.getYear() ;它给了我一个返回值,但它们是错误的值,即如果我的日期是20120118,它会返回day=3,month=0,yearh=112。
我该如何解决这个问题? 提前致谢。
PD。我使用的是iReport 4.0.0
I have a timestamp textField called $F{Fecha}, and I want to get from it the day,month, and year. I've create 3 variables var1,var2,var3 and in their expressions I've put the following $F{Fecha}.getDay(), $F{Fecha}.getMonth(),$F{Fecha}.getYear(); it gives me in return a value but they're wrong values,ie if my date is 20120118 it returns me day=3, month=0,yearh=112.
How can I do to fix this?
Thanks in advance.
PD. I'm using iReport 4.0.0
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
下面是使用 Java 表达式实现这一点的方法:
Day:
new Integer($F{Fecha}.getDate())
月份:
new Integer($F{Fecha}.getMonth()+1)
年:
新整数($F{Fecha}.getYear()+1900)
Here is a way to do it with Java expressions:
Day:
new Integer($F{Fecha}.getDate())
Month:
new Integer($F{Fecha}.getMonth()+1)
Year:
new Integer($F{Fecha}.getYear()+1900)
我同意亚历克斯的观点:通过字符串解析来提取日期信息是一个坏主意。例如:使用不同的区域设置运行报表,您会发现报表以意想不到的方式中断。
Java 没有为此目的内置非常好的日期处理。但是您可以使用 Joda-Time 或 Apache Commons Lang 来获取许多有用的功能。
I'm with Alex: string parsing to pull out date information is a bad idea. For example: run the report using a different Locale, and you'll find the your report breaks in unexpected ways.
Java doesn't have very good date handling built in for this purpose. But you can use Joda-Time or Apache Commons Lang to get a bunch of helpful functions.
如果可能的话,您可以在 SQL 中执行此操作。
MySQL 有 MONTH()、DAY() 和 YEAR() 函数可以执行此操作。
If possible, you could do this in the SQL.
MySQL has MONTH(), DAY() and YEAR() functions that would do this.