Skip to content

Instantly share code, notes, and snippets.

@icfantv
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save icfantv/119b56bd9cc90b9c4cfb to your computer and use it in GitHub Desktop.
Save icfantv/119b56bd9cc90b9c4cfb to your computer and use it in GitHub Desktop.
Date/Time Format Provider for GWT/GXT
package com.foo.bar.shared;
public interface DateFormatProvider
{
public Date format(String pattern, String dateString);
}
package com.foo.bar.client;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.foo.bar.shared.DateFormatProvider;
import java.util.Date;
public class DateTimeFormatProvider implements DateFormatProvider, Serializable
{
@Override
public Date format(String pattern, String dateString)
{
Date result = null;
if (StringUtils.isNotBlank(dateString))
{
try
{
DateTimeFormat dtf = DateTimeFormat.getFormat(pattern);
result = dtf.parse(dateString);
}
catch (IllegalArgumentException ex)
{
// handle error
}
}
return result;
}
}
package com.foo.bar.server;
import com..foo.bar.shared.DateFormatProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class SimpleDateFormatProvider implements DateFormatProvider
{
@Override
public Date format(String pattern, String dateString)
{
Date result = null;
if (StringUtils.isNotBlank(dateString))
{
try
{
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
result = sdf.parse(dateString);
}
catch (ParseException ex)
{
// handle error
LOGGER.warn("illegal date string specified (from database?): {}", dateString, ex);
}
}
return result;
}
private static final Logger LOGGER = LoggerFactory.getLogger(SimpleDateFormatProvider.class);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment