工作的Java代码

public static DateTime convertToUTC(String date) throws ParseException {

    DateTimeParser[] parsers = {
        DateTimeFormat.forPattern( "yyyy-MM-dd" ).getParser(),
        DateTimeFormat.forPattern( "yyyy-MM-dd'T'HH:mm:ss" ).getParser(),
        DateTimeFormat.forPattern( "yyyy-MM-dd'T'HH:mm:ssZZ" ).getParser(),
        DateTimeFormat.forPattern( "yyyy-MM-dd'T'HH:mm:ss.SSSZZ" ).getParser()};

    DateTimeFormatter formatter = new DateTimeFormatterBuilder().append( null, parsers ).toFormatter();


    DateTime dtime = new DateTime(formatter.parseDateTime(date),DateTimeZone.UTC);
    DateTimeFormatter formatter = DateTimeFormat.forPattern(format);
    return formatter.print(dtime );
}


我正在尝试的Groovy代码,但是却得到异常,这是底部给出的异常。

import org.joda.time.format.*
import org.joda.time.DateTimeZone
import org.joda.time.DateTime

def input = message.getInvocationProperty('after').toString()
DateTimeParser[] parsers = [[DateTimeFormat.forPattern( "yyyy-MM-dd" ).getParser()],[DateTimeFormat.forPattern( "yyyy-MM-dd'T'HH:mm:ss" ).getParser()],[DateTimeFormat.forPattern( "yyyy-MM-dd'T'HH:mm:ssZZ" ).getParser(),DateTimeFormat.forPattern( "yyyy-MM-dd'T'HH:mm:ss.SSSZZ" ).getParser()]]
def formatter = new DateTimeFormatterBuilder().append( null, parsers ).toFormatter()
return formatter.print(new DateTime(formatter.parseDateTime(input),DateTimeZone.UTC))



  根异常堆栈跟踪:
    org.codehaus.groovy.runtime.typehandling.GroovyCastException:无法将类“ java.util.ArrayList”的对象“ [org.joda.time.format.DateTimeFormatterBuilder$Composite@755ed6df]”转换为类“ org.joda.time”。 format.DateTimeParser',原因是:groovy.lang.GroovyRuntimeException:找不到与以下对象匹配的构造函数:org.joda.time.format.DateTimeParser(org.joda.time.format.DateTimeFormatterBuilder $ Composite)

最佳答案

DateTimeParser[] parsers = ...


上面的代码行实际上创建了一个ArrayList而不是Array。尝试

def formatter =
    new DateTimeFormatterBuilder()
    .append( null, parsers.toArray() ).toFormatter()

07-28 01:56
查看更多