1
|
/**
|
2
|
* Copyright (C) 2017 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
package eu.etaxonomy.cdm.vaadin.util;
|
10
|
|
11
|
import org.joda.time.Partial;
|
12
|
import org.joda.time.format.DateTimeFormatter;
|
13
|
import org.joda.time.format.ISODateTimeFormat;
|
14
|
|
15
|
import eu.etaxonomy.cdm.model.common.TimePeriod;
|
16
|
|
17
|
/**
|
18
|
*
|
19
|
* FIXME move into cdmlib
|
20
|
*
|
21
|
* @author a.kohlbecker
|
22
|
* @since Apr 26, 2017
|
23
|
*
|
24
|
*/
|
25
|
public class TimePeriodFormatter {
|
26
|
|
27
|
public enum Format {
|
28
|
/**
|
29
|
* yyyy-mm-dd or yyyy-mm or yyyy
|
30
|
*/
|
31
|
ISO8601
|
32
|
}
|
33
|
|
34
|
private Format format;
|
35
|
|
36
|
public TimePeriodFormatter(Format format) {
|
37
|
this.format = format;
|
38
|
}
|
39
|
|
40
|
public String print(TimePeriod timePeriod) {
|
41
|
|
42
|
switch (format) {
|
43
|
case ISO8601:
|
44
|
default:
|
45
|
return printISO8601(timePeriod);
|
46
|
}
|
47
|
}
|
48
|
|
49
|
/**
|
50
|
* @param datePublished
|
51
|
*/
|
52
|
private String printISO8601(TimePeriod datePublished) {
|
53
|
StringBuffer sb = new StringBuffer();
|
54
|
if (datePublished.getStart() != null) {
|
55
|
sb.append(datePublished.getStart().toString(determineISO860Formatter(datePublished.getStart())));
|
56
|
}
|
57
|
if (datePublished.getEnd() != null) {
|
58
|
if (sb.length() > 0) {
|
59
|
sb.append('-');
|
60
|
sb.append(datePublished.getEnd().toString(determineISO860Formatter(datePublished.getEnd())));
|
61
|
}
|
62
|
}
|
63
|
return sb.toString();
|
64
|
}
|
65
|
|
66
|
/**
|
67
|
* @param partial
|
68
|
* @return
|
69
|
*/
|
70
|
private DateTimeFormatter determineISO860Formatter(Partial partial) {
|
71
|
if (partial.isSupported(TimePeriod.DAY_TYPE)) {
|
72
|
return ISODateTimeFormat.yearMonthDay();
|
73
|
}
|
74
|
if (partial.isSupported(TimePeriod.MONTH_TYPE)) {
|
75
|
return ISODateTimeFormat.yearMonth();
|
76
|
}
|
77
|
return ISODateTimeFormat.year();
|
78
|
|
79
|
}
|
80
|
|
81
|
}
|