Formatting dates in Freemarker to say Today, Yesterday, etc.

Is there a way in freemarker to compare dates for checking if the date is today or yesterday ... or do I need to write Java code to run these tests?

I basically want to do this:

<#------------------------------------------------------------------------------
 formatDate
------------------------------------------------------------------------------->
<#macro formatDate date showTime=true>
    <#if date??>
        <span class="Date">
            <#if date?is_today>
                Today
            <#elseif date?is_yesterday>
                Yesterday
            <#else>
                ${date?date}
            </#if>
        </span>
        <#if showTime>
            <span class="Time">${date?time}</span>
        </#if>
    </#if>
</#macro>

EDIT: . The best guess for implementing this is to pass the today and yesterday to the model for pages using this function, and then compare the date values ​​with these two objects to the model. I have no options, but I would prefer not to do this for every page using this macro. Any other options that are better?

<#if date??>
    <span class="Date">
        <#if date?date?string.short == today?date?string.short>
            Today
        <#elseif date?date?string.short == yesterday?date?string.short>
            Yesterday
        <#else>
            ${date?date}
        </#if>
    </span>
    <#if showTime>
        <span class="Time">${date?time}</span>
    </#if>
</#if>
+3
source share
4

, , , :

<#assign diff = (mydate?long / 86400000)?round - (.now?long / 86400000)?round />

.now - buildin, 86400 - , , , . 0, -1 ..

is_today, freemarker, TemplateDirectiveModel, :

http://freemarker.sourceforge.net/docs/pgui_datamodel_directive.html

, :... : -)

+9

1. :

import org.joda.time.format.*;
...
public class StringAndDateUtils {

    public static String yesterdayOrToday(String date) {
        DateTime dateTime = DateTimeFormat.forPattern("MM/dd/yyyy").parseDateTime(date);
        DateTime today = new DateTime();
        DateTime yesterday = today.minusDays(1);

        if (dateTime.toLocalDate().equals(today.toLocalDate())) {
            return "Today " ;
        } else if (dateTime.toLocalDate().equals(yesterday.toLocalDate())) {
            return "Yesterday " ;
        } else {
            return date;
        }
    }
}

2. :

modelMap.addAttribute("StringAndDateUtils", new StringAndDateUtils());

3. .FTL , :

<#assign date = realDateObject?string("MM/dd/yyyy")/>  
${StringAndDateUtils.yesterdayOrToday(date)}
+2

int ( !) http://sourceforge.net/projects/freemarker/forums/forum/2345/topic/3027925?message=6479650

, int, , .

( )

[#assign  Adate = myDateA?string("yyyyMMdd")?number?int ]
[#assign  Atime = myDateA?string("HHmmss")?number?int ]
[#assign  Bdate = myDateB?string("yyyyMMdd")?number?int ]
[#assign  Btime = myDateB?string("HHmmss")?number?int ]

, , TemplateMethodModel (: df - SimpleDateFormatter ):

public BooleanModel exec(List args) throws TemplateModelException {
    int argcnt = args.size();

    if (argcnt != 3) {
        throw new TemplateModelException("Wrong arguments.  Use \"exec(Date?string(\"yyyyMMddHHmmss\"), " +
                "CompareString, Date?string(\"yyyyMMddHHmmss\"))\"," +
                " where CompareString is < = > ");
    }

    String firstDate = (String) args.get(0);
    String compareString = (String) args.get(1);
    String secondDate = (String) args.get(2);

    if (null == firstDate || null == secondDate || null == compareString ||
             compareString.length() != 1) {
        throw new TemplateModelException("Wrong arguments.  Use \"exec(Date?string(\"yyyyMMddHHmmss\"), " +
                "CompareString, Date?string(\"yyyyMMddHHmmss\"))\"," +
                " where CompareString is < = > ");
    }

    Date first = null;
    Date second = null;
    try {
        first = df.parse(firstDate);
        second = df.parse(secondDate);
    } catch (ParseException e) {
        throw new TemplateModelException("Wrong arguments.  Use \"exec(Date?string(\"yyyyMMddHHmmss\"), " +
                "CompareString, Date?string(\"yyyyMMddHHmmss\"))\"," +
                " where CompareString is < = > ");
    }

    if ("<".equals(compareString)) {
        return new BooleanModel(first.before(second), BeansWrapper.getDefaultInstance());
    }
    else if ("=".equals(compareString)) {
        return new BooleanModel(first.equals(second), BeansWrapper.getDefaultInstance());           
    }
    else if (">".equals(compareString)) {
        return new BooleanModel(first.after(second), BeansWrapper.getDefaultInstance());            
    }

    return new BooleanModel(Boolean.FALSE, BeansWrapper.getDefaultInstance());

}

:

[#if compareDate(now?string("yyyyMMddHHmmss"),"<", program.resStartDateTime?string("yyyyMMddHHmmss"))]

FYI, "now" - DateModel, .

model.put("now", new DateModel(new Date(), BeansWrapper.getDefaultInstance()));

TemplateMethodModel , .

+1

${houradd (date, 24)? string ( "yyyy-MM-dd HH: mm: ss" )}

${houradd (date, -24)? string ( "yyyy-MM-dd HH: mm: ss" )}

${dayadd (date, 1)? string ( "yyyy-MM-dd HH: mm: ss" )}

0
source

Source: https://habr.com/ru/post/1779043/


All Articles