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
.store
.model
;
12 import eu
.etaxonomy
.cdm
.model
.common
.TimePeriod
;
20 public class TimeUtil
{
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.
25 * @see #getValidYear(String yearStr)
28 * @throws NumberFormatException
30 public static TimePeriod
convertTimePeriod(String refYear
) throws NumberFormatException
{
32 if (refYear
== null || ("").equals(refYear
)){
36 TimePeriod datePublished
= TimePeriod
.NewInstance();
38 // In case format is "xxxx-xxxx"
39 String
[] years
= refYear
.split("-");
41 // Unlikely case of "xxxx-xxxx-xxxx..."
42 if (years
.length
> 2) {
43 throw new NumberFormatException();
47 datePublished
.setStartYear(getValidYear(years
[0]));
49 // Format is "xxxx-xxxx"
50 if (years
.length
== 2) {
51 datePublished
.setEndYear(getValidYear(years
[1]));
58 * Checks whether a <code>String</code> is a valid year between
59 * 1750 and 2030. Throws a <code>NumberFormatException</code> if not.
63 * @throws NumberFormatException
65 public static Integer
getValidYear(String yearStr
) throws NumberFormatException
{
67 Integer yearInt
= null;
69 // Try casting string - don't catch number format exception
71 yearInt
= new Integer(yearStr
);
72 } catch (ClassCastException e
) {
73 throw new NumberFormatException();
76 // Is year in valid range?
77 if (yearInt
< 1750 || yearInt
> 2030) {
78 throw new NumberFormatException();