Flex local dates/time transfer issue

First, let me explain how Flex deals with Date transfer. Dates are transfered to/from Flex client as UTC date – no timezone information available. Transfer to the UTC/local time happens automatically on protocol level. As a result, if the server does not know client’s timezone, it can not derive the entered time – rather it operates on Global time only. It means that if I am on the East coast and entered 1PM, person in Denver will see 11AM.
Depending on the type of application it can be desirable behavior – however in most of the applications it is not. Sometimes I want to enter time in the client’s local time – regardless of the timezone I am in at the time of data entry. It means that application has to operate without timezone – or for that matter in one timezone. Usually it is solved either on the server side by keeping client timezone information in session and adjusting dates on each transfer or by communicating date as String. In either case it requires a lot of code and constant tracking of the issue.

However, it can be solved much easier with usage of transient tag/keyword during data transfer:


package com.farata.datasource.dto
import flash.events.EventDispatcher;
public dynamic class LineItemDTO extends EventDispatcher //implements IManaged

private var _myDate : Date;

public function get myDateUTC() : Date{
return _myDate ==null?null:new Date(_myDate.valueOf() – _myDate.getTimezoneOffset()*60000);
public function set myDateUTC( value : Date ):void{
var oldValue:Object = _myDate;
if (oldValue !== value) {
this._myDate = value == null?null:new Date(value.valueOf() + value.getTimezoneOffset()*60000);

public function get myDate() : Date{
return _myDate;
public function set myDate( value : Date ):void{
var oldValue:Object = this._myDate;
if (oldValue !== value) {
this._myDate = value;
dispatchUpdateEvent(“myDate”, oldValue, value);

package com.farata.datasource.dto;

import java.io.Serializable;
import java.util.*;

public class LineItemDTO implements Serializable

transient public java.util.Date myDate;
public java.util.Date getMyDateUTC()
return myDate;

public void setMyDateUTC(java.util.Date value)
this.myDate = value;

That is all – you have normal public variables on both sides, and serialization works transparently, keeping Date in UTC zone on both sides (you also need to set JAVA VM timezonne to UTC) – and now you are always in the servers timezone.

Anatole Tartakovsky

PS Please evaluate your need for local vs global time – otherwise you would have cases like Dell’s support center in India when they were asking for feedback on service 8 hours before it was suppose to happen – I once got a call @ 2AM about delivery set up for 10AM – plan carefully.

8 thoughts on “Flex local dates/time transfer issue

  1. This is useful, but how do you apply it when using Farata’s DTO2FX to generate the AS DTO’s from Java? The adjusted DateUTC methods need to be manually written in the generated parent AS class, but they need to reference the data property in the generated child class. Not sure how to get round this, without using @FXIgnore on all date fields and handling them manually.

  2. Ken,
    2 Choices – either manually on DTO level rather then _generated one ( we generate 2 classes per DTO for generation gap pattern) or by modifying XSLT – either way it is quite simple.
    Thank you

  3. Thank you !
    I write your code in my two general method : setFormData(model) and getFormData(model) and it’s perfect !
    No need to write it on all dto’s

    Example : model =
    public class Evenement extends BaseObjectDao {
    [Bindable] //dateHeureFin
    public var dateHeureFin : Date;

    public function getFormData(model) {
    if (property.substring(0,4) == ‘date’) {
    var dt : Date = widget.value as Date;
    model[property] = new Date(dt.valueOf() – dt.getTimezoneOffset()*60000);

  4. A bit late on this one, but here is what I am doing so I can make the change for all dates.

    Use a custom Channel that overrides send:
    message.headers[“timezoneOffset”] = new Date().getTimezoneOffset();

    Use a custom java adapter that overrides invoke:
    int serverOffset = Calendar.getInstance().getTimeZone().getOffset(new Date().getTime()) / 1000 / 60 * -1;
    int clientOffset = (Integer) message.getHeader(“timezoneOffset”);
    if (serverOffset != clientOffset) {
    for (Object obj : ((RemotingMessage)message).getParameters()) {
    if (obj instanceof Date) {
    Date date = (Date) obj;
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.MINUTE, serverOffset – clientOffset);

  5. David,
    You need it in 2 directions – no headers on the retrieve – thus client code is preferred. You might need it on some fields, and not the others – hence transient getters/setters. Finally, if you are for server only solution, you can use IP to address DB to get rid of the extra headers and make it symmetrical – we do it for more advanced localization – be careful with mobile platforms though.
    Anatole Tartakovsky

  6. Hi, can you tell me why it is necessary to use transient also on java side ?
    In my application I use the same DTO also to call a webService and if i set a property to transient this in not working.

  7. I use SQL Convert(varchar(10),dateField,101) as dateField to return date in format “MM/DD/YYYY”. Do not use date formatter in ActionScript and it works for me.

  8. i am working on flex date. I am facing a small issue with that.

    Flex date starts with 1st jan, 1970 and i want to make it start from 1st Jan, 1960.

    for that.. i have written some code.. but its behaving perfect in some locales and for some locales, it’s not. (specifically for the ones which has daylight saving. )

    Can anyone suggest, is can this be a locale issue ? or the code one?

    the sample is code is below.

    var d:Date = new Date(2008, 4, 1); // May 1, 2008
    var baseDateTime:Number = flexDateToBaseDate(d.getTime()); // this function will convert the base of date d from 1st Jan, 1970 to 1st Jan, 1960 and convert it in the number of miliseconds since then.

    the function flexDateToBaseDate is as below.

    public static function flexDateToBaseDate(dateNum:Number):int{

    return (dateNum – datediffOffset )/millisInDay;

    datediffOffset is calculated as below

    _baseYear= 1960;
    _date = new Date( _baseYeas, 0, 1, 0, 0, 0, 0 );
    _datediffOffset= _date.getTime();

    var millisInDay:int = (24*60*60*1000);

    can you please tell me, where am doing mistake?

Comments are closed.