Skip to content

Instantly share code, notes, and snippets.

@dardison
Created September 17, 2015 19:22
Show Gist options
  • Save dardison/04dfda2e5f7113c4ca01 to your computer and use it in GitHub Desktop.
Save dardison/04dfda2e5f7113c4ca01 to your computer and use it in GitHub Desktop.
package com.asbitec.vbi.framework.shared.util;
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;
/**
* Clase que encapsula la conversion de Fecha a String dependiendo de si esta en
* el Servidor o en el Cliente
*
* @author Daniel - (c)2014 ASBITEC
*
*/
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("No se puede convertir a fecha: "+ dateString);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment