java date timezoneoffset_Java DateTimeZone.forOffsetHours方法代码示例

import org.joda.time.DateTimeZone; //导入方法依赖的package包/类

/**

* Test date histogram aggregation with hour interval, timezone shift and

* extended bounds (see https://github.com/elastic/elasticsearch/issues/12278)

*/

public void testSingleValueFieldWithExtendedBoundsTimezone() throws Exception {

String index = "test12278";

prepareCreate(index)

.setSettings(Settings.builder().put(indexSettings()).put("index.number_of_shards", 1).put("index.number_of_replicas", 0))

.execute().actionGet();

DateMathParser parser = new DateMathParser(Joda.getStrictStandardDateFormatter());

// we pick a random timezone offset of +12/-12 hours and insert two documents

// one at 00:00 in that time zone and one at 12:00

List builders = new ArrayList<>();

int timeZoneHourOffset = randomIntBetween(-12, 12);

DateTimeZone timezone = DateTimeZone.forOffsetHours(timeZoneHourOffset);

DateTime timeZoneStartToday = new DateTime(parser.parse("now/d", System::currentTimeMillis, false, timezone), DateTimeZone.UTC);

DateTime timeZoneNoonToday = new DateTime(parser.parse("now/d+12h", System::currentTimeMillis, false, timezone), DateTimeZone.UTC);

builders.add(indexDoc(index, timeZoneStartToday, 1));

builders.add(indexDoc(index, timeZoneNoonToday, 2));

indexRandom(true, builders);

ensureSearchable(index);

SearchResponse response = null;

// retrieve those docs with the same time zone and extended bounds

response = client()

.prepareSearch(index)

.setQuery(QueryBuilders.rangeQuery("date").from("now/d").to("now/d").includeLower(true).includeUpper(true).timeZone(timezone.getID()))

.addAggregation(

dateHistogram("histo").field("date").dateHistogramInterval(DateHistogramInterval.hours(1)).timeZone(timezone).minDocCount(0)

.extendedBounds(new ExtendedBounds("now/d", "now/d+23h"))

).execute().actionGet();

assertSearchResponse(response);

assertThat("Expected 24 buckets for one day aggregation with hourly interval", response.getHits().getTotalHits(), equalTo(2L));

Histogram histo = response.getAggregations().get("histo");

assertThat(histo, notNullValue());

assertThat(histo.getName(), equalTo("histo"));

List extends Bucket> buckets = histo.getBuckets();

assertThat(buckets.size(), equalTo(24));

for (int i = 0; i < buckets.size(); i++) {

Histogram.Bucket bucket = buckets.get(i);

assertThat(bucket, notNullValue());

assertThat("InternalBucket " + i + " had wrong key", (DateTime) bucket.getKey(), equalTo(new DateTime(timeZoneStartToday.getMillis() + (i * 60 * 60 * 1000), DateTimeZone.UTC)));

if (i == 0 || i == 12) {

assertThat(bucket.getDocCount(), equalTo(1L));

} else {

assertThat(bucket.getDocCount(), equalTo(0L));

}

}

internalCluster().wipeIndices("test12278");

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值