1
2
3
4 package de.tivsource.page.admin.converter;
5
6 import java.text.SimpleDateFormat;
7 import java.util.Date;
8 import java.util.Locale;
9 import java.util.Map;
10
11 import org.apache.logging.log4j.LogManager;
12 import org.apache.logging.log4j.Logger;
13 import org.apache.struts2.util.StrutsTypeConverter;
14
15 import com.opensymphony.xwork2.conversion.TypeConversionException;
16
17
18
19
20
21 public class TimeConverter extends StrutsTypeConverter {
22
23
24
25
26 private static final Logger LOGGER = LogManager.getLogger(TimeConverter.class);
27
28
29
30
31 @SuppressWarnings("rawtypes")
32 @Override
33 public Object convertFromString(Map context, String[] values, Class toClass) {
34
35 if (values == null || values.length == 0 || values[0].trim().length() == 0) {
36 LOGGER.info("Keine Zeit angegeben.");
37 throw new TypeConversionException("Keine Zeit angegeben.");
38 }
39
40 try {
41 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy", Locale.ENGLISH);
42 LOGGER.info("Angegebener Zeit String: " + values[0]);
43 return simpleDateFormat.parse(values[0]);
44 } catch (Exception e) {
45 LOGGER.info("Keine gültige Zeit angegeben.");
46 throw new TypeConversionException("Keine gültige Zeit angegeben.");
47 }
48 }
49
50
51
52
53 @SuppressWarnings("rawtypes")
54 @Override
55 public String convertToString(Map context, Object o) {
56 Date time = (Date)o;
57 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy", Locale.ENGLISH);
58 return simpleDateFormat.format(time);
59 }
60
61 }