2016-07-06 45 views
0

我想知道爲什麼日期格式在一些字段之間是不同的。我的原則聲明如下:Cumulocity日期格式

@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 /結束日期呈現不同?更奇怪的是,當顯示我的事件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」。我也嘗試使用cast(min(e.time),Date),但是我得到了一個錯誤(類名爲'Date'的cast函數列出的類不能被加載)。我嘗試了toDate()函數,但它沒有改變任何東西。

回答

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字符串