Avg. Rating 3.8

Problem

Preserve the date sending from/to between client UI and server back end regardless timezone

Solution

Use an intermediate variable with additional computation to do the transfer

Detailed explanation

 In this blog entry http://flexblog.faratasystems.com/?p=289, Anatole described  the underneath how BlazeDS does the data/time transfer and gave a solution to date/time transfer in Flex Application when client and server are in different timezone. But the solution is limitted to the point that, you have to set your server in GMT timezone which might affect other applications if they're running in the same VM.

 
My booking system needs the functionality to accept the entered date/time from user, persist that into the database and via versa, retrieve date/time from database and display correct to the user, regardless timezone of clients and server. I'll examine few scenarios before proposing the solution
 
Scenario 1: Client in GMT+11 sends 1/1/2009 00:00
- Actual: Server in GMT+8 receives 31/12/2008 21:00
- Expect: Server in GMT+8 receives 1/1/2009 00:00
 
Scenario 2: Server in GMT+8 sends 1/1/2009 00:00
- Actual: Client in GMT+11 receives 1st Jan 2009, 03:00
- Expect: Client in GMT+11 receives 1/1/2009 00:00
 
Scenario 3: Server in GMT+8 sends 1/1/2009 00:00
- Actual: Client in GMT-2 receives 31st Dec 2008, 14:00
 
- Expect: Client in GMT-2 receives 1/1/2009 00:00
 

Solution:

 
When sending the date/time (client or server), we advance the date/time to the offset so that when dispatching, the transferred date/time is exactly equal to the originated date/time. E.g.: Client GMT+11 sends 1/1/2009 00:00, at the client we advance the date/time to 1/1/2009 11:00 (+11 to the original date/time), hence the transferred date/time would be 1/1/2009 11:00 - 11 = 1/1/2009 00:00
 
When receiving the date/time (client or server), we deduct the date/time to the offset
 
In code, we use a intermedia variable to adjust the date/time before it is dispatched, i.e.: adjustedStartDate and adjustedEndDate as shown below. Notice that the actual date/time objects: startDate and endDate are marked as transient so that it won't get transferred.
 

Booking.as

 
package
{
[Bindable]
[RemoteClass(alias="mypackage.Booking")]
public class Booking
{
[Transient]
public var startDate:Date;
[Transient]
public var endDate:Date;
public function set adjustedStartDate(value:Date):void {
var newDate:Date = new Date(value.valueOf() - value.timezoneOffset);
this.startDate = newDate;
}
[Bindable(event="justIgnored")]
public function get adjustedStartDate():Date {
var newDate:Date = new Date(startDate.valueOf() + startDate.timezoneOffset);
return newDate;
}
public function set adjustedEndDate(value:Date):void {
var newDate:Date = new Date(value.valueOf() - value.timezoneOffset);
this.endDate = newDate;
}
[Bindable(event="justIgnored")]
public function get adjustedEndDate():Date {
var newDate:Date = new Date(endDate.valueOf() + endDate.timezoneOffset);
return newDate;
}
}
}
 

Booking.java

 
package mypackage;
 
import java.io.Serializable;
import java.util.Date;
import java.util.TimeZone;
 
/**
 * @author trung
 *
 */
public class Booking implements Serializable {
/**
*/
private static final long serialVersionUID = -4227973768782813114L;
transient private Date startDate;
transient private Date endDate;
public Date getAjustedStartDate() {
Date newDate = new Date(startDate.getTime() - TimeZone.getDefault().getOffset(startDate.getTime()));
return newDate;
}
public void setAjustedStartDate(Date value) {
Date newDate = new Date(value.getTime() + TimeZone.getDefault().getOffset(value.getTime()));
this.startDate = newDate;
}
public Date getAjustedEndDate() {
Date newDate = new Date(endDate.getTime() - TimeZone.getDefault().getOffset(endDate.getTime()));
return newDate;
}
public void setAjustedEndDate(Date value) {
Date newDate = new Date(value.getTime() + TimeZone.getDefault().getOffset(value.getTime()));
this.endDate = newDate;
}
public Date getStartDate() {
return startDate;
}
public void setStartDate(Date startDate) {
this.startDate = startDate;
}
public Date getEndDate() {
return endDate;
}
public void setEndDate(Date endDate) {
this.endDate = endDate;
}
}
 
The code works out of the box between DTOs in Flex and DTOs in Java, it is totally transparency to service layer.

 

Report abuse

Related recipes