TimeZone conversion

Hi there,

I’ve spent a few minutes on the forum looking for a similar topic, but I found nothing.
Currently I’m using platform ver 6.10, as mentioned here in the manual:
https://doc.cuba-platform.com/manual-6.10/datatype_format_strings.html

If both fields are empty, no time zone conversions are performed for the user. Otherwise, the platform saves time zone in the UserSession object when the user logs in and uses it for displaying and entering timestamp values. The application code can also use the value returned by UserSession.getTimeZone() for custom functionality.

My server timezone is CST. I did select PDT for admin user.
Then injecting a DateField to controller:

	@Inject
	DateField fromDate;


System.out.println(userSessionSrc.getUserSession().getTimeZone());
System.out.println(fromDate.getTimeZone());
System.out.println(scheduleFrom);

What I expected is the Date object should be converted to PDT, but it was not.

Here is what it printed out:
sun.util.calendar.ZoneInfo[id=“US/Pacific-New”,offset=-28800000,dstSavings=3600000,useDaylight=true,transitions=185,lastRule=java.util.SimpleTimeZone[id=US/Pacific-New,offset=-28800000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]]_
null
Tue Jun 04 00:00:00 CDT 2019

Any suggestion would be appreciated.
Thanks

Hi,
What is wrong with the convertion?

System.out.println(scheduleFrom) uses Date.toString that uses the server time zone. You can use SimpleDateFormat with timezone for correct string representation.

Be aware that java.util.Date objects do not contain any timezone information by themselves - you cannot set the timezone on a Date object. The only thing that a Date object contains is a number of milliseconds since the “epoch” - 1 January 1970, 00:00:00 UTC.

CUBA uses SimpleDateFormat with timezone for date representation and parsing from string and doesn’t perform any date transformations.

1 Like

ok. Got that. Thanks for your response. I thought CUBA will automatically store the User timezone along with the Date object and save to DB.

However, that would be helpful if CUBA can support to store the user timezone to DB, something like using Java 8 ZonedDateTime instead of old Java Date object.

Thanks

Hi,
CUBA stores timezone on a User/UserSession objects and we convert old Java Date objects into a string presentation using user timezone.
By default ORM (JPA) doesn’t support ZonedDateTime, but it’s possible to use OffsetDateTime with CUBA and JPA.