1

I am converting UTC to CET with this groovy / java coding:

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import org.joda.time.*;
import org.joda.time.format.*;

def Message processData(Message message) {

       def messageLog = messageLogFactory.getMessageLog(message);
       def map = message.getHeaders();
       def value = map.get("dateOfBirth");
       if (value != null) {
           String pattern = "yyyy-MM-dd'T'HH:mm:ssZ";
           DateTimeFormatter dtf = DateTimeFormat.forPattern(pattern);
           DateTime dateTime = dtf.parseDateTime(value.toString());
           TimeZone tz = TimeZone.getTimeZone("CET");
           def result = dateTime.withZone( DateTimeZone.forTimeZone(tz) ).toString();
           message.setHeader("dateOfBirth", result);
           return message;
       }
}

this code works, UTC is getting converted to CET.

But I am receiving an error message anyway and I don't know why. Could anybody help me to get rid of this error?

Error:

javax.script.ScriptException: java.lang.Exception: 
java.lang.IllegalArgumentException: Invalid format: ""@ line 20 in script8.groovy, 
cause: java.lang.IllegalArgumentException: Invalid format: ""

Thanks in advance

Solution:

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import org.joda.time.*;
import org.joda.time.format.*;

def Message processData(Message message) {
      def messageLog = messageLogFactory.getMessageLog(message);
      def map = message.getHeaders();
      def value = map.get("dateOfBirth");
      if (value?.trim() != "") {
        String pattern = "yyyy-MM-dd'T'HH:mm:ssZ";
        DateTimeFormatter dtf = DateTimeFormat.forPattern(pattern);
        DateTime dateTime = dtf.parseDateTime(value.toString());
        TimeZone tz = TimeZone.getTimeZone("CET");
        def result = dateTime.withZone( DateTimeZone.forTimeZone(tz) ).toString();
        message.setHeader("dateOfBirth", result);
     }
     return message;
}
dotchuZ
  • 2,621
  • 11
  • 39
  • 65

3 Answers3

4

Here you can go:

//Change the date and date formate as needed
def inputDateString = "Wed Aug 23 00:00:00 UTC 2017"
def inputDateFormat = "E MMM dd HH:mm:ss Z yyyy"

def outputDateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
def outputTZ = TimeZone.getTimeZone('CET')

def date = Date.parse(inputDateFormat, inputDateString)
def convertedDate = date.format(outputDateFormat, outputTZ)

println convertedDate

You can quickly try it online demo

Rao
  • 20,781
  • 11
  • 57
  • 77
1

your code works fine when the value variable contains correct datetime value.

if I change value = "" (empty string) then I have the same exception:

java.lang.IllegalArgumentException: Invalid format: ""

@Grab(group='joda-time', module='joda-time', version='2.0')

import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import java.util.TimeZone;


def value = '2016-12-31T13:14:15+02';

String pattern = "yyyy-MM-dd'T'HH:mm:ssZ";
DateTimeFormatter dtf = DateTimeFormat.forPattern(pattern);
DateTime dateTime = dtf.parseDateTime(value);

TimeZone tz = TimeZone.getTimeZone("CET");
def result = dateTime.withZone( DateTimeZone.forTimeZone(tz) ).toString();
daggett
  • 26,404
  • 3
  • 40
  • 56
  • thanks, that was also @SzymonStepniak 's idea. this does not include a solution but lead me to it. thanks. – dotchuZ Aug 23 '17 at 09:26
1

In Groovy you may find adding a dateUtil.nullSafeFormat might work just fine.

But just FYI, here is another way to use maybeNull?.with { it.whatever() } ?: "" that works well for avoiding null passed to DateFormat:

Map<String,String> getStuff(def json) {
  DateTimeFormatter df = DateTimeFormatter.ISO_LOCAL_DATE
  [oneThing:  json?.some?.path,
   someLocalDate: json?.some?.big?.path?.myDate?.with { df.format(it) } ?: "",
   keepGoing: json?.more?.stuff?.with { [lastName, firstName]?.join(' ') },
  ...]

You can see I'm going rather overboard with the null checks, but when it's so easy why not!

Jim P
  • 569
  • 3
  • 15