Skip to content

Instantly share code, notes, and snippets.

@Kotlin-Native
Created July 10, 2015 07:55
Show Gist options
  • Select an option

  • Save Kotlin-Native/10f8d393be1d81d86a9f to your computer and use it in GitHub Desktop.

Select an option

Save Kotlin-Native/10f8d393be1d81d86a9f to your computer and use it in GitHub Desktop.
// Das Attribut 'format' wird als 'ThreadLocal' definiert:
private static ThreadLocal<SimpleDateFormat> format = new ThreadLocal<SimpleDateFormat>();
// Beim ersten Zugriff innerhalb eines Threads wird das Objekt initialisiert
private SimpleDateFormat getFormat() {
if (format.get() == null) {
format.set(new SimpleDateFormat("yyyy-MM-dd"));
}
return format.get();
}
@Override
public XMLGregorianCalendar unmarshal(String value) throws Exception {
...
// Hier wird die 'ThreadLocal' Instanz verwendet
Date date = getFormat().parse(value);
GregorianCalendar calendar = new GregorianCalendar();
calendar.setTime(date);
return DatatypeFactory.newInstance().newXMLGregorianCalendar(calendar);
...
}
@Override
public String marshal(XMLGregorianCalendar value) throws Exception {
// Hier wird die 'ThreadLocal' Instanz verwendet
return getFormat().format(value.toGregorianCalendar().getTime());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment