Java 在新变量中使用startDate和endDate定义自己的日期间隔

Java 在新变量中使用startDate和endDate定义自己的日期间隔,java,jasper-reports,expression,bar-chart,Java,Jasper Reports,Expression,Bar Chart,我正在努力进行日期计算,以便为条形图系列定义自己的范围 我的动态日期是参数时间戳参数“timestamp 1”、“timestamp 2”、“timestamp 3”、“timestamp 4”,我要创建时间间隔 间隔1:“时间戳1”和“时间戳2”之间的天数 间隔2:“时间戳3”和“时间戳4”之间的天数 为了使条形图系列有两个时间间隔,我必须向变量中添加什么表达式?我对这个问题完全不知所措。正如大卫·华莱士所评论的,你应该看看这个问题。用“joda”和“interval”搜索其他问题 2.3是一

我正在努力进行日期计算,以便为条形图系列定义自己的范围

我的动态日期是参数时间戳参数“timestamp 1”、“timestamp 2”、“timestamp 3”、“timestamp 4”,我要创建时间间隔

间隔1:“时间戳1”和“时间戳2”之间的天数

间隔2:“时间戳3”和“时间戳4”之间的天数


为了使条形图系列有两个时间间隔,我必须向变量中添加什么表达式?我对这个问题完全不知所措。

正如大卫·华莱士所评论的,你应该看看这个问题。用“joda”和“interval”搜索其他问题

2.3是一个用于处理日期时间计算的开源库。Joda Time启发了java.Time.*类的创建,这些类被构建到Java8中

正如Joda Time所提供的,它完全满足您的需求:以智能的方式处理一对开始-停止日期,熟悉时区和其他异常情况

DateTime now = DateTime.now( DateTimeZone.forID( "Europe/Paris" ) );  // Current moment in Paris.
Interval interval = new Interval( now, now.plusHours( 8 ) );
要描述时间跨度(经过),请参阅和类。它们可以用人类语言(法语、英语等)来描述年、月、日、时、分、秒。它们可以用ISO 8601格式描述跨度


更多提示

要在时间上表示单个时刻,请使用类

当需要与只知道java.util.Date的其他类交互时,调用
toDate
方法


请注意,应该避免使用java.util.Date/Calendar类,因为它们的设计和实现都很糟糕。Java8将用上面提到的Java.time.*类取代它们。

我用完全不同的方法解决了这个问题。因此,如果有人想在IRPRT图表中添加时间间隔,请考虑以下内容:

我总结了SQL查询中每个时间间隔的下载数量。就像这样:

SELECT  
SUM(CASE WHEN Downloads.ExtractDate BETWEEN "$P{myTimestamp1}" AND "$P{myTimestamp2}" THEN Downloads.Downloads ELSE 0 END) AS Sum1,
SUM(CASE WHEN Downloads.ExtractDate BETWEEN "$P{myTimestamp2}" AND "$P{myTimestamp3}" THEN Downloads.Downloads ELSE 0 END) As Sum
FROM aacdb.Downloads
在iReport中,我使用以下图表系列信息创建了条形图(右键单击图表->图表数据->详细信息):

系列1

级数表达式

(new SimpleDateFormat("dd.MM.yy").format($P{myTimestamp1})) + " - " + (new SimpleDateFormat("dd.MM.yy").format($P{myTimestamp2}))
$F{Name}
$F{Sum1}
(new SimpleDateFormat("dd.MM.yy").format($P{myTimestamp2})) + " - " + (new SimpleDateFormat("dd.MM.yy").format($P{myTimestamp3}))
$F{Name}
$F{Sum2}
类别表达

(new SimpleDateFormat("dd.MM.yy").format($P{myTimestamp1})) + " - " + (new SimpleDateFormat("dd.MM.yy").format($P{myTimestamp2}))
$F{Name}
$F{Sum1}
(new SimpleDateFormat("dd.MM.yy").format($P{myTimestamp2})) + " - " + (new SimpleDateFormat("dd.MM.yy").format($P{myTimestamp3}))
$F{Name}
$F{Sum2}
值表达式

(new SimpleDateFormat("dd.MM.yy").format($P{myTimestamp1})) + " - " + (new SimpleDateFormat("dd.MM.yy").format($P{myTimestamp2}))
$F{Name}
$F{Sum1}
(new SimpleDateFormat("dd.MM.yy").format($P{myTimestamp2})) + " - " + (new SimpleDateFormat("dd.MM.yy").format($P{myTimestamp3}))
$F{Name}
$F{Sum2}
系列2

级数表达式

(new SimpleDateFormat("dd.MM.yy").format($P{myTimestamp1})) + " - " + (new SimpleDateFormat("dd.MM.yy").format($P{myTimestamp2}))
$F{Name}
$F{Sum1}
(new SimpleDateFormat("dd.MM.yy").format($P{myTimestamp2})) + " - " + (new SimpleDateFormat("dd.MM.yy").format($P{myTimestamp3}))
$F{Name}
$F{Sum2}
类别表达

(new SimpleDateFormat("dd.MM.yy").format($P{myTimestamp1})) + " - " + (new SimpleDateFormat("dd.MM.yy").format($P{myTimestamp2}))
$F{Name}
$F{Sum1}
(new SimpleDateFormat("dd.MM.yy").format($P{myTimestamp2})) + " - " + (new SimpleDateFormat("dd.MM.yy").format($P{myTimestamp3}))
$F{Name}
$F{Sum2}
值表达式

(new SimpleDateFormat("dd.MM.yy").format($P{myTimestamp1})) + " - " + (new SimpleDateFormat("dd.MM.yy").format($P{myTimestamp2}))
$F{Name}
$F{Sum1}
(new SimpleDateFormat("dd.MM.yy").format($P{myTimestamp2})) + " - " + (new SimpleDateFormat("dd.MM.yy").format($P{myTimestamp3}))
$F{Name}
$F{Sum2}

感谢大家的努力

您的计划中当前的日期是如何表示的?还有,你是否也关心一天中的时间,还是只关心没有时间的日期?我觉得这就像是一个复制品。对这个问题最好的答案是Michal的答案,接近底部,因为它正确地考虑了夏令时。顺便说一句,我将否决任何不正确处理夏令时的答案。只需减去毫秒,再除以86400000就可以了。你的时间戳是哪种格式?我的意思是,你应该有像“dd-mm-yyyy”这样的字符串形式的日期?这是一个相关的问题,是关于JasperReports的