Subversion Repositories XServices

Rev

Rev 66 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 66 Rev 83
Line 13... Line 13...
13
 *   See the License for the specific language governing permissions and
13
 *   See the License for the specific language governing permissions and
14
 *   limitations under the License.
14
 *   limitations under the License.
15
 */
15
 */
16
package net.brutex.xservices.types;
16
package net.brutex.xservices.types;
Line -... Line 17...
-
 
17
 
-
 
18
import java.text.ParseException;
-
 
19
import java.text.SimpleDateFormat;
-
 
20
import java.util.Date;
-
 
21
import java.util.Locale;
-
 
22
import java.util.TimeZone;
17
 
23
 
18
import javax.xml.bind.annotation.XmlEnum;
24
import javax.xml.bind.annotation.XmlEnum;
-
 
25
import javax.xml.bind.annotation.XmlEnumValue;
-
 
26
import javax.xml.bind.annotation.XmlType;
-
 
27
 
Line 19... Line 28...
19
import javax.xml.bind.annotation.XmlEnumValue;
28
import net.brutex.xservices.util.BrutexNamespaces;
20
 
29
 
21
/**
30
/**
22
 * Different pre-defined date formats.
31
 * Different pre-defined date formats.
23
 *
32
 *
24
 * @author Brian Rosenberger, bru@brutex.de
33
 * @author Brian Rosenberger, bru@brutex.de
25
 */
34
 */
Line 26... Line 35...
26
@XmlEnum(value = String.class)
35
@XmlEnum()
27
public enum DateFormatType {
36
public enum DateFormatType {
28
 
37
 
29
    /**
38
    /**
30
     * ISO 8601 format (2011-05-24T14:39Z)
39
     * ISO 8601 format (2011-05-24T14:39+01:00)
-
 
40
     */
-
 
41
	@XmlEnumValue("ISO 8601")
-
 
42
    ISO8601("ISO 8601", "yyyy-MM-dd'T'HH:mm:ss.SSSZ"),
-
 
43
    /**
-
 
44
     * RFC822 format (2011-05-24T14:39+0100)
31
     */
45
     */
32
    @XmlEnumValue("ISO 8601")
46
    @XmlEnumValue("RFC 822")
33
    ISO8601("ISO 8601", "yyyy-MM-dd'T'HH:mm:ssZ"),
47
    RFC822("RFC 822", "yyyy-MM-dd'T'HH:mm:ss.SSSZ"),
34
    /**
48
    /**
35
     * yyyy/mm/dd
49
     * yyyy/mm/dd
Line 36... Line 50...
36
     */
50
     */
37
    @XmlEnumValue("yyyy/mm/dd")
51
    @XmlEnumValue("DateOnly-slashed")
38
    YYYYMMDD("yyyy/mm/dd", "yyyy/MM/dd"),
52
    YYYYMMDD("DateOnly-slashed", "yyyy/MM/dd"),
39
    
53
    
40
    /**
54
    /**
-
 
55
     * dd.mm.yyyy
-
 
56
     */
-
 
57
    @XmlEnumValue("DateOnly-dotted")
-
 
58
    DDMMYYYY("DateOnly-dotted", "dd.MM.yyyy"),
-
 
59
    
-
 
60
	/**
Line 41... Line 61...
41
     * dd.mm.yyyy
61
     * dd.mm.yyyy
42
     */
62
     */
Line 43... Line 63...
43
    @XmlEnumValue("dd.mm.yyyy")
63
    @XmlEnumValue("DateOnly-dashed")
Line 57... Line 77...
57
     */
77
     */
58
    public String value() {
78
    public String value() {
59
        return value;
79
        return value;
60
    }
80
    }
Line -... Line 81...
-
 
81
    
-
 
82
    public String format(Date date, Locale locale, TimeZone timezone) {
-
 
83
    	if(date==null) return "";
-
 
84
    	if(locale==null) locale = Locale.getDefault();
61
    
85
    	if(timezone==null) timezone = TimeZone.getDefault();
-
 
86
    	String result = "";
-
 
87
    	SimpleDateFormat f;
-
 
88
    	if(this.equals(ISO8601)) {
-
 
89
    		//apply ISO8061 hack
-
 
90
    		f = new SimpleDateFormat(RFC822.format, locale);
-
 
91
    		f.setTimeZone(timezone);
-
 
92
    		result = f.format(date);
-
 
93
    		result = result.substring(0, 26) + ":" + result.substring(26);
-
 
94
    	} else {
-
 
95
        	f = new SimpleDateFormat(this.format, locale);
-
 
96
        	f.setTimeZone(timezone);
-
 
97
    		result = f.format(date);
62
    public String format() {
98
    	}
-
 
99
    	return result;
-
 
100
    }
-
 
101
    
-
 
102
    public Date parse(String s, Locale locale, TimeZone timezone)
-
 
103
    	throws ParseException {
-
 
104
    	if(locale==null) locale = Locale.getDefault();
-
 
105
    	if(timezone==null) timezone = TimeZone.getDefault();
-
 
106
    	SimpleDateFormat fin = new SimpleDateFormat(this.format, locale);
-
 
107
    	fin.setTimeZone(timezone);
-
 
108
    	Date date = fin.parse(s);
63
    	return format;
109
    	return date;    
64
    }
110
    }