Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GWT: use DateTimeFormat on client and SimpleDateFormat on server

Tags:

I have a function that must work the same way on both client and server and it formats dates.

if (GWT.isClient())
{
  // Use DateTimeFormat
} else {
  // Use SimpleDateFormat
}

GWT complains: No source code is available for type SimpleDateFormat. The error is not fatal (at least in dev mode), but annoying and no way to suppress it. Found a similar question on http://groups.google.com/group/google-web-toolkit/browse_thread/thread/981247fca161c287 . There they suggest:

You can provide a dummy supersource implementation of SimpleDateTimeFormat so that it would compile.

I tried. Now Eclipse complains:

java.text The declared package "java.text" does not match the expected package "foo.jre.java.text" SimpleDateFormat.java

like image 835
basin Avatar asked Apr 23 '12 16:04

basin


2 Answers

You can use com.google.gwt.i18n.shared.DateTimeFormat on both server and client:

Call the protected constructor to avoid GWT.create

String pattern = "yyyyMMdd"; /*your pattern here*/ 
DefaultDateTimeFormatInfo info = new DefaultDateTimeFormatInfo();
DateTimeFormat dtf = new DateTimeFormat(pattern, info) {};  // <= trick here

Example

Date d = dtf.parse("20120301");
CalendarUtil.addDaysToDate(d, -1);
String s = dtf.format(d);
// s now contains "20120229"

The trick is done by extending the DateTimeFormat so we can use protected constructor with DateTimeFormatInfo where we use the new DefaultDateTimeFormatInfo() to avoid calling of GWT.create

like image 171
Michal Moravcik Avatar answered Sep 21 '22 04:09

Michal Moravcik


This solution is a bit different but in the same path of the one @ochakov presented but it resolves the GWT 2.7 problem @stepancheg and I mentioned.

import java.util.Date;

import com.google.gwt.core.client.GWT;
import com.google.gwt.thirdparty.guava.common.annotations.GwtCompatible;
import com.google.gwt.thirdparty.guava.common.annotations.GwtIncompatible;

public abstract class DateTimeFormat {
    static DateTimeFormat getFormat(String pattern)
    {
        if (GWT.isClient())
            return new DateTimeFormatClient(pattern);
        else
            return new DateTimeFormatServer(pattern);
    }

    public abstract String format(Date date);

    public abstract Date parse(String dateString);

    @GwtCompatible
    private static class DateTimeFormatClient extends DateTimeFormat
    {
        protected String pattern;

        public DateTimeFormatClient(String pattern)
        {
            this.pattern = pattern;
        }


        public String format(Date date)
        {
            return com.google.gwt.i18n.client.DateTimeFormat.getFormat(pattern).format(date);
        }

        public Date parse(String stringDate){
            return com.google.gwt.i18n.client.DateTimeFormat.getFormat(pattern).parseStrict(stringDate);
        }
    }

    private static class DateTimeFormatServer extends DateTimeFormatClient
    {

        public DateTimeFormatServer(String pattern)
        {
            super(pattern);
        }


        @GwtIncompatible("Server format")
        public String format(Date date)
        {
            return (new java.text.SimpleDateFormat(pattern)).format(date);
        }  

        @GwtIncompatible("Server parse")
        public Date parse(String dateString){
            try{
                return (new java.text.SimpleDateFormat(pattern)).parse(dateString);
            }catch(Exception ex){
            throw new IllegalArgumentException("Cannot convert to date: "+ dateString);
            }
        }

    }
}

Hope this help others.

like image 34
Daniel Ardison Avatar answered Sep 20 '22 04:09

Daniel Ardison