最近项目中有这样的需求,需要将普通的时间格式转行成时间间隔模式,如:将2009-07-01 10:16:14这种格式变成:昨天21:45:26 格式,我思前想后,为了显示的需要还是自定义个标签较合理。现将方法跟大家分享,算法不重要,重要的是自定义标签的流程。
我们分二步走
1.编写自定义标签类。
2. 创建taglib文件(.tld),编写标签声明。
第一步:
首先编写时间格式转换的方法:
- public static String getDisplayTime(Date date){
- Date nowDate=new Date();
- Calendar oldCalendar=Calendar.getInstance();
- Calendar nowCalendar=Calendar.getInstance();
- oldCalendar.setTime(date);
- nowCalendar.setTime(nowDate);
- int day=getDaysBetween(oldCalendar,nowCalendar);
- Long nowTime=nowDate.getTime();
- Long oldTime=date.getTime();
- Long time=nowTime-oldTime;
- if(time>60*60*1000){
- if(day==0){
- return new SimpleDateFormat("HH:mm:ss").format(date);
- }else if(day==1){
- return "昨天"+new SimpleDateFormat("HH:mm:ss").format(date);
- }else if(day==2){
- return "前天"+new SimpleDateFormat("HH:mm:ss").format(date);
- }else{
- return new SimpleDateFormat("yyyy-MM-dd").format(date);
- }
- }else if(time<60*1000){
- return "刚刚";
- }else{
- return (long)time/(60*1000)+"分钟前";
- }
- }
public static String getDisplayTime(Date date){
Date nowDate=new Date();
Calendar oldCalendar=Calendar.getInstance();
Calendar nowCalendar=Calendar.getInstance();
oldCalendar.setTime(date);
nowCalendar.setTime(nowDate);
int day=getDaysBetween(oldCalendar,nowCalendar);
Long nowTime=nowDate.getTime();
Long oldTime=date.getTime();
Long time=nowTime-oldTime;
if(time>60*60*1000){
if(day==0){
return new SimpleDateFormat("HH:mm:ss").format(date);
}else if(day==1){
return "昨天"+new SimpleDateFormat("HH:mm:ss").format(date);
}else if(day==2){
return "前天"+new SimpleDateFormat("HH:mm:ss").format(date);
}else{
return new SimpleDateFormat("yyyy-MM-dd").format(date);
}
}else if(time<60*1000){
return "刚刚";
}else{
return (long)time/(60*1000)+"分钟前";
}
}
这段代码很简单,相信大家一看便懂了。
下面是距离当前时间天数的方法:
- public class Times extends Component{
- private String name;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public Times(ValueStack arg0) {
- super(arg0);
- }
- @Override
- public boolean start(Writer writer) {
- boolean result = super.start(writer);
- try {
- java.util.Date date = (java.util.Date) findValue(name);
- String str=DataTime.getDisplayTime(date);
- writer.write(str);
- } catch (IOException ex) {
- Logger.getLogger(Pages.class.getName()).log(Level.SEVERE, null, ex);
- }
- return result;
- }
- }
public class Times extends Component{
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Times(ValueStack arg0) {
super(arg0);
}
@Override
public boolean start(Writer writer) {
boolean result = super.start(writer);
try {
java.util.Date date = (java.util.Date) findValue(name);
String str=DataTime.getDisplayTime(date);
writer.write(str);
} catch (IOException ex) {
Logger.getLogger(Pages.class.getName()).log(Level.SEVERE, null, ex);
}
return result;
}
}
这里是写一个方法调用这个算法返回一个值
- public class TimeTag extends ComponentTagSupport{
- private String name;
- public void setName(String name) {
- this.name = name;
- }
- @Override
- public Component getBean(ValueStack arg0, HttpServletRequest arg1, HttpServletResponse arg2) {
- return new Times(arg0);
- }
- //获得参数
- protected void populateParams() {
- super.populateParams();
- Times pages = (Times)component;
- pages.setName(name);
- }
- }
public class TimeTag extends ComponentTagSupport{
private String name;
public void setName(String name) {
this.name = name;
}
@Override
public Component getBean(ValueStack arg0, HttpServletRequest arg1, HttpServletResponse arg2) {
return new Times(arg0);
}
//获得参数
protected void populateParams() {
super.populateParams();
Times pages = (Times)component;
pages.setName(name);
}
}
这里继承ComponentTagSupport类是为了获得标签中的属性值,并包装成Component对象。继承Component类是为了从Struts2中的ValueStack中获得相对应的值。
第二步:建立smallfish.tld
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
- <taglib>
- <tlib-version>2.2.3</tlib-version>
- <jsp-version>1.2</jsp-version>
- <short-name>smallfish</short-name>
- <uri>/jzy</uri>
- <display-name>"smallfish Tags"</display-name>
- <tag>
- <name>times</name>
- <tag-class>com.common.tag.TimeTag</tag-class>
- <body-content>empty</body-content>
- <attribute>
- <name>name</name>
- <required>true</required>
- <rtexprvalue>true</rtexprvalue>
- </attribute>
- </tag>
- </taglib>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
<tlib-version>2.2.3</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>smallfish</short-name>
<uri>/jzy</uri>
<display-name>"smallfish Tags"</display-name>
<tag>
<name>times</name>
<tag-class>com.common.tag.TimeTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>name</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
经过这些操作后,一个自己的标签就弄好了,tld文件可以扩充,在用的时候就像Struts2自带的标签一样,导入,页面显示时就用<smallfish:times name="time" />这样就可以了。是不是很简单呢。
学java和asp.net的朋友-免费的浪曦java视频和asp.net视频下载.
是全套的完整的。http://www.itpob.cn/thread-196-1-1.html
有需要的可以去下。