0

I have a formatted Date in EDT timezone. When I store this value in java.util.Date object and try to print or display it , it again changes the TimeZone to my local Timezone. This happens on gwt client side.

How can I keep the timezone same after storing it in a date object ?

Please help.

Prakhar
  • 92
  • 12
  • I think it answered here: http://stackoverflow.com/questions/2891361/how-to-set-time-zone-of-a-java-util-date – Fady Saad Apr 05 '17 at 03:11
  • I have checked this thread. It is not related to GWT. GWT client side supports less number of util packages because of which we cant use many of the APIs on client side. Thats why I want to know a way out using gwt. – Prakhar Apr 05 '17 at 03:13
  • Sorry that's my bad – Fady Saad Apr 05 '17 at 03:15
  • Some good date time libraries are listed [here](http://stackoverflow.com/q/10311754/1495050) – Daniel Bickler Apr 05 '17 at 03:31
  • This has been handled many many many times already on Stack Overflow. Many people have explained the `toString` method on `Date` applies a time zone while generating the string, as a poorly designed anti-feature. And many people have advised avoiding those old legacy date-time classes that have been supplanted by the java.time classes. Please search Stack Overflow thoroughly before posting. Also, I get [searching for "gwt date"](http://stackoverflow.com/search?q=gwt+date) gives me 801 hits. – Basil Bourque Apr 05 '17 at 05:54
  • The guy who is marking this as a duplicate / already has an answer. This is about GWT, not about Java. DateFormat does not work in GWT as proposed in the 'already has an answer thread..... – Knarf Apr 05 '17 at 06:53
  • This is a hard problem to fix in GWT. Since it always forces times in the timezone of the browser (I think this is also the case in vanilla javascript). Maybe you can look into moment.js (https://momentjs.com/ or gwt-wrapper https://github.com/ahome-it/moment4j). Never used it but I believe it was developed because to solve these kinds of problems. – Knarf Apr 05 '17 at 06:56
  • @Knarf I would be glad to vote to reopen this Question if edited to explain how none of the [801 hits for "gwt java" search](http://stackoverflow.com/search?q=gwt+date) address this issue. – Basil Bourque Apr 06 '17 at 00:53

1 Answers1

0

When you store the date in to java.util.Date, it gets converted to UTC timezone. You need to use SimpleDateFormat and specify the timezone you want and convert it. Please see the below for doing the conversion.

https://www.google.co.in/amp/www.journaldev.com/696/how-to-convert-java-date-into-specific-timezone-format/amp

OTM
  • 656
  • 5
  • 8
  • 1
    I agree but GWT does not support SimpleDateFormat package on client side. While compiling it throws an error saying No source code found for SimpleDateFormat class. – Prakhar Apr 05 '17 at 03:29
  • Okay, you can use DateTimeFormat class instead. Please see the http://stackoverflow.com/questions/24054410/gwt-timezone-issue for the conversion in gwt. – OTM Apr 05 '17 at 03:39