[英]Groovy Date & TimeZone formatting
I am converting UTC to CET with this groovy / java coding:我正在使用此 groovy / java 编码将 UTC 转换为 CET:
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.此代码有效,UTC 正在转换为 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;
}
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
your code works fine when the value
variable contains correct datetime value. 当value
变量包含正确的datetime值时,您的代码可以正常工作。
if I change value = ""
(empty string) then I have the same exception: 如果我将value = ""
更改value = ""
(空字符串),则我有相同的异常:
java.lang.IllegalArgumentException: Invalid format: "" java.lang.IllegalArgumentException:无效格式:“”
@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();
In Groovy you may find adding a dateUtil.nullSafeFormat might work just fine.在 Groovy 中,您可能会发现添加 dateUtil.nullSafeFormat 可能效果很好。
But just FYI, here is another way to use maybeNull?.with { it.whatever() }?: ""
that works well for avoiding null passed to DateFormat:但仅供参考,这是另一种使用maybeNull?.with { it.whatever() }?: ""
的方法,可以很好地避免将 null 传递给 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!你可以看到我对 null 支票有点过分了,但既然如此简单,为什么不呢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.