time processing for the use of Freemarker function functions
Java background features:
1. Format time format
public static String formatdate (date date) {
Formatter.settimezone (Timezone.getdefault ());
Formatter.applypattern ("Yyyy-mm-dd HH:mm:ss");
Formatter.setlenient (FALSE);
Return Adjust (Formatter.format (date));
}
public static string FormatDate (date date, String patter) {
Formatter.settimezone (Timezone.getdefault ());
Formatter.applypattern (patter);
Formatter.setlenient (FALSE);
Return Adjust (Formatter.format (date));
}
2. Get the date interval of two date period
/**
* Obtain date interval of two date periods
*
* @param d1 Date 1
* @param d2 date 2
* @return The number of days between T2 and T1
*/
private static int getbetweendays (date d1, date D2) {
if (D1 = NULL | | d2 = NULL) {
return-1;
}
int betweendays;
Calendar C1 = Calendar.getinstance ();
Calendar C2 = calendar.getinstance ();
C1.settime (D1);
C2.settime (D2);
Guarantee that the second time must be greater than the first time
if (C1.after (C2)) {
C2.settime (D1);
C1.settime (D2);
}
int betweenyears = C2.get (calendar.year)-C1.get (calendar.year);
Betweendays = C2.get (calendar.day_of_year)-C1.get (calendar.day_of_year);
for (int i = 0; i < Betweenyears
;
i++) {
C1.set (Calendar.year, (C1.get (calendar.year) + 1));
Betweendays + + c1.getmaximum (calendar.day_of_year);
}
return betweendays;
}
/**
* Obtain date interval of two date periods
*
* @param T1 Time 1
* @param T2 Time 2
* @return The number of days between T2 and T1
*/
public static int Getbetweendays (string t1, string t2) {
Date D1 = parsedate (t1);
Date d2 = parsedate (T2);
Return Getbetweendays (D1, D2);
}
freemarker foreground function definition:
1, get the current time, the format is YYYY-MM-DD
< #function formatdate date= "" pattern= "Yyyy-mm-dd" >
< #if date?is_date>
< #return statics["Com.dm.core.util.DateUtil"].formatdate (Date,pattern)/>
< #else >
< #return ""/>
</#if >
</#function >
2. Get the date interval of two date period
< #function getbetweendays date1= "" date2= ">
< #return statics["Com.dm.core.util.DateUtil"].getbetweendays (date1,date2)/>
</#function >
Freemarker foreground features are actually called:
<li style= "List-style-type:circle;color: #000000; margin-left:20px; " >
<span style= "Display:block;height:24px;float:right;color:blue;" >[< @dateMonthOut item.wa_postdate/>
]</span>
<span class= "News_title" >
<a href= "${path}/web/article_newsviewa.do?wa_id=${(item.wa_id)!}"
target= "_blank" >${ellipsis (item.wa_title,28)}</a>
< #if getbetweendays (FormatDate (item.wa_postdate), Getnow ("Yyyy-mm-dd")) <=3>
</#if >
</span>
</li>
As shown in the figure: