eclipse Java - XMLGregorianCalendar 异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11413670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 18:31:05  来源:igfitidea点击:

Java - XMLGregorianCalendar Exception

javaxmleclipsegregorian-calendar

提问by ns1

I am working on a web service program that makes calls back and forth in the form of XML. My issue is not with that however. When I run this locally (in Eclipse) on my machine, it seems to work. However, when my coworker runs it/if I attempt to run it on the server, he gets this exception before the web service is even called:

我正在开发一个以 XML 形式来回调用的 Web 服务程序。然而,我的问题与此无关。当我在我的机器上本地(在 Eclipse 中)运行它时,它似乎工作。但是,当我的同事运行它/如果我尝试在服务器上运行它时,他甚至在调用 Web 服务之前就会收到此异常:

Exception #6 in ClientMain.java java.lang.IllegalArgumentException: 2012-07-09T08:19:44-0400

ClientMain.java 中的异常 #6 java.lang.IllegalArgumentException: 2012-07-09T08:19:44-0400

Does anyone have any idea why it would take the argument on one machine but not the other? The build should not be the issue as I copied my entire eclipse workspace on a thumbdrive (so all associated jars and the build path should have carried over). As far as the server run, I exported the entire program as a runnable jar (running Eclipse Indigo) and get a similar issue.

有谁知道为什么它会在一台机器上而不是另一台机器上进行论证?构建应该不是问题,因为我将整个 eclipse 工作区复制到拇指驱动器上(因此所有相关的 jar 和构建路径都应该保留)。就服务器运行而言,我将整个程序导出为一个可运行的 jar(运行 Eclipse Indigo)并遇到类似的问题。

I'd appreciate any insight!

我很感激任何见解!

Stacktrace/Output:

堆栈跟踪/输出:

java.lang.IllegalArgumentException: 2012-07-09T09:19:42-0400
      at com.sun.org.apache.xerces.internal.jaxp.datatype.XMLGregorianCalendarImpl$Parser.skip(Unknown Source)
      at com.sun.org.apache.xerces.internal.jaxp.datatype.XMLGregorianCalendarImpl$Parser.parse(Unknown Source)
      at com.sun.org.apache.xerces.internal.jaxp.datatype.XMLGregorianCalendarImpl.<init>(Unknown Source)
      at com.sun.org.apache.xerces.internal.jaxp.datatype.DatatypeFactoryImpl.newXMLGregorianCalendar(Unknown Source)
      at com.travelport.worldspanimageserver.client.ClientMain.requestRun(ClientMain.java:231)******************************************************
Running by Yesterday's Date ...
2012-07-09T09:19:42-0400
Current date being used is 2012-07-09T09:19:42-0400
Exception #6 in ClientMain.java java.lang.IllegalArgumentException: 2012-07-09T09:19:42-0400

      at com.travelport.worldspanimageserver.client.ClientMain.runTypeCheck(ClientMain.java:161)
      at com.travelport.worldspanimageserver.client.ClientMain.main(ClientMain.java:81)
Exception #13 in ClientMain.java java.lang.NullPointerException

Method causing exception:

导致异常的方法:

    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.GregorianCalendar;
    import java.util.List;
    import java.util.Properties;
    import java.util.TimeZone;

    import javax.mail.Message;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeMessage;
    import javax.swing.text.Document;
    import javax.xml.datatype.DatatypeConfigurationException;
    import javax.xml.datatype.DatatypeFactory;
    import javax.xml.datatype.XMLGregorianCalendar;

    import org.joda.time.format.DateTimeFormatter;
    import org.joda.time.format.ISODateTimeFormat;

        public static void requestRun(String strRunType) throws DatatypeConfigurationException{
                //-TODO: determine how date is passed in/retrieved from database        
                 GregorianCalendar c = new GregorianCalendar();

                 XMLGregorianCalendar dateTime = null;

                 if (strRunType == "fullLoad") {dateTime = null;}
                 if (strRunType.substring(0,2).equals("OD")) 
                 { 
                     System.out.println("Current date being used is " + DatatypeFactory.newInstance().newXMLGregorianCalendar(strRunType.substring(2,strRunType.length())));
                     dateTime = DatatypeFactory.newInstance().newXMLGregorianCalendar(strRunType.substring(2,strRunType.length()));
                 }
                 if (strRunType.substring(0,2).equals("DT")) 
                 { 
                     System.out.println("Current date being used is " + strRunType.substring(2,strRunType.length()));           
                     dateTime = DatatypeFactory.newInstance().newXMLGregorianCalendar(strRunType.substring(2,strRunType.length()));
                 }

 String address = "http://vhligssas001:31860/services/ImageViewerService"; /**getServiceURL(sServer);**/

           try
           {
              ArrayList<PropertyType> propertyIdList = null;                                         
              ArrayList tempList = null;

               if (strRunType.equals("properties"))
               {
                  propertyIdList = getPropertiesFromFile(); //to fix later               
               }
               else
               {
                  propertyIdList = initialCall(dateTime, propertyIdList, address);
               }

               secondCall(propertyIdList, address);

           }  
           catch(Exception e)
           {
              System.out.println("Exception #5 in ClientMain.java " + e);
              try
              {
                 postMail("Exception #5 in ClientMain.java " + e);
              }
              catch (Exception e1)
              {
                  e1.printStackTrace();
              }
              System.exit(16);
           }  
       }  
       catch(Exception e)
       {
          System.out.println("Exception #6 in ClientMain.java " + e);
          try
          {
             postMail("Exception #6 in ClientMain.java " + e);
                 e.printStackTrace(); //added

          }
          catch (Exception e1)
          {
             e1.printStackTrace();
          }
          System.exit(16);
       }        
    }

Main Class:

主要类:

    public static void main(String args[]) throws Exception 
        {
            String sRunType = new String("");
         try
            {    
              File fPropertiesToLoad = new File("runType.txt");
              BufferedReader in = null;
              String strRunType = new String("");

               if (!fPropertiesToLoad.exists())
               {         
                  System.out.println("******************************************************");
                  System.out.println("Exception #1 - Run Type file does not exist");
                  System.out.println("******************************************************");
                  postMail("Exception #1 - Run Type file does not exist");
                  System.exit(16);
               }           
               else
               {
                   in = new BufferedReader(new FileReader("runType.txt"));   
                   if ((strRunType = in.readLine()) != null)
                   {
                       sRunType = strRunType.trim();
                   }
                   in.close();     
                   runTypeCheck(sRunType);
               }
            }        
            catch (Exception e)
            {
               System.out.println("Exception #2 in ClientMain.java " + e);
               postMail("Exception #2 in ClientMain.java " + e);
               System.exit(16);
            }
        }      

runTypeCheck:

运行类型检查:

    public static void runTypeCheck(String sRunType)
        {
            try
            {
             //Hard-coded in "yesterday" as the date
             sRunType = "DT";

             System.out.println("******************************************************");
             DateTimeFormatter parser2 = ISODateTimeFormat.dateTimeNoMillis();

             if (sRunType.equals("DT")) 
             {
                 System.out.println("Running by Yesterday's Date ...");
                 Calendar cal = Calendar.getInstance();
                 cal.add(Calendar.DATE, -1);
                 DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
                 sRunType =  dateFormat.format(cal.getTime());
                 System.out.println(sRunType);
                 sRunType = sRunType.replaceAll("-0500", "-05:00");
                 sRunType = "DT" + sRunType;
                 requestRun(sRunType);
             }
System.out.println("******************************************************");

       }
       catch(Exception e)
       {
           System.out.println("Exception #4 in ClientMain.java " + e);
           try
           {
              postMail("Exception #4 in ClientMain.java " + e);
           }
           catch (Exception e1)
           {
              e1.printStackTrace();
           }
           System.exit(16);
        }  
    }

only output:

仅输出:

******************************************************

Running by Yesterday's Date ...

2012-07-09T08:19:44-0400

Current date being used is 2012-07-09T08:19:44-0400

Exception #6 in ClientMain.java java.lang.IllegalArgumentException: 2012-07-09T08:19:44-0400

采纳答案by radimpe

The problem is that your 'date' that you get from strRunType.substring(2,strRunType.length())isn't a valid lexicalRepresentationof the date.

问题是您从中获得的“日期”strRunType.substring(2,strRunType.length())不是有效lexicalRepresentation的日期。

You'll need to get your input value correctly formatted in order to parse as an XMLGregorianCalendarobject.

您需要正确格式化输入值才能将其解析为XMLGregorianCalendar对象。

The error being that you are missing a :in the -04:00in your date.

错误之处在于你缺少一个:-04:00您的日期。

Which you must have been aware of because of sRunType = sRunType.replaceAll("-0500", "-05:00");code... except you were using a 5 instead of a 4?

由于sRunType = sRunType.replaceAll("-0500", "-05:00");代码,您一定已经意识到了这一点……除了您使用的是 5 而不是 4?