Clarity

  • 1.  How to convert to Date format in Gel script

    Posted Jan 07, 2016 12:57 AM

    Hello Everyone, I'm reading a file which has dates is dd/mm/yyyy Format now I'm trying it to convert them to  yyyy-mm-dd Format
    I'm new to Gel and couldn't track my code where it is going wrong .

    I'm able to read from file and Write to File , but unable to convert to yyyy-mm-dd Format
    Could you please help me out

     

    Here is my Code

    <gel:script

    xmlns:core="jelly:core"

    xmlns:gel="jelly:com.niku.union.gel.GELTagLibrary"

    xmlns:file="jelly:com.niku.union.gel.FileTagLibrary"

    >

     

    <core:new className="java.util.Date" var="date"/>

     

    <!-- Reading File from Provided Path -->

    <gel:parameter default="D:\Gel Script Docs\JS Custom Scripts\Dates Task\dates.txt" var="infile"/>

    <file:readFile fileName="${infile}" var="infileParsed" embedded="false"/>           

     

     

    <!-- Writing to File in yyyy-mm--dd Format -->

            <file:writeFile

            delimiter=","

            embedded="false"

            fileName="D:\Gel Script Docs\JS Custom Scripts\ReadWriteResult.txt">

     

              <core:forEach items="${infileParsed.rows}" var="row" indexVar="i" begin="0" step="1">

              

                <file:line>

                <gel:formatDate format="yyyy-MM-dd" stringVar="mydate" dateVar="row[0]"/>

                                    <file:column value="${mydate[0]}" />

                                </file:line>

              

              </core:forEach>

            </file:writeFile>

    <!-- Writing to File is closed here -->

     

    <!-- FORMAT DATE -->

    <gel:formatDate format="yyyy-MM-dd" stringVar="today" dateVar="date"/>

    <gel:out> <gel:formatDate format="yyyy-MM-dd"  dateVar="date"/></gel:out>

    <gel:out>

    today Date== ${today}

    </gel:out>

     

     

    </gel:script>

     

    Error in Command Prompt is : Non-Date variable used as Date.



  • 2.  Re: How to convert to Date format in Gel script
    Best Answer

    Posted Jan 07, 2016 02:29 AM

    <gel:formatDate> accepts java.util.Date and returns a String in specified format.

    <gel:parseDate> accepts String of specified format and returns java.util.Date.

     

    "dateVar" attribute of <gel:formatDate> is of type java.util.Date whereas you are trying to assign "String". Hence you get the error. If I understand your requirement correctly, I think you can use <gel:parseDate> first to convert it into Date Object and then use <gel:formatDate> to convert it into required format.



  • 3.  Re: How to convert to Date format in Gel script

    Posted Jan 07, 2016 02:39 AM

    Hello , I have Found the Solution for it , As you said
    1st) we need to convert it using parseDate
    2nd) we need to change the result to our custom format using `formatDate`

     

    I have Framed my code like this and got the expected output

     

     

                   <gel:parseDate dateVar="date1" format="dd/MM/yyyy">${row[0]}</gel:parseDate>

               

                <gel:out><gel:formatDate format="yyyy-MM-dd" dateVar="date1" stringVar="mydate"/></gel:out>

                <gel:out>${mydate}</gel:out>

                  <file:line>

            <file:column value="${mydate}" />

                </file:line>

     

    Thanks Sridhar,