PrettyTime is an OpenSource time formatting library. Completely customizable, PrettyTime creates human readable, relative timestamps like those seen on Digg, Twitter, and Facebook. It’s simple, get started “right now!”
To use PrettyTime in Java code, it's petty simple:
public String getPretty(Date date) {
PrettyTime p = new PrettyTime();
String prettier = p.format(date);
return prettier;
}
It is generally a good practice to render date prettier in display layer, that is, JSP file. Therefore, we will create a simple JSP Tag to make our date or time display in a fashion way. And the tag will implement in JSP Tag Files, which was introduced in JSP 2.0.
Preparation
Add PrettyTime in your project's building path, I use Maven:com.ocpsoft ocpsoft-pretty-time 1.0.5
prettyTime.tag
This JSP Tag File will be placed at:webapp/WEB-INF/tags/prettyTime.tagThis tag will require one Date attribute, the date you want to format. The prettyTime.tag as following:
<%@ tag import="com.ocpsoft.pretty.time.PrettyTime" import="java.util.Date"%>
<%@ attribute name="date" required="true" type="java.util.Date" %>
<%
PrettyTime p = new PrettyTime();
String prettier = p.format(date);
out.println(prettier);
%>
Usage in JSP file
It is very easy to use prettyTime tag in your JSP file. You need to declare our tag at first.<%@ taglib prefix="kc" tagdir="/WEB-INF/tags"%>Ordered:
3 comments:
This post is useless without the source for the tag class. Sorry.
Hi dude,
I'm using tag file instead of tag class.....
So no class required.
Yeah, the code hasn't displayed properly, you have to view the page source to see all of it.
Post a Comment