1
|
/**
|
2
|
* Copyright (C) 2007 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
|
|
10
|
package eu.etaxonomy.taxeditor.store.model;
|
11
|
|
12
|
import eu.etaxonomy.cdm.model.common.TimePeriod;
|
13
|
|
14
|
/**
|
15
|
* @autor p.ciardelli
|
16
|
* @author n.hoffmann
|
17
|
* @created 18.03.2009
|
18
|
* @version 1.0
|
19
|
*/
|
20
|
public class TimeUtil {
|
21
|
/**
|
22
|
* Converts a <code>String</code> whose format is either a valid year
|
23
|
* or two valid years with the format "XXXX-XXXX" into a TimePeriod.
|
24
|
*
|
25
|
* @see #getValidYear(String yearStr)
|
26
|
* @param refYear
|
27
|
* @return
|
28
|
* @throws NumberFormatException
|
29
|
*/
|
30
|
public static TimePeriod convertTimePeriod(String refYear) throws NumberFormatException {
|
31
|
|
32
|
if (refYear == null || ("").equals(refYear)){
|
33
|
return null;
|
34
|
}
|
35
|
|
36
|
TimePeriod datePublished = TimePeriod.NewInstance();
|
37
|
|
38
|
// In case format is "xxxx-xxxx"
|
39
|
String[] years = refYear.split("-");
|
40
|
|
41
|
// Unlikely case of "xxxx-xxxx-xxxx..."
|
42
|
if (years.length > 2) {
|
43
|
throw new NumberFormatException();
|
44
|
}
|
45
|
|
46
|
// Set startYear
|
47
|
datePublished.setStartYear(getValidYear(years[0]));
|
48
|
|
49
|
// Format is "xxxx-xxxx"
|
50
|
if (years.length == 2) {
|
51
|
datePublished.setEndYear(getValidYear(years[1]));
|
52
|
}
|
53
|
|
54
|
return datePublished;
|
55
|
}
|
56
|
|
57
|
/**
|
58
|
* Checks whether a <code>String</code> is a valid year between
|
59
|
* 1750 and 2030. Throws a <code>NumberFormatException</code> if not.
|
60
|
*
|
61
|
* @param yearStr
|
62
|
* @return
|
63
|
* @throws NumberFormatException
|
64
|
*/
|
65
|
public static Integer getValidYear(String yearStr) throws NumberFormatException {
|
66
|
|
67
|
Integer yearInt = null;
|
68
|
|
69
|
// Try casting string - don't catch number format exception
|
70
|
try {
|
71
|
yearInt = new Integer(yearStr);
|
72
|
} catch (ClassCastException e) {
|
73
|
throw new NumberFormatException();
|
74
|
}
|
75
|
|
76
|
// Is year in valid range?
|
77
|
if (yearInt < 1750 || yearInt > 2030) {
|
78
|
throw new NumberFormatException();
|
79
|
}
|
80
|
|
81
|
return yearInt;
|
82
|
}
|
83
|
}
|