Created
December 27, 2012 05:56
-
-
Save mauhiz/4385797 to your computer and use it in GitHub Desktop.
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
import java.io.IOException; | |
import java.text.ParseException; | |
import java.text.SimpleDateFormat; | |
import java.util.Date; | |
import java.util.Locale; | |
import javax.servlet.jsp.JspException; | |
import javax.servlet.jsp.JspTagException; | |
import javax.servlet.jsp.tagext.BodyTagSupport; | |
import org.apache.commons.lang3.time.FastDateFormat; | |
public class JapaneseDateTag extends BodyTagSupport { | |
private static final Locale JAPANESE_IMPERIAL = new Locale("ja", "JP", "JP"); | |
private static final long serialVersionUID = 1L; | |
private String format; | |
private boolean imperial = true; | |
private String value; | |
@Override | |
public int doEndTag() throws JspException { | |
try { | |
pageContext.getOut().print(computeDisplay()); | |
} catch (IOException e) { | |
throw new JspTagException(e); | |
} | |
return super.doEndTag(); | |
} | |
public String getFormat() { | |
return format; | |
} | |
public String getValue() { | |
return value; | |
} | |
public boolean isImperial() { | |
return imperial; | |
} | |
public void setFormat(String format) { | |
this.format = format; | |
} | |
public void setImperial(boolean imperial) { | |
this.imperial = imperial; | |
} | |
public void setValue(String value) { | |
this.value = value; | |
} | |
protected Locale getLocale() { | |
return imperial ? JAPANESE_IMPERIAL : Locale.JAPANESE; | |
} | |
protected String getOutputFormat() { | |
return imperial ? "GGGGyyyy'年'MM'月'dd'日'" : "yyyy'年'MM'月'dd'日'"; | |
} | |
private String computeDisplay() throws JspTagException { | |
try { | |
Date date = new SimpleDateFormat(format, Locale.US).parse(value); | |
return FastDateFormat.getInstance(getOutputFormat(), getLocale()).format(date); | |
} catch (ParseException pe) { | |
throw new JspTagException(pe); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment