SimpleDateFormat和Date:new如何才能简单地报告将来的日期? XSLT,XML1.0,Java [英] SimpleDateFormat and Date:new how can I simply report a date in the future? XSLT, XML1.0, Java

查看:79
本文介绍了SimpleDateFormat和Date:new如何才能简单地报告将来的日期? XSLT,XML1.0,Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在此代码后半部分填充的当前日期前添加90天。我在下面附加了生成的XML(使用xml1.0和xalan 2.7.1),它看起来很棒,我只需要Licensing_End_Window即可读取今天的90天后的日期,并且无法解决我的一生。请注意,这只是XSLT及其结果XML的片段。

I need to add 90 days to the current date populated in the 2nd half of this code. I've attached the resulting XML below (using xml1.0 and xalan 2.7.1), it looks great i just need Licensing_End_Window to read 90days post todays date and can't figure it out for the life of me. Note this is just a snippet of the XSLT and resulting XML.

                    <xsl:element name="App_Data">
                    <xsl:attribute name="App">MOD</xsl:attribute> 
                    <xsl:attribute name="Name">Licensing_Window_Start</xsl:attribute> 
                    <xsl:attribute name="Value">
                        <xsl:variable name="s" select="SimpleDateFormat:new('MM-dd-yyyy')"/>
                        <xsl:variable name="Date" select="Date:new()"/>
                        <xsl:choose>
                            <xsl:when test="add:FlightStart"><xsl:value-of select="substring(add:FlightStart,1,10)"/></xsl:when>
                            <xsl:otherwise>
                                <xsl:value-of select="SimpleDateFormat:format($s,$Date)" />
                            </xsl:otherwise>
                        </xsl:choose>
                    </xsl:attribute>
                </xsl:element>
                <xsl:element name="App_Data">
                    <xsl:attribute name="App">MOD</xsl:attribute> 
                    <xsl:attribute name="Name">Licensing_Window_End</xsl:attribute> 
                    <xsl:attribute name="Value">
                        <xsl:variable name="s" select="SimpleDateFormat:new('MM-dd-yyyy')"/>
                        <xsl:variable name="Date" select="Date:new()"/>
                        <xsl:choose>
                            <xsl:when test="add:FlightEnd"><xsl:value-of select="substring(add:FlightEnd,1,10)"/></xsl:when>
                            <xsl:otherwise>
                                <xsl:value-of select="SimpleDateFormat:format($s,$Date)"/>
                            </xsl:otherwise>
                        </xsl:choose>
                    </xsl:attribute>
                </xsl:element>

生成的XML

 <Name="Licensing_Window_Start" Value="12-10-2014"/><App_Data App="MOD" Name="Licensing_Window_End" Value="12-10-2014"/><App_Data App="MOD"


推荐答案

我看到你在尝试使用Java扩展来做到这一点。我不知道它是否可以工作以及如何工作,但是我可以建议一个(几乎)XSLT 1.0解决方案。以下样式表:

I see you are trying to do this with Java extensions. I don't know if and how this can work, but I can suggest a purely (almost) XSLT 1.0 solution. The following stylesheet:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:date="http://exslt.org/dates-and-times"
extension-element-prefixes="date">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:variable name="today" select="date:date()" />

<xsl:template match="/">
    <output>
        <start>
            <xsl:value-of select="concat(substring($today, 6, 2), '-', substring($today, 9, 2), '-', substring($today, 1, 4))" />
        </start>
        <end>
            <xsl:call-template name="add-days-to-today">
                <xsl:with-param name="days-to-add" select="90" />
            </xsl:call-template>
        </end>
    </output>
</xsl:template> 

<xsl:template name="add-days-to-today">
    <xsl:param name="days-to-add"/>
    <xsl:param name="year" select="substring($today, 1, 4)"/>
    <xsl:param name="month" select="substring($today, 6, 2)"/>
    <xsl:param name="day" select="substring($today, 9, 2)"/>
    <xsl:param name="a" select="floor((14 - $month) div 12)"/>
    <xsl:param name="y" select="$year + 4800 - $a"/>
    <xsl:param name="m" select="$month + 12*$a - 3"/>
    <xsl:param name="JDN" select="$day + floor((153*$m + 2) div 5) + 365*$y + floor($y div 4) - floor($y div 100) + floor($y div 400) - 32045 + $days-to-add" />
    <xsl:param name="f" select="$JDN + 1401 + floor((floor((4 * $JDN + 274277) div 146097) * 3) div 4) - 38"/>
    <xsl:param name="e" select="4*$f + 3"/>
    <xsl:param name="g" select="floor(($e mod 1461) div 4)"/>
    <xsl:param name="h" select="5*$g + 2"/>
    <xsl:param name="D" select="floor(($h mod 153) div 5 ) + 1"/>
    <xsl:param name="M" select="(floor($h div 153) + 2) mod 12 + 1"/>
    <xsl:param name="Y" select="floor($e div 1461) - 4716 + floor((14 - $M) div 12)"/>
    <xsl:param name="MM" select="format-number($M, '00')"/>
    <xsl:param name="DD" select="format-number($D, '00')"/>
    <xsl:value-of select="concat($MM, '-', $DD, '-', $Y)" />
</xsl:template>     

</xsl:stylesheet>

当应用于任何XML输入时(2014年12月11日),返回:

when applied to any XML input (on Dec 11, 2014), returns:

<?xml version="1.0" encoding="UTF-8"?>
<output>
   <start>12-11-2014</start>
   <end>03-11-2015</end>
</output>

这篇关于SimpleDateFormat和Date:new如何才能简单地报告将来的日期? XSLT,XML1.0,Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆