Source code: org/esau/ptarmigan/util/HelperDate.java
1 /* $Header: /cvsroot/ptarmigan/ptarmigan/src/java/org/esau/ptarmigan/util/HelperDate.java,v 1.1 2002/09/10 06:24:34 reedesau Exp $ */
2
3 package org.esau.ptarmigan.util;
4
5 import java.text.DateFormat;
6 import java.text.SimpleDateFormat;
7 import java.util.Calendar;
8 import java.util.Date;
9
10 /**
11 * Some static methods related to use of Dates
12 *
13 * @author Reed Esau
14 * @version $Revision: 1.1 $ $Date: 2002/09/10 06:24:34 $
15 */
16 public final class HelperDate {
17
18 //
19 // date helpers
20 //
21
22 /**
23 * Create an ISO-8601 date string from the long, which
24 * is the number of msecs since the epoch.
25 */
26 public static String epochtoIso8601(long date, boolean date_only) {
27 if (date_only)
28 return iso_8601_date_only.format( new Date( date ) );
29 else
30 return iso_8601_date_time.format( new Date( date ) );
31 }
32
33 /** */
34 static final DateFormat iso_8601_date_time =
35 new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
36
37 /** */
38 static final DateFormat iso_8601_date_only =
39 new SimpleDateFormat("yyyy-MM-dd");
40
41
42 //
43 // date parsing support
44 //
45
46 /**
47 * attempt to extract a valid year from a 2 or 4 digit string
48 *
49 * Example:
50 * <pre>
51 * "65" is returned as 1965
52 * "01" is returned as 2001 (if year is 2002)
53 * "02" is returned as 2002 (if year is 2002)
54 * "03" is returned as 2003 (if year is 2002)
55 * "04" is returned as 2004 (if year is 2002)
56 * "05" is returned as 1905 (if year is 2002)
57 * "06" is returned as 1906 (if year is 2002)
58 * "1965" is returned as 1965
59 * "2015" is returned as 2015
60 * </pre>
61 *
62 * @return null if no year could be extracted
63 */
64 public static String extractYear(String val) {
65
66 final int FUDGE = 2;
67 int yyyy = 0;
68
69 if (val.length() == 2
70 && Character.isDigit(val.charAt(0))
71 && Character.isDigit(val.charAt(1))) {
72
73 int yy = HelperMisc.parseInt(val, 0);
74
75 int current_yy = (m_current_year%100);
76
77 yyyy = m_current_year - current_yy + yy;
78
79 // allow for N years into the future for specifying two-digit years;
80 // otherwise treat them as last century
81 int threshold = current_yy + FUDGE;
82
83 if (yy > threshold)
84 yyyy -= 100;
85 }
86 else if (val.length() == 4
87 && Character.isDigit(val.charAt(0))
88 && Character.isDigit(val.charAt(1))
89 && Character.isDigit(val.charAt(2))
90 && Character.isDigit(val.charAt(3))) {
91 yyyy = HelperMisc.parseInt(val, 0);
92 }
93
94 return(yyyy > 0
95 ? Integer.toString(yyyy)
96 : null);
97 }
98
99 //
100 // class variables
101 //
102
103 /** the current year */
104 static int m_current_year = Calendar.getInstance().get(Calendar.YEAR);
105
106 /**
107 * logging object
108 */
109 //static Log log = LogFactory.getLog(HelperDate.class);
110 }
111
112 /*
113 PTARMIGAN MODIFIED BSD LICENSE
114
115 Copyright (c) 2002, Reed Esau (reed.esau@pobox.com) All rights reserved.
116
117 Redistribution and use in source and binary forms, with or without
118 modification, are permitted provided that the following conditions are
119 met:
120
121 Redistributions of source code must retain the above copyright notice,
122 this list of conditions and the following disclaimer.
123
124 Redistributions in binary form must reproduce the above copyright notice,
125 this list of conditions and the following disclaimer in the documentation
126 and/or other materials provided with the distribution.
127
128 Neither the name of the Ptarmigan Project
129 (http://ptarmigan.sourceforge.net) nor the names of its contributors may
130 be used to endorse or promote products derived from this software without
131 specific prior written permission.
132
133 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
134 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
135 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
136 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
137 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
138 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
139 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
140 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
141 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
142 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
143 POSSIBILITY OF SUCH DAMAGE.
144 */