Skip to Content

In this blog, I am going to share how I came across time zone when I tried to use local time zone to be the target file name on Cloud Platform Integration (CPI).

Scenario

My requirement is that I want to create a file on receiver system and the name of this file is local time when file generated. Something like 20180228T2156.txt. So, I started exploring how CPI can help me to achieve this.

First I tried with following Camel’s Simple expression in the File Name field inside SFTP adapter:

${date:now:ddMMyyyy'T'HHmm}

date:<command>:<pattern> for date formatting using the SimpleDateFormat patterns. Supported commands are: now for current timestamp. If you are not familiar with Camel’s Simple expression language, please read through [1] and [2] in the references.

I thought this is the one to solve my problem, but after ran my integration flow. The generated file name is not what I expect. It is in UTC time but not local time. So, any other options in this expression I can set to have result in local time zone? I tried many different patterns in SimpleDateFormat, but still can’t find a way to get what I want.

My next trying is to use groovy script to generate the local date time string for the file name. Below small code snippet helps me to generate local date and time and it’s correct when time zone is in day light saving. I save the formatted string into message header for later used in SFTP channel configuration.

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import static java.util.Calendar.*;
def Message processData(Message message) {    
    def localTimeZone = TimeZone.getTimeZone('Australia/Melbourne');
    def cal = Calendar.instance;
    def date = cal.time;
    def dateFormat = 'ddMMyyyy\'T\'HHmm';
    //Header
    message.setHeader("CamelFileName", date.format(dateFormat, localTimeZone) + '.csv');     
    return message;
}

 

In SFTP adapter Target tab, I enter ${header.CamelFileName} in File Name field to retrieve the value I save in the script. All in the end I get my desired result. Now, it’s much better than using UTC time.

References

[1] Get to know Camel’s Simple expression language in HCI by Morten Wittrock

[2] Apache Camel SimpleLanguage JavaAPI

 

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply