How to filter measurements on the basis of UTC Date and Time

Product/components used and version/fix level:

Cumulocity Production

Detailed explanation of the problem:

Hi, I am using Cumulocity Java SDK to fetch measurements. I am using Measurement Filter class to fetch the measurements on date basis. What I am trying to do is I want to fetch my measurements on the basis of date like dateTo will be 2023-06-11T00:00:00.000Z and dateFrom will be 2023-06-04T00:00:00.000Z but as the measurementFilter byDate method accepts a java.util.Date object only so when converting it into date it automatically converts my string date to Sun Jun 11 00:00:00 UTC 2023.

Date dateTo = CommonUtil.timestampByInterval(0);

Date dateFrom = CommonUtil.timestampByInterval(-7);

public static Date timestampByInterval(int interval) {
        try {
            Calendar cal = Calendar.getInstance();

            cal.add(Calendar.DATE, interval);

            Date date = cal.getTime();

            DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");

            String resultDate = format.format(date);

            String[] str = resultDate.split("T");

            resultDate = str[0] + "T00:00:00.000Z";

            System.out.println("resultDate: "+resultDate);

            final DateTimeFormatter dateTimeFormat = ISODateTimeFormat.basicDateTime();

            DateTime dateTime = new DateTime(resultDate,DateTimeZone.UTC);
            System.out.println("Date time format: "+dateTimeFormat.withZoneUTC().print(dateTime));

            date = dateTime.withZone(DateTimeZone.UTC).toDate();
            return date;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


MeasurementFilter measurementFilter = new MeasurementFilter().bySource(new GId(device)).byType(type).byDate(dateFrom,dateTo);

Now when I pass this date to byDate method of measurementFilter it again converts it into string but it by default takes my IST Timezone and appends +5:30 automatically to my date.

measurementApi.getMeasurementsByFilter(measurementFilter).get(2000, RevertQueryParameter.getInstance())
Response: 
{"next":"{devURL}/measurement/measurements?dateTo=2023-06-11T05:30:00.000%2B05:30&pageSize=2000&revert=true&source=22687&dateFrom=2023-06-04T05:30:00.000%2B05:30&type=type&currentPage=2","self":"{devURL}/measurement/measurements?dateTo=2023-06-11T05:30:00.000%2B05:30&pageSize=2000&revert=true&source=22687&dateFrom=2023-06-04T05:30:00.000%2B05:30&type=type&currentPage=1","statistics":{"currentPage":1,"pageSize":2000},"measurements":[]}

How can I set UTC timezone instead of IST and get data based on my time that I am sending? Is there any other way to set the time or is there any other method that I can use to filter measurements on time.

Regards,
Samanyu

Hi,

I solved it by creating a creating a Filter class and extending Measurement Filter class. Copy all the content of measurementFilter class and use your own filter class to filter the measurements and pass it to other methods.

Hope this helps. :slight_smile:

Thanks & Regards,
Samanyu

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.