2 * Copyright (C) 2020 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.format
.common
;
11 import org
.apache
.commons
.lang
.StringUtils
;
12 import org
.joda
.time
.DateTimeFieldType
;
13 import org
.joda
.time
.Partial
;
14 import org
.joda
.time
.format
.DateTimeFormatter
;
16 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
17 import eu
.etaxonomy
.cdm
.model
.common
.TimePeriod
;
23 public class TimePeriodFormatter
{
25 public static final DateTimeFieldType YEAR_TYPE
= DateTimeFieldType
.year();
26 public static final DateTimeFieldType MONTH_TYPE
= DateTimeFieldType
.monthOfYear();
27 public static final DateTimeFieldType DAY_TYPE
= DateTimeFieldType
.dayOfMonth();
28 public static final DateTimeFieldType HOUR_TYPE
= DateTimeFieldType
.hourOfDay();
29 public static final DateTimeFieldType MINUTE_TYPE
= DateTimeFieldType
.minuteOfHour();
31 public static final TimePeriodFormatter
NewDefaultInstance(){
32 return new TimePeriodFormatter();
35 private TimePeriodFormatter(){}
37 public String
format (TimePeriod timePeriod
){
39 if ( StringUtils
.isNotBlank(timePeriod
.getFreeText())){
40 result
= timePeriod
.getFreeText();
42 result
= getTimePeriod(timePeriod
);
48 * Returns the concatenation of <code>start</code> and <code>end</code>
50 public String
getTimePeriod(TimePeriod tp
){
52 DateTimeFormatter formatter
= TimePeriodPartialFormatter
.NewInstance();
53 if (tp
.isContinued()){
54 String strStart
= tp
.getStart() != null ? tp
.getStart().toString(formatter
): null;
55 result
= CdmUtils
.concat("", strStart
, "+");
57 Partial start
= tp
.getStart();
58 Partial end
= tp
.getEnd();
59 if (start
!= null && end
!= null){
60 if (start
.isSupported(YEAR_TYPE
) && end
.isSupported(YEAR_TYPE
)
61 && start
.get(YEAR_TYPE
) == end
.get(YEAR_TYPE
)){
62 if (start
.getFields().length
== 1){
65 start
= start
.without(YEAR_TYPE
);
68 if (end
!= null && !start
.isSupported(YEAR_TYPE
) && start
.isSupported(MONTH_TYPE
) &&
69 end
.isSupported(MONTH_TYPE
) && start
.get(MONTH_TYPE
) == end
.get(MONTH_TYPE
)){
70 start
= start
.without(MONTH_TYPE
);
73 String strStart
= start
!= null ? start
.toString(formatter
): null;
74 String strEnd
= end
!= null ? end
.toString(formatter
): null;
75 result
= CdmUtils
.concat(TimePeriod
.SEP
, strStart
, strEnd
);
81 public String
getYear(TimePeriod tp
){
83 if (tp
.getStartYear() != null){
84 result
+= String
.valueOf(tp
.getStartYear());
85 if (tp
.getEndYear() != null && !tp
.getStartYear().equals(tp
.getEndYear())){
86 result
+= TimePeriod
.SEP
+ String
.valueOf(tp
.getEndYear());
89 if (tp
.getEndYear() != null){
90 result
+= String
.valueOf(tp
.getEndYear());
93 if (tp
.isContinued()){