Issue in Date conversion in microservice

Hello All,

This the the code i am using to convert date in different time zone date but it is not working on Cumulocity tenant where as on local it is working fine.

private String getGMTTimeZoneDate(String date) throws ParseException {

	DateTimeZone gmtTime = DateTimeZone.forTimeZone(TimeZone.getTimeZone("GMT"));
	SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMM dd yyyy hh:mm:ss aa");

	DateTime timeZoneStartDate = new DateTime(simpleDateFormat.parse(date));
	return timeZoneStartDate.withZone(gmtTime).toString();
}

Local date conversion output

Input IST date : Mar 04 2024 05:51:12 PM
Output GMT date : 2024-03-04T12:21:12.000Z

On dev Cumulocity tenant conversion output

Input IST date : Mar 04 2024 05:51:12 PM
Output GMT date : 2024-03-04T17:51:12.000Z (Not converted as per given time zone)

Let me know , what is wrong with this.

Regards/-
Prakahs

Hi Prakash,

the SimpleDateFormat is using the timezone in which you are running the microservice. If you are running locally it is IST and a conversion is happening to GMT but if you are running inside Cumulocity, the timezone of the microservice is already GMT thus no conversion takes place.

SimpleDateFormat has a setTimezone method that you can use to provide a different timezone.

Best regards,
Harald

What’s the use of the date conversion? If possible I would store all kind of timestamps in UTC format in Cumulocity. The conversion to the target timezone is normally automatically done in the UIs or 3rd Party Applications. In Cumulocity the timezone is retrieved from the client/browser settings and converted automatically to the target timezone.

1 Like