我想知道为什么某些字段之间的日期格式不同。我的规则是这样声明的:
@Name("measurement_occupation")
context ParkingSpotOccupation
insert into CreateMeasurement
select
e.source as source,
"ParkingSpotOccupation" as type,
min(e.time) as time,
{
"startDate", min(e.time),
"endDate", max(e.time),
"duration", dateDifferenceInSec(max(e.time), min(e.time))
} as fragments
from
SmartParkingEvent e
output
last when terminated;
结果如下使用API测量:
{
"time": "2016-05-30T06:00:00.000+02:00",
"id": "33200",
"self": "https://management.post-iot.lu/measurement/measurements/33200",
"source": {
"id": "26932",
"self": "https://management.post-iot.lu/inventory/managedObjects/26932"
},
"type": "ParkingSpotOccupation",
"startDate": {
"time": 1464580800000,
"minutes": 0,
"seconds": 0,
"hours": 6,
"month": 4,
"timezoneOffset": -120,
"year": 116,
"day": 1,
"date": 30
},
"duration": 600,
"endDate": {
"time": 1464581400000,
"minutes": 10,
"seconds": 0,
"hours": 6,
"month": 4,
"timezoneOffset": -120,
"year": 116,
"day": 1,
"date": 30
}
}
为什么吃时间和startDate / endDate的呈现方式不同?更奇怪的是,当我显示我的事件processig规则的输出时,它的格式如下:
{ "time": { "date": 30, "day": 1, "hours": 6, "minutes": 0, "month": 4, "seconds": 0, "time": 1464580800000, "timezoneOffset": -120, "year": 116 }, "source": "26932", "fragments": [ "startDate", { "date": 30, "day": 1, "hours": 6, "minutes": 0, "month": 4, "seconds": 0, "time": 1464580800000, "timezoneOffset": -120, "year": 116 }, "endDate", { "date": 30, "day": 1, "hours": 6, "minutes": 10, "month": 4, "seconds": 0, "time": 1464581400000, "timezoneOffset": -120, "year": 116 }, "duration", 600 ], "type": "ParkingSpotOccupation" }
所以每个日期看起来都一样,但是当我使用API访问测量时却没有。我希望所有日期都以这种格式存储:“2016-05-30T06:00:00.000 + 02:00”。我也尝试使用强制转换(min(e.time),Date),但是我收到了一个错误(无法加载名为'Date'的强制转换函数中列出的类)。我尝试了toDate()函数,但它没有改变任何东西。
答案 0 :(得分:2)
问题在于,在Esper中,所有日期实际上都是Java中的Date类,在解析它时,你得到的结构不是很好。
最简单的方法是自己将其格式化为ISO字符串。 您可以使用Java SimpleDateFormat。在模块中声明它。
create constant variable SimpleDateFormat ISO_FORMATTER = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
然后只使用它
ISO_FORMATTER.format(min(e.time))
ISO_FORMATTER.format(max(e.time))
并返回日期的ISO字符串