2 * Copyright (C) 2005 The Mir-coders group
4 * This file is part of Mir.
6 * Mir is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Mir is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Mir; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * In addition, as a special exception, The Mir-coders gives permission to link
21 * the code of this program with any library licensed under the Apache Software License.
22 * You must obey the GNU General Public License in all respects for all of the code used
23 * other than the above mentioned libraries. If you modify this file, you may extend this
24 * exception to your version of the file, but you are not obligated to do so.
25 * If you do not wish to do so, delete this exception statement from your version.
29 import org.apache.oro.text.regex.Pattern;
30 import org.apache.oro.text.regex.Perl5Matcher;
31 import org.apache.oro.text.regex.Perl5Substitution;
32 import org.apache.oro.text.regex.Util;
34 import java.text.NumberFormat;
35 import java.util.Calendar;
36 import java.util.Date;
37 import java.util.GregorianCalendar;
38 import java.util.TimeZone;
42 public final class StringUtil {
44 private static TimeZone UTC = TimeZone.getTimeZone("UTC");
46 private StringUtil() { } // this avoids contruction
49 * Formats a number with the specified minimum and maximum number of digits.
51 public static synchronized String zeroPaddingNumber(long value, int minDigits,
54 NumberFormat numberFormat = NumberFormat.getInstance();
55 numberFormat.setMinimumIntegerDigits(minDigits);
56 numberFormat.setMaximumIntegerDigits(maxDigits);
57 return numberFormat.format(value);
61 * Wandelt Datum in einen 8-ziffrigen String um (yyyymmdd)
63 * @return 8-ziffriger String (yyyymmdd)
66 public static String date2webdbDate (GregorianCalendar theDate) {
67 StringBuffer webdbDate = new StringBuffer();
68 webdbDate.append(String.valueOf(theDate.get(Calendar.YEAR)));
69 webdbDate.append(pad2(theDate.get(Calendar.MONTH) + 1));
70 webdbDate.append(pad2(theDate.get(Calendar.DATE)));
72 return webdbDate.toString();
76 * Return a http://www.w3.org/TR/NOTE-datetime formatted date (yyyy-mm-ddThh:mm:ssTZ)
78 * @return w3approved datetime
81 public static String date2w3DateTime (GregorianCalendar theDate) {
82 StringBuffer webdbDate = new StringBuffer();
83 webdbDate.append(String.valueOf(theDate.get(Calendar.YEAR)));
84 webdbDate.append("-");
85 webdbDate.append(pad2(theDate.get(Calendar.MONTH) + 1));
86 webdbDate.append("-");
87 webdbDate.append(pad2(theDate.get(Calendar.DATE)));
88 webdbDate.append("T");
89 webdbDate.append(pad2(theDate.get(Calendar.HOUR_OF_DAY)));
90 webdbDate.append(":");
91 webdbDate.append(pad2(theDate.get(Calendar.MINUTE)));
92 webdbDate.append(":");
93 webdbDate.append(pad2(theDate.get(Calendar.SECOND)));
94 //assumes you are an hour-multiple away from UTC....
95 int offset=(theDate.get(Calendar.ZONE_OFFSET)/(60*60*1000));
97 webdbDate.append("-");
100 webdbDate.append("+");
102 webdbDate.append(pad2(Math.abs(offset)));
103 webdbDate.append(":00");
104 return webdbDate.toString();
108 * wandelt Calendar in dd.mm.yyyy / hh.mm um
110 * @return String mit (dd.mm.yyyy / hh.mm um)
112 public static String date2readableDateTime (GregorianCalendar theDate) {
113 String readable = "";
115 readable += pad2(theDate.get(Calendar.DATE));
116 readable += "." + pad2(theDate.get(Calendar.MONTH) + 1);
117 readable += "." + String.valueOf(theDate.get(Calendar.YEAR));
118 hour = theDate.get(Calendar.HOUR);
119 if (theDate.get(Calendar.AM_PM) == Calendar.PM)
121 readable += " / " + pad2(hour);
122 readable += ":" + pad2(theDate.get(Calendar.MINUTE));
127 * wandelt eine Datum in einen 8-buchstabigen String, der durch <code>/</code>
131 * @return String mit <code>/yyyy/mm/dd</code>
133 public static String webdbDate2path (String webdbDate) {
134 StringBuffer path = new StringBuffer();
135 path.append("/").append(webdbDate.substring(0, 4));
136 path.append("/").append(webdbDate.substring(4, 6));
139 //path.append("/").append(webdbDate.substring(6, 8));
140 return path.toString();
144 * L?scht <code>/</code> am Ende des Strings, falls vorhanden
146 * @return String ohne <code>/</code> am Ende
148 public static String removeSlash (String path) {
149 return path.length() > 1 && path.endsWith("/") ? path.substring(0, path.length()
154 * formatiert eine Zahl (0-99) zweistellig (z.B. 5 -> 05)
155 * @return zwistellige Zahl
157 public static String pad2 (int number) {
158 return number < 10 ? "0" + number : String.valueOf(number);
162 * formatiert eine Zahl (0-999) dreistellig (z.B. 7 -> 007)
164 * @return 3-stellige Zahl
166 public static String pad3 (int number) {
167 return number < 10 ? "00" + number : number < 100 ? "0" + number : String.valueOf(number);
171 * Liefert Default-Wert def zur?ck, wenn String <code>s</code>
176 * @return geparster int aus s oder def
178 public static int parseInt(String s, int def) {
179 if (s == null) return def;
181 return Integer.parseInt(s);
182 } catch (NumberFormatException e) {
189 * Converts mir's horrible internal date format (yyyy-MM-dd HH:mm:ss+zz) into a java Date
191 * @param anInternalDate
193 public static Date convertMirInternalDateToDate(String anInternalDate) {
194 Calendar calendar = new GregorianCalendar();
204 year = Integer.parseInt(anInternalDate.substring(0,4));
205 month = Integer.parseInt(anInternalDate.substring(5,7));
206 day = Integer.parseInt(anInternalDate.substring(8,10));
207 hours = Integer.parseInt(anInternalDate.substring(11,13));
208 minutes = Integer.parseInt(anInternalDate.substring(14,16));
209 seconds = Integer.parseInt(anInternalDate.substring(17,19));
211 timezoneOffset = Integer.parseInt(anInternalDate.substring(20,22));
212 if (anInternalDate.charAt(19) == '-') {
213 timezoneOffset = -timezoneOffset;
216 calendar.setTimeZone(UTC);
217 calendar.set(year, month-1, day, hours, minutes, seconds);
218 calendar.add(Calendar.HOUR, -timezoneOffset);
220 return calendar.getTime();
223 public String regexpReplace(String anInput, Pattern anExpression, String aReplacement) {
224 Perl5Matcher matcher = new Perl5Matcher();
226 return Util.substitute(
227 matcher, anExpression,
228 new Perl5Substitution(aReplacement), anInput,
229 Util.SUBSTITUTE_ALL);