15,34 → 15,54 |
*/ |
package net.brutex.xservices.types; |
|
import java.text.ParseException; |
import java.text.SimpleDateFormat; |
import java.util.Date; |
import java.util.Locale; |
import java.util.TimeZone; |
|
import javax.xml.bind.annotation.XmlEnum; |
import javax.xml.bind.annotation.XmlEnumValue; |
import javax.xml.bind.annotation.XmlType; |
|
import net.brutex.xservices.util.BrutexNamespaces; |
|
/** |
* Different pre-defined date formats. |
* |
* @author Brian Rosenberger, bru@brutex.de |
*/ |
@XmlEnum(value = String.class) |
@XmlEnum() |
public enum DateFormatType { |
|
/** |
* ISO 8601 format (2011-05-24T14:39Z) |
* ISO 8601 format (2011-05-24T14:39+01:00) |
*/ |
@XmlEnumValue("ISO 8601") |
ISO8601("ISO 8601", "yyyy-MM-dd'T'HH:mm:ssZ"), |
ISO8601("ISO 8601", "yyyy-MM-dd'T'HH:mm:ss.SSSZ"), |
/** |
* RFC822 format (2011-05-24T14:39+0100) |
*/ |
@XmlEnumValue("RFC 822") |
RFC822("RFC 822", "yyyy-MM-dd'T'HH:mm:ss.SSSZ"), |
/** |
* yyyy/mm/dd |
*/ |
@XmlEnumValue("yyyy/mm/dd") |
YYYYMMDD("yyyy/mm/dd", "yyyy/MM/dd"), |
@XmlEnumValue("DateOnly-slashed") |
YYYYMMDD("DateOnly-slashed", "yyyy/MM/dd"), |
|
/** |
* dd.mm.yyyy |
*/ |
@XmlEnumValue("dd.mm.yyyy") |
DDMMYY("dd.mm.yyyy", "dd.MM.yyyy"); |
@XmlEnumValue("DateOnly-dotted") |
DDMMYYYY("DateOnly-dotted", "dd.MM.yyyy"), |
|
/** |
* dd.mm.yyyy |
*/ |
@XmlEnumValue("DateOnly-dashed") |
DDMMYYYYdashed("DateOnly-dashed", "dd-MM-yyyy"); |
|
private String value; |
private String format; |
|
59,7 → 79,33 |
return value; |
} |
|
public String format() { |
return format; |
public String format(Date date, Locale locale, TimeZone timezone) { |
if(date==null) return ""; |
if(locale==null) locale = Locale.getDefault(); |
if(timezone==null) timezone = TimeZone.getDefault(); |
String result = ""; |
SimpleDateFormat f; |
if(this.equals(ISO8601)) { |
//apply ISO8061 hack |
f = new SimpleDateFormat(RFC822.format, locale); |
f.setTimeZone(timezone); |
result = f.format(date); |
result = result.substring(0, 26) + ":" + result.substring(26); |
} else { |
f = new SimpleDateFormat(this.format, locale); |
f.setTimeZone(timezone); |
result = f.format(date); |
} |
return result; |
} |
|
public Date parse(String s, Locale locale, TimeZone timezone) |
throws ParseException { |
if(locale==null) locale = Locale.getDefault(); |
if(timezone==null) timezone = TimeZone.getDefault(); |
SimpleDateFormat fin = new SimpleDateFormat(this.format, locale); |
fin.setTimeZone(timezone); |
Date date = fin.parse(s); |
return date; |
} |
} |