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;
33 import org.apache.oro.text.regex.Perl5Compiler;
35 import java.text.NumberFormat;
36 import java.util.Calendar;
37 import java.util.Date;
38 import java.util.GregorianCalendar;
39 import java.util.TimeZone;
43 public final class StringUtil {
45 private static TimeZone UTC = TimeZone.getTimeZone("UTC");
47 private StringUtil() { } // this avoids contruction
50 * Formats a number with the specified minimum and maximum number of digits.
52 public static synchronized String zeroPaddingNumber(long value, int minDigits,
55 NumberFormat numberFormat = NumberFormat.getInstance();
56 numberFormat.setMinimumIntegerDigits(minDigits);
57 numberFormat.setMaximumIntegerDigits(maxDigits);
58 return numberFormat.format(value);
62 * Wandelt Datum in einen 8-ziffrigen String um (yyyymmdd)
64 * @return 8-ziffriger String (yyyymmdd)
67 public static String date2webdbDate (GregorianCalendar theDate) {
68 StringBuffer webdbDate = new StringBuffer();
69 webdbDate.append(String.valueOf(theDate.get(Calendar.YEAR)));
70 webdbDate.append(pad2(theDate.get(Calendar.MONTH) + 1));
71 webdbDate.append(pad2(theDate.get(Calendar.DATE)));
73 return webdbDate.toString();
77 * Return a http://www.w3.org/TR/NOTE-datetime formatted date (yyyy-mm-ddThh:mm:ssTZ)
79 * @return w3approved datetime
82 public static String date2w3DateTime (GregorianCalendar theDate) {
83 StringBuffer webdbDate = new StringBuffer();
84 webdbDate.append(String.valueOf(theDate.get(Calendar.YEAR)));
85 webdbDate.append("-");
86 webdbDate.append(pad2(theDate.get(Calendar.MONTH) + 1));
87 webdbDate.append("-");
88 webdbDate.append(pad2(theDate.get(Calendar.DATE)));
89 webdbDate.append("T");
90 webdbDate.append(pad2(theDate.get(Calendar.HOUR_OF_DAY)));
91 webdbDate.append(":");
92 webdbDate.append(pad2(theDate.get(Calendar.MINUTE)));
93 webdbDate.append(":");
94 webdbDate.append(pad2(theDate.get(Calendar.SECOND)));
95 //assumes you are an hour-multiple away from UTC....
96 int offset=(theDate.get(Calendar.ZONE_OFFSET)/(60*60*1000));
98 webdbDate.append("-");
101 webdbDate.append("+");
103 webdbDate.append(pad2(Math.abs(offset)));
104 webdbDate.append(":00");
105 return webdbDate.toString();
109 * wandelt Calendar in dd.mm.yyyy / hh.mm um
111 * @return String mit (dd.mm.yyyy / hh.mm um)
113 public static String date2readableDateTime (GregorianCalendar theDate) {
114 String readable = "";
116 readable += pad2(theDate.get(Calendar.DATE));
117 readable += "." + pad2(theDate.get(Calendar.MONTH) + 1);
118 readable += "." + String.valueOf(theDate.get(Calendar.YEAR));
119 hour = theDate.get(Calendar.HOUR);
120 if (theDate.get(Calendar.AM_PM) == Calendar.PM)
122 readable += " / " + pad2(hour);
123 readable += ":" + pad2(theDate.get(Calendar.MINUTE));
128 * wandelt eine Datum in einen 8-buchstabigen String, der durch <code>/</code>
132 * @return String mit <code>/yyyy/mm/dd</code>
134 public static String webdbDate2path (String webdbDate) {
135 StringBuffer path = new StringBuffer();
136 path.append("/").append(webdbDate.substring(0, 4));
137 path.append("/").append(webdbDate.substring(4, 6));
140 //path.append("/").append(webdbDate.substring(6, 8));
141 return path.toString();
145 * L?scht <code>/</code> am Ende des Strings, falls vorhanden
147 * @return String ohne <code>/</code> am Ende
149 public static String removeSlash (String path) {
150 return path.length() > 1 && path.endsWith("/") ? path.substring(0, path.length()
155 * formatiert eine Zahl (0-99) zweistellig (z.B. 5 -> 05)
156 * @return zwistellige Zahl
158 public static String pad2 (int number) {
159 return number < 10 ? "0" + number : String.valueOf(number);
163 * formatiert eine Zahl (0-999) dreistellig (z.B. 7 -> 007)
165 * @return 3-stellige Zahl
167 public static String pad3 (int number) {
168 return number < 10 ? "00" + number : number < 100 ? "0" + number : String.valueOf(number);
172 * Liefert Default-Wert def zur?ck, wenn String <code>s</code>
177 * @return geparster int aus s oder def
179 public static int parseInt(String s, int def) {
180 if (s == null) return def;
182 return Integer.parseInt(s);
183 } catch (NumberFormatException e) {
190 * Converts mir's horrible internal date format (yyyy-MM-dd HH:mm:ss+zz) into a java Date
192 * @param anInternalDate
194 public static Date convertMirInternalDateToDate(String anInternalDate) {
195 Calendar calendar = new GregorianCalendar();
205 year = Integer.parseInt(anInternalDate.substring(0,4));
206 month = Integer.parseInt(anInternalDate.substring(5,7));
207 day = Integer.parseInt(anInternalDate.substring(8,10));
208 hours = Integer.parseInt(anInternalDate.substring(11,13));
209 minutes = Integer.parseInt(anInternalDate.substring(14,16));
210 seconds = Integer.parseInt(anInternalDate.substring(17,19));
212 timezoneOffset = Integer.parseInt(anInternalDate.substring(20,22));
213 if (anInternalDate.charAt(19) == '-') {
214 timezoneOffset = -timezoneOffset;
217 calendar.setTimeZone(UTC);
218 calendar.set(year, month-1, day, hours, minutes, seconds);
219 calendar.add(Calendar.HOUR, -timezoneOffset);
221 return calendar.getTime();
224 public static String regexpReplace(String anInput, String anExpression, String aReplacement) throws Exception {
225 Perl5Matcher matcher = new Perl5Matcher();
227 return Util.substitute(
228 matcher, new Perl5Compiler().compile(anExpression),
229 new Perl5Substitution(aReplacement), anInput,
230 Util.SUBSTITUTE_ALL);