3

I'm trying to figure out how to use SmartGWT's TimeItem, Calendar and DateItem. I'm stuck since all of these components convert their data to client's timezone. For the sake of the argument let us take TimeItem.

I have a DynamicForm which has a TimeItem inside if you set the value to (Java.Util.Date)12:00 the dynamicform displays 19:30. If you get that value via TimeItem.getValue it returns (com.smartgwt.client.util.LogicalTime)13:30. The code example is below

    DateTimeFormat timeFormatter = DateTimeFormat.getFormat("HHmmss");

    layout = new VLayout();
    form = new DynamicForm();
    item = new TimeItem("TIME","TIME");
    form.setItems(item);
    layout.addMember(form);
    Button btn = new Button("put");
    btn.addClickHandler(new ClickHandler() {

        @Override
        public void onClick(ClickEvent event) {
            Date parse = timeFormatter.parse("120000");
            System.out.println(parse.toString());// prints out Tue Aug 20 12:00:00 VET 2013
            form.getField("TIME").setValue(parse); // sets 19:30 seen by the user
            //form.getField("TIME").setValue("120000");
        }
    });

    Button btn2 = new Button("get");
    btn2.addClickHandler(new ClickHandler() {

        @Override
        public void onClick(ClickEvent event) {
            Object value = form.getField("TIME").getValue();
            System.out.println(value.toString());// prints out Thu Jan 01 13:30:00 VET 1970
            //However if the value is set using a simple string,
            //(like so "form.getField("TIME").setValue("120000");")
            //this line
            //prints out Thu Jan 01 06:00:00 VET 1970
        }
    });

    layout.addMember(btn);
    layout.addMember(btn2);

    layout.draw();

My computers timezone is set to: (UTC+02:00) Istanbul, if that clears anything up

Thank you in advance for any help.

Berker Soyluoglu
  • 198
  • 2
  • 14
  • You need to use GWT DateTimeFormat to reformat the time into the timezone you desire. – Engineer2021 Aug 20 '13 at 18:52
  • That is only possible for the output, which is when I'm trying to get the value. Also in my opinion that does not seem to be the ideal solution is there a solution that I can get the exact value entered by the user (not the string but the date object). Besides, the problem with the set is still there. Do you have other suggestions regarding the set issue? Thank you in advance – Berker Soyluoglu Aug 21 '13 at 12:49
  • I am currently using the RelativeDateItem with a separate label for showing what I call the "calculated date time". So that the user entered time is shown in local time and to the right of the relative date item, it shows the time in UTC. This was the recommended approach by Isomorphic. – Engineer2021 Aug 21 '13 at 12:52
  • Thanks I can try to change my DateItem's into RelativeDateItem but the Calendar and TimeItem problems still remains. (Assuming that you can't hide datepicker from RelativeDateItem) – Berker Soyluoglu Aug 21 '13 at 13:10
  • You can call setShowPickerIcon(false); on RelativeDateItem and then hide the calculated field so that you can add your own StaticTextItem adjacent to the RelativeDateItem if you want to show say UTC in the StaticTextItem and Client time in the drop-down. – Engineer2021 Aug 21 '13 at 13:47
  • Here is the code to make a system-wide change to the date format. http://pastebin.com/vu4Qe4nn – Engineer2021 Aug 21 '13 at 13:50
  • You then need code to format the StaticTextItem in UTC, for instance – Engineer2021 Aug 21 '13 at 13:52

2 Answers2

2

This is really interesting a friend of mine told me that this was due to some annoying behaviour on Windows. I've no idea why or how but the solution is as follows. If my time zone is set to +2 Istanbul the above bug can be reproduced however if i change my time zone settings on my machine to Athens +2 the bug disappears so... All I did was change my settings such that my time zone is set to Athens +2. Hope this helps anybody in the future.

Berker Soyluoglu
  • 198
  • 2
  • 14
1

Have you used following (probably as a workaround to some other issue)?

DateUtil.setDefaultDisplayTimezone("00:00");

By default, SmartGWT uses the browser timezone to render date/time values.
setDefaultDisplayTimezone() is used to globally set the UTC offset, that is used during date/time formatting.

However, DateTimeFormat is a GWT class and is not affected by setDefaultDisplayTimezone(), which is SmartGWT.
GWT by default uses the client’s local time zone.

Based on code comments, VET is "Venezuelan Standard Time", which is "UTC-04:30 hours".
When "120000" is parsed without specifying a timezone, its parsed into "12:00:00 UTC-04:30" by GWT.
This is converted back to UTC+00:00 by SmartGWT as per the setDefaultDisplayTimezone() value, causing "19:30:00 UTC+00:00" to be shown.

When setDefaultDisplayTimezone() is not used or setDefaultDisplayTimezone("-04:30"); is used

System.out.println(parse.toString());  // => 12:00:00 VET
form.getField("TIME").setValue(parse); // => 12:00

When DateUtil.setDefaultDisplayTimezone("00:00"); is used

System.out.println(parse.toString());  // => 12:00:00 VET
form.getField("TIME").setValue(parse); // => 19:30

If default timezone must be set to UTC+00:00 in SmartGWT, something similar to following can be used

Date parse = DateTimeFormat.getFormat("HHmmss Z").parse("120000 +00:00");

System.out.println(parse.toString());  // => 19:30:00 VET
form.getField("TIME").setValue(parse); // => 12:00

Still, beware of DST changes, etc.

Also, according to SmartClient support

Stored datetime values are milliseconds since epoch.
They do not *have a* timezone, they are *rendered in* a timezone.
Sithsu
  • 2,209
  • 2
  • 21
  • 28
  • Very detailed answer thank you very much, but I haven't used DateUtil.setDefaultDisplayTimezone("00:00"); I did try it at some point but removed it since i didn't help. However your answer gives very detailed insight on GWT DateTime. I have decided to use Strings instead of SmartGWT TimeItem (still working on Calender though). Yet i'm still wondering what actually caused this problem. – Berker Soyluoglu Aug 27 '13 at 06:06