users@jaxb.java.net

Re: Better xs:duration support?

From: Malachi de AElfweald <malachi_at_EOTI.ORG>
Date: Thu, 24 Jul 2003 11:34:15 -0600

This is not a cleaned up piece of code, just a little test that shows there are easy ways to parse the duration, for any who are interested...

 public xsdDurationTest(String duration)
 throws Exception
 {
  System.out.println("Trying to parse: " + duration);
  HashMap dateMap;
  HashMap timeMap;

  int tNdx = period.indexOf("T");
  if(tNdx == -1)
  {
   dateMap = createMap(duration);
   timeMap = new HashMap();
  }else{
   dateMap = createMap(duration.substring(0, tNdx));
   timeMap = createMap(duration.substring(tNdx+1));
  }

  System.out.println("Years: " + dateMap.get("Y"));
  System.out.println("Months: " + dateMap.get("M"));
  System.out.println("Days: " + dateMap.get("D"));
  System.out.println("Hours: " + timeMap.get("H"));
  System.out.println("Minutes: " + timeMap.get("M"));
  System.out.println("Seconds: " + timeMap.get("S"));
 }

 public HashMap createMap(String s)
 {
  HashMap map = new HashMap();
  int num = 0;
  char c;
  for(int pos=0; pos<s.length(); pos++)
  {
   c = s.charAt(pos);
   if(Character.isDigit(c))
   {
    num = (num * 10) + Integer.parseInt("" + c);
   }else{
    map.put(("" + c).toUpperCase(), new Integer(num));
    num = 0;
   }
  }
  return map;
 }