From few days I am fighting with different Timezone issues of server and GWT client. but cannot get any success.
Scenario is Server is in UTC timezone let say Client A is in IST timezone.
When client select a date (with time) I pushed to server but date is automatically changed to server's timezone. I dig around this issue and I found multiple solutions like
create custom serializer (No idea how to do that can't found any proper example)
transfer date as a string to server and convert it to server timezone and store it. and when fetching data convert again from server's timezone to client local timezone. sounds good idea.
So my query is.
Any other solutions?
which is best way to manage this ?
any sample code or link?
Simply format the date in UTC format at client side and pass the date as string to the server and store the data in UTC format in database as well.
On the server side everything would be fine, since formatting the date would use the timezone of the server, which is what the date is stored in. On the client side, however, GWT will use the timezone of the client machine, and so there could be a discrepancy.
Sample code:
DateTimeFormat f = DateTimeFormat.getFormat("MMM dd yyyy");
TimeZoneConstants t = (TimeZoneConstants) GWT.create(TimeZoneConstants.class)
TimeZone est = TimeZone.createTimeZone(t.americaNewYork());
int offset = est.isDaylightTime(date) ? +240 : +300;
TimeZone tz = TimeZone.createTimeZone(offset);
String date = f.format(user.getBirthDate(), est);
There are a few other possible solutions, but one of these two might do the trick.
How do I get GWT DateTimeFormat to display using the server TimeZone, rather than the client's?
Related
I want to store all date time and when a user wants to get information. The server must to return data with datetime and 3rd party devices (like Android , IOS, Web apps) should convert that datetime for specific timezone. The basic goal is store all date timezone with "0" timezone
You can use Java8 ZonedDateTime to get time in UTC. My assumption is 0 timezone means UTC
ZonedDateTime.now(ZoneOffset.UTC);// This will give current time in UTC
To convert ZontedDateTime , You can put GMT,US/Central etc in the config file
public static ZonedDateTime getDateinTimeZone(String timeZone,String dateInSting )
{
return ZonedDateTime.parse(dateInSting).now(ZoneId.of(timeZone));
}
I have problems with formatting server time. Server is on central time I guess. I need to format following string 2016-08-22T10:29:22 in default zone (Central European Summer Time = GMT+2). I tried with Joda-Time library, I need to get 12:29:22, but I only managed to get same date with +02:00 the end, with code like this:
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss");
DateTime dateTime = formatter.withZone(DateTimeZone.getDefault()).parseDateTime(time);
Output of this code is: 2016-08-22T10:29:22.000+02:00, when I try to dateTime.getHourOfDay(); - I getting 10 again.
Where am I going wrong?
2016-08-22T10:29:22 in default zone (Central European Summer Time = GMT+2).
10:29 in GMT+2 is 10:29 in GMT+2 and 8:29 in GMT, and the 2016-08-22T10:29:22 simply lacks information of timezone. So either make server return timezone or manually add it (i.e. append Z) prior converting.
I am working on Spring MVC project. It is an quasi online system where each client will install our system (Tomcat n Mysql will get installed through an installer) on their machine. They will download the data by connecting their machine to internet. Once data get downloaded they can disconnect from internet.
By considering above scenario, we want to validate the system date n time is correct according to time zone. I have checked How to get local time of different time zones?, The code :
java.util.TimeZone tz = java.util.TimeZone.getTimeZone("IST");
java.util.Calendar c = java.util.Calendar.getInstance(tz);
System.out.println(c.get(java.util.Calendar.HOUR_OF_DAY)+":"+c.get(java.util.Calendar.MINUTE)+":"+c.get(java.util.Calendar.SECOND));
Give the same time of the system. I want something which will tell time according to the time zone. Same as like when we set time zone on OS clock it will automatically set the correct date and time according to that time zone.
Implementation like this:
Date accurateTimeZoneDate = //HERE I WANT SOMETHING TO GET DATE ACCORDING TO TIME ZONE.
Date machineCurrentDate = new Date();
if(accuratetimeZoneDate == machineCurrentDate)
{
//machine date and time zone date is correct.
}
else
{
//machine date and time zone date is NOT correct.
}
Update
We have tried this:
Daily it is mandatory to connect the system to internet so that application will ping to an central ntp and get the time and validate. once validation is successful then they can disconnect from internet. But in this case after validation they switch to some old date and use the expired content.
Ok if you want system time and date then you should try new Date().
it will give current date time with timezone.
Instead of using Calendar, you should use new Date()
System.out.println("Current Date And Time"+ new Date());
In my Android application server will return some UTC date in following format(yyyy-MM-dd HH:mm:ss) 24hours and I need to convert those time into user's TimeZone for example CST, IST.
I did the following code but I do know is it correct or not, please assist me to do the time zone conversion in right way.
I get UTC date as json string and converting into user's time zone format and showing Android side
private static Date utcDate;
private static DateFormat expireFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
try {
expireFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
utcDate = expireFormat.parse("2014-04-01 10:32:00");
System.out.println(TimeZone.getDefault().getID());
expireFormat.setTimeZone(TimeZone.getTimeZone(TimeZone.getDefault().getID()));
System.out.println(expireFormat.format(utcDate));
} catch (ParseException e) {
e.printStackTrace();
}
output of the code:
Asia/Calcutta
2014-04-01 16:02:00
The overall approach is OK if a re-formatted String is really what you're trying to get.
There are some issues though
SimpleDateFormat is not a threadsafe class. Setting it to a static field inside a server is a problem!
Same as #1 regarding using a static field to hold the intermediate Date object.
Is "CST" China Standard Time? Central Standard Time (US or Australia)? Cuba Standard Time? Three letter abbreviations TimeZone are bad news in general. Try to use an Olson Name or Alias if at all possible.
Is this the server side or the android? If it's a server, you could probably benefit from the new Java 8 API for DateTime handling.
I am having a hard time trying to make a web service client work. It is a XML RPC specification. I am using Apache WS XML-RPC library, which I find full of holes that causes problem due to Serialization. I have to send a Date parameter for the library to add the tags , however the web service expects it with the TZ, that means adding -0500 at the end of the Date object. If I dont send it as Date Object, it wont add the tags and it will fail. And when trying to do this:
DateFormat df = new SimpleDateFormat("yyyyMMdd'T'HH:mm:ssZ");
String fecha = df.format(new Date());
Date date = new SimpleDateFormat("yyyyMMdd'T'HH:mm:ssZ").parse(fecha);
And using parameter date, and it always sends it as
<dateTime.iso8601>20130517T20:30:33</dateTime.iso8601>
Can't find a way for it to send it as Date object in the format above but with the -0500 at the end. Any help would be appreciated.