users@jaxb.java.net

Re: Better xs:duration support?

From: Gary Gregory <ggregory_at_seagullsw.com>
Date: Wed, 16 Jul 2003 16:30:40 -0400

Thank you kindly for the offer Craig, but I cannot accept. Unless this code
is part of an open source project with an ASF-style license as I cannot have
our company deal with any legal issues.

FYI, I am not having much luck using the JRE's SimpleDateFormat to parse
xs:duration instances. SimpleDateFormat can create strings for dates, no
problem but not string to dates. Still playing around with:

/*
 * Copyright (C) 1993-2003 SEAGULL
 *
 * TestSimpleDateFormat.java
 * Created on Jul 16, 2003, 12:31:14 PM
 *
 */

package test;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import junit.framework.Assert;
import junit.framework.TestCase;

/**
 * Experiments.
 *
 * @author <a href="mailto:ggregory_at_seagullsw.com">Gary Gregory</a>
 * @version $Id: $
 */
public class TestSimpleDateFormat extends TestCase {

    public static final String ISO_EXTENDED_FORMAT_PATTERN_F =
"'P'yyyy'Y'M'M'd'DT'H'H'm'M's.S'S'";
    public static final String ISO_EXTENDED_FORMAT_PATTERN_P =
"'P'yyyy'Y'MM'M'dd'DT'HH'H'mm'M'ss.S'SS'";

    public void test() {
        DateFormat dfParser = new
SimpleDateFormat(ISO_EXTENDED_FORMAT_PATTERN_P);
        DateFormat dfFormatter = new
SimpleDateFormat(ISO_EXTENDED_FORMAT_PATTERN_F);

        Calendar cal = Calendar.getInstance();
        cal.set(1, 1, 3, 10, 30, 0);
        cal.set(Calendar.MILLISECOND, 0);
        Date date = cal.getTime();
        String text = dfFormatter.format(date);
        System.out.println(date + ": " + text);
        // really want:
        //Assert.assertEquals("P1Y2M3DT10H30M0.0S", text);
        // but we can only the following which is ISO8601 compliant.
        Assert.assertEquals("P0001Y2M3DT10H30M0.0S", text);
        //Assert.assertEquals(dfFormatter.format(date),
dfParser.format(date));
        try {
            // This should work
            //dfParser.parse("P1Y2M3DT10H30M0.0S");
            // this blows up
            dfParser.parse("P0001Y02M03DT10H30M00.00S");
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}

-----Original Message-----
From: Craig Raw [mailto:craig_at_quirk.co.za]
Sent: Wednesday, July 16, 2003 02:56
To: JAXB-INTEREST_at_JAVA.SUN.COM
Subject: Re: Better xs:duration support?

Hi,

I am using a custom Duration utility class to handle duration strings
once they have been unmarshalled. Basically, it contains a method

public static Duration parseDuration( String str ) throws ParseException

to parse a duration string, and performing conversion to year, month,
day etc. fields with an accessor for the long value as well. Mail me if
it would be of help.

Craig

craig_at_quirk.co.za


-----Original Message-----
From: Discussion list for the Java Architecture for XML Binding
[mailto:JAXB-INTEREST_at_JAVA.SUN.COM] On Behalf Of Kohsuke Kawaguchi
Sent: 16 July 2003 03:05 AM
To: JAXB-INTEREST_at_JAVA.SUN.COM
Subject: Re: Better xs:duration support?


Gary Gregory <ggregory_at_seagullsw.com> wrote:
> Hi K,
>
> Well, I /was/ planning on using an xs:duration attribute to specific a

> time out for one of our long lived component, where seconds is just
> fine as the level of granularity and quite easier to read than a
> milliseconds number.

OK. So essentially you are just interested in the time span in terms of
some fixed measure. Thank you for the input.

BTW, what's your plan if you see "1 month"? How many seconds should be
one month?


regards,
--
Kohsuke Kawaguchi                  408-276-7063 (x17063)
Sun Microsystems                   kohsuke.kawaguchi_at_sun.com