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

I am struggling with a date calculation to define my own range for a bar chart series.

My dynamic dates are parameters the timestamp parameters "timestamp1", "timestamp2", "timestamp3", "timestamp4" wheras I want to create the intervals

interval1: days between "timestamp1" and "timestamp2"

interval2: days between "timestamp3" and "timestamp4".

What is the expression that I have to add to my variables in order to have two time intervals for my bar chart series? I am completely lost with this issue.

解决方案

As commented by David Wallace, you should look at the question Calculating the Difference Between Two Java Date Instances. And search for other questions with "joda" and "interval".

Joda-Time 2.3 is an open-source library for handling date-time calculations. Joda-Time has inspired the creation of the java.time.* classes being built into Java 8.

As discussed in the doc, Joda-Time offers the Interval class for exactly your needs: Handling a pair of start-stop dates in an intelligent manner, savvy with time zones and Daylight Savings Time (DST) and other anomalies.

DateTime now = DateTime.now( DateTimeZone.forID( "Europe/Paris" ) ); // Current moment in Paris.

Interval interval = new Interval( now, now.plusHours( 8 ) );

To describe the time span (elapsed), see the Period and Duration classes. They can describe the years-months-days-hours-minutes-seconds in human language (French, English, etc.). They can describe the span in ISO 8601 format of PnYnMnDTnHnMnS.

Further tips…

For representing a single moment in time, use the DateTime class.

When necessary to interact with other classes that know only java.util.Date, call the toDate method.

Note that you should avoid using the java.util.Date/Calendar classes, as they are badly designed and implemented. Java 8 will supplant them with the java.time.* classes mentioned above.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值