Subversion Repositories XServices

Rev

Rev 83 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
66 brianR 1
/*
2
 *   Copyright 2011 Brian Rosenberger (Brutex Network)
3
 *
4
 *   Licensed under the Apache License, Version 2.0 (the "License");
5
 *   you may not use this file except in compliance with the License.
6
 *   You may obtain a copy of the License at
7
 *
8
 *       http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 *   Unless required by applicable law or agreed to in writing, software
11
 *   distributed under the License is distributed on an "AS IS" BASIS,
12
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 *   See the License for the specific language governing permissions and
14
 *   limitations under the License.
15
 */
16
package net.brutex.xservices.types;
17
 
83 brianR 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;
23
 
66 brianR 24
import javax.xml.bind.annotation.XmlEnum;
25
import javax.xml.bind.annotation.XmlEnumValue;
83 brianR 26
import javax.xml.bind.annotation.XmlType;
66 brianR 27
 
83 brianR 28
import net.brutex.xservices.util.BrutexNamespaces;
29
 
66 brianR 30
/**
31
 * Different pre-defined date formats.
32
 *
33
 * @author Brian Rosenberger, bru@brutex.de
34
 */
83 brianR 35
@XmlEnum()
66 brianR 36
public enum DateFormatType {
37
 
38
    /**
83 brianR 39
     * ISO 8601 format (2011-05-24T14:39+01:00)
66 brianR 40
     */
83 brianR 41
	@XmlEnumValue("ISO 8601")
42
    ISO8601("ISO 8601", "yyyy-MM-dd'T'HH:mm:ss.SSSZ"),
66 brianR 43
    /**
83 brianR 44
     * RFC822 format (2011-05-24T14:39+0100)
45
     */
46
    @XmlEnumValue("RFC 822")
47
    RFC822("RFC 822", "yyyy-MM-dd'T'HH:mm:ss.SSSZ"),
48
    /**
66 brianR 49
     * yyyy/mm/dd
50
     */
83 brianR 51
    @XmlEnumValue("DateOnly-slashed")
52
    YYYYMMDD("DateOnly-slashed", "yyyy/MM/dd"),
66 brianR 53
 
54
    /**
55
     * dd.mm.yyyy
56
     */
83 brianR 57
    @XmlEnumValue("DateOnly-dotted")
58
    DDMMYYYY("DateOnly-dotted", "dd.MM.yyyy"),
66 brianR 59
 
83 brianR 60
	/**
61
     * dd.mm.yyyy
62
     */
63
    @XmlEnumValue("DateOnly-dashed")
64
    DDMMYYYYdashed("DateOnly-dashed", "dd-MM-yyyy");
65
 
66 brianR 66
    private String value;
67
    private String format;
68
 
69
    DateFormatType(String value, String format) {
70
        this.value = value;
71
        this.format = format;
72
    }
73
 
74
    /**
75
     * Return the value of the enum.
76
     * @return  String representation of the mime type
77
     */
78
    public String value() {
79
        return value;
80
    }
81
 
83 brianR 82
    public String format(Date date, Locale locale, TimeZone timezone) {
83
    	if(date==null) return "";
84
    	if(locale==null) locale = Locale.getDefault();
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);
98
    	}
99
    	return result;
66 brianR 100
    }
83 brianR 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);
109
    	return date;
110
    }
66 brianR 111
}