Last active
August 29, 2015 14:05
-
-
Save icfantv/119b56bd9cc90b9c4cfb to your computer and use it in GitHub Desktop.
Date/Time Format Provider for GWT/GXT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.foo.bar.shared; | |
public interface DateFormatProvider | |
{ | |
public Date format(String pattern, String dateString); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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