Date Formatting in Groovy Script

October 13, 2022

We can format the date in groovy without importing the java simple date formats. Simply use like this :

String oldDate = '01/10/2022 13:48'
Date date = Date.parse( 'MM/dd/yyyy HH:mm', oldDate )
String newDate = date.format( 'yyyy-MM-dd' )

println newDate

XSLT : Converting decimal hours to hours minutes and seconds

February 16, 2022

If you want to convert decimal values into hours and minutes, and you are stuck with XSLT, use this.

<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>

<xsl:variable name="decimal_hours" select="3.14"/>

<xsl:template match="/">
<xsl:value-of select="concat(
  format-number(floor($decimal_hours              ), '00:'),
  format-number(floor($decimal_hours *  60 mod  60), '00:'),
  format-number(floor($decimal_hours * 360 mod 360), '00'))"/>
</xsl:template>

</xsl:stylesheet>

Successfactors : Adding attachment to MDF Objects via OData

December 3, 2021

In order to upsert into an MDF Object which have attachment, first you have to upsert into the Attachment entity with the base64 encoded File String. This upsert will return the attachment ID. Then retrieve the attachment ID from the response and upsert into the MDF object. The attachment id can be retrieved with XPatch expression like this.

<uri>Attachment(attachmentId=<xsl:copy-of select="substring-after(//atom:entry/atom:content/m:properties/d:key,'Attachment/attachmentId=')"/>)</uri>

After this, Upsert the attachment using this attachment ID wtih payload looking like this:

{
        "cust_ParPaySlip_externalCode": "bbbb",
        "cust_PayrollPeriod": "/Date(1625316265000)/",
        "cust_AttachmentNav": {
            "__metadata": {
                "uri": "Attachment(attachmentId=16840)"
            }
        },
        "__metadata": {
          "uri": "cust_ListPaySlip"
        }
      }

Format Date Time with XSLT

November 17, 2021

To format the datetime in XSLT, first convert it to date time, and use the XSLT function format-dateTime. For example use this to format to the pattern yyyy-MM-dd. Have fun 🙂

<HireDate>
     <xsl:value-of select="format-dateTime(xs:dateTime(startDate),'[Y0001]-[M01]-[D01]')"/>
</HireDate>

SCPI – Covert DateTime to Epoch (Unix time)

June 15, 2021

Upserting to OData entities in Successfactors need the date to be in the Unix (Epoch) time format. The normal datetime value can be changed to Unix time either by groovy script, or by XSLT. For eg, think the input XML like this :

<Input>
   <UnixTime>1444150760</UnixTime>
   <ISODateTime>2015-10-06T16:59:20.555</ISODateTime>
</Input> 

The conversion can be done with XSLT like this :

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/Input">
    <output>
		<ISODateTime>
			<xsl:value-of select="xs:dateTime('1970-01-01T00:00:00') + xs:dayTimeDuration(concat('PT', UnixTime, 'S'))"/>
		</ISODateTime>
		<UnixTimestamp>
			<xsl:value-of select="floor((xs:dateTime(ISODateTime) - xs:dateTime('1970-01-01T00:00:00')) div xs:dayTimeDuration('PT1S')) "/>
		</UnixTimestamp>
	</output>
</xsl:template> 

</xsl:stylesheet>

The epoch basically, is the seconds after the date 01.01.1970. So apparently, the calculation includes taking the difference.

Thats it, happy coding!

SCPI – Add days with TimeZone

June 11, 2021

We may need to create a date value in header/property, which could we use in the filter at a later point.
Can be easily achieved using groovy script. Usually its very easily with normal java util class. But problems come when we have to add this based on a particular timezone. Code below :

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import java.time.ZonedDateTime
import java.time.ZoneId
import java.time.format.DateTimeFormatter;

def Message processData(Message message) {    
    
    ZonedDateTime now = ZonedDateTime.now(ZoneId.of("Europe/Berlin"));	
    futureLimitDate = now.plusDays(30).format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'"));

    println futureLimitDate

    message.setProperty("futureLimitDate", futureLimitDate);
    return message;
}

That’s it. Happy Coding. :B

SCPI – Get current time in different Timezone

June 11, 2021

The current datetime can be easily retrieved and assigned to a property or header with the Content Modifier Step like this. Use a Camel Expression like these :

${date:now:yyyy-MM-dd’T’00:00:00’Z’}

or

${date:now:yyyy-MM-dd}

based on the date format you need.

But the problem comes when you need the time in different timezone. For eg, I wanted time in CET, but the camel expression always gives the time in UTC zone. This could be tackled with a groovy script to convert the time into any desired timezone.

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import java.time.ZonedDateTime
import java.time.ZoneId
import java.time.format.DateTimeFormatter;

def Message processData(Message message) {    
    
    ZonedDateTime now = ZonedDateTime.now(ZoneId.of("Europe/Berlin"));	
    todayTZ = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'00:00:00'Z'"));
    today = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));

    println today
    println todayTZ
    println futureLimitDate

    message.setProperty("todayTZ", todayTZ);
    message.setProperty("today", today);
    return message;
}

That’s it. Println is just for debugging in the IDE console, you can remove it.

Happy Coding. 😉

Change Column Separator on CSV in OpenOffice Calc

August 3, 2017

If you want to change the existing column separator or the “delimiter” of a CSV in open office, follow the easy steps :

  • open the csv in openoffice calc
  • go to file-> save as
  • opens the typical save dialog boxUntitled
  • Select the Edit Filter Settings option and click save
  • Opens up a dialog box to change all the default settings.Untitled
  • Change whatever you need and click Ok, and thats all.

Its so easy, but so much hidden with weird settings name. Hope it will be helpful for someone. Enjoy and happy coding!

 

Gradle not finding tools.jar

May 17, 2017

Was struggling with building my project using gradle for some time. Whenever i tried to build it, i get an error message “Could not find tools.jar. Please check that C:\Program Files\Java\jre1.8.0_131 contains a valid JDK installation.”. I made it double sure that my standard JVM is pointing to the JDK installation folder, not the JRE folder. Still getting the error, later I found out that gradle is taking the java.home for the build process. For that you can either start the eclipse through command line parameter for java.home.

Or the easy solution i found is, go to the gradle task you want to run, right click, go to gradle configuration, and in the Java Home tab, point to the JDK installation folder. Voila, it will build the project without crying.

Happy Coding guys!

Viewing complete strings while debugging in Eclipse

February 14, 2017

While debugging Java code, Strings in the views “Variables” and “Expressions” show up only till a certain length, after which Eclipse shows “…”. Even after working with eclipse for 10 years it was a new info for me. There is a way to show the complete string.

In the Variables view you can right click on Details pane (the section where the string content is displayed) and select “Max Length…” popup menu. The same length applies to expression inspector popup and few other places. In the max length give 0 and you will see the whole values in the value box.

untitled

Thats it guys, enjoy coding.