2 * Copyright (C) 2007 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.
10 package eu
.etaxonomy
.taxeditor
.model
;
12 import eu
.etaxonomy
.cdm
.model
.common
.TimePeriod
;
15 * <p>TimeHelper class.</p>
22 public class TimeHelper
{
24 * Converts a <code>String</code> whose format is either a valid year
25 * or two valid years with the format "XXXX-XXXX" into a TimePeriod.
27 * @see #getValidYear(String yearStr)
28 * @param refYear a {@link java.lang.String} object.
29 * @throws java.lang.NumberFormatException if any.
30 * @return a {@link eu.etaxonomy.cdm.model.common.TimePeriod} object.
32 public static TimePeriod
convertTimePeriod(String refYear
) throws NumberFormatException
{
34 if (refYear
== null || ("").equals(refYear
)){
38 TimePeriod datePublished
= TimePeriod
.NewInstance();
40 // In case format is "xxxx-xxxx"
41 String
[] years
= refYear
.split("-");
43 // Unlikely case of "xxxx-xxxx-xxxx..."
44 if (years
.length
> 2) {
45 throw new NumberFormatException();
49 datePublished
.setStartYear(getValidYear(years
[0]));
51 // Format is "xxxx-xxxx"
52 if (years
.length
== 2) {
53 datePublished
.setEndYear(getValidYear(years
[1]));
60 * Checks whether a <code>String</code> is a valid year between
61 * 1750 and 2030. Throws a <code>NumberFormatException</code> if not.
63 * @param yearStr a {@link java.lang.String} object.
64 * @throws java.lang.NumberFormatException if any.
65 * @return a {@link java.lang.Integer} object.
67 public static Integer
getValidYear(String yearStr
) throws NumberFormatException
{
69 Integer yearInt
= null;
71 // Try casting string - don't catch number format exception
73 yearInt
= new Integer(yearStr
);
74 } catch (ClassCastException e
) {
75 throw new NumberFormatException();
78 // Is year in valid range?
79 if (yearInt
< 1750 || yearInt
> 2030) {
80 throw new NumberFormatException();