Skip to content

Instantly share code, notes, and snippets.

@dardison
Created September 1, 2015 13:49
Show Gist options
  • Save dardison/a910518661ab83d4b9a9 to your computer and use it in GitHub Desktop.
Save dardison/a910518661ab83d4b9a9 to your computer and use it in GitHub Desktop.
StringDate
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 DateTimeFormatClient.getFormat(pattern);
else
return DateTimeFormatServer.getFormat(pattern);
}
public abstract String format(Date date);
public abstract Date parse(String dateString);
@GwtCompatible
private static class DateTimeFormatClient extends DateTimeFormat
{
private com.google.gwt.i18n.client.DateTimeFormat dateTimeFormat;
protected DateTimeFormatClient(String pattern)
{
this.dateTimeFormat = com.google.gwt.i18n.client.DateTimeFormat.getFormat(pattern);
}
public static DateTimeFormat getFormat(String pattern)
{
return new DateTimeFormatClient(pattern);
}
public String format(Date date)
{
return dateTimeFormat.format(date);
}
public Date parse(String stringDate){
return dateTimeFormat.parseStrict(stringDate);
}
}
@GwtIncompatible("Server version of the class")
private static class DateTimeFormatServer extends DateTimeFormat
{
private java.text.SimpleDateFormat dateTimeFormat;
protected DateTimeFormatServer(String pattern)
{
this.dateTimeFormat = new java.text.SimpleDateFormat(pattern);
}
public static DateTimeFormat getFormat(String pattern)
{
return new DateTimeFormatServer(pattern);
}
public String format(Date date)
{
return dateTimeFormat.format(date);
}
public Date parse(String dateString){
try{
return dateTimeFormat.parse(dateString);
}catch(Exception ex){
throw new IllegalArgumentException("No se puede convertir a fecha: "+ dateString);
}
}
}
}
package com.asbitec.vbi.framework.shared.util;
import java.io.Serializable;
import java.util.Date;
/**
* Clase que brinda soporte a una fecha/hora inmutable entre el Cliente y el Servidor.<br>
* <p><b>NOTA IMPORTANTE:</b> Todas Las Fechas que se Manejen en los DTO, Process, Result y cualquier
* otro objeto que se pase entre el Cliente y el Servidor via RPC deben utilizar esta clase.<br>
* Esta clase guarda internamente la representacion String de la Fecha y Hora para lograr su inmutabilidad
* cuando se pase entre el Cliente y el Servidor (Independiente de la TimeZone).<br>
* Provee metodo para obtener el Date correspondiente ver {@link StringDate#getDate()}
*
* @author Daniel H. Ardison - ASBITEC
*
*/
@SuppressWarnings("serial")
public class StringDate implements Serializable {
private static final String SERIALIZED_FORMAT = "dd/MM/yyyy HH:mm:ss";
private String stringDate;
public StringDate(){
this.stringDate=null;
}
public StringDate(Date date){
if(date==null){
this.stringDate=null;
}else{
this.stringDate=StringDate.getDateString(date);
}
}
/**
* Obtiene el Date
* @return
*/
public Date getDate(){
if(this.stringDate==null){
return null;
}else{
return getDateFromString(stringDate);
}
}
/**
* Setea el Date
* @param date
*/
public void setDate(Date date){
if(date==null){
this.stringDate=null;
}else{
this.stringDate=getDateString(date);
}
}
private static final String getDateString(Date date){
return DateTimeFormat.getFormat(SERIALIZED_FORMAT).format(date);
}
private static final Date getDateFromString(String stringDate){
return DateTimeFormat.getFormat(SERIALIZED_FORMAT).parse(stringDate);
}
public String getStringDate() {
return stringDate;
}
public void setStringDate(String stringDate) {
this.stringDate = stringDate;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((stringDate == null) ? 0 : stringDate.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof StringDate))
return false;
StringDate other = (StringDate) obj;
if (stringDate == null) {
if (other.stringDate != null)
return false;
} else if (!stringDate.equals(other.stringDate))
return false;
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment