001
002 /*****************************************************************************
003
004 jBudget.
005 Copyright (C) 2006 Dmitri Petrov
006
007 This program is free software: you can redistribute it and/or modify
008 it under the terms of the GNU General Public License as published by
009 the Free Software Foundation, either version 3 of the License, or
010 (at your option) any later version.
011
012 This program is distributed in the hope that it will be useful,
013 but WITHOUT ANY WARRANTY; without even the implied warranty of
014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015 GNU General Public License for more details.
016
017 You should have received a copy of the GNU General Public License
018 along with this program. If not, see <http://www.gnu.org/licenses/>.
019
020 /*****************************************************************************
021
022 /*
023 * DateFormater.java
024 *
025 * Created on May 16, 2006, 7:45 AM
026 */
027
028 package org.jbudget.util;
029
030 import java.text.DateFormatSymbols;
031 import java.text.ParseException;
032 import java.util.Calendar;
033 import java.util.Locale;
034
035 /**
036 * Utilities for formating objects rlated to dates.
037 * @author petrov
038 */
039 public class DateFormater
040 {
041 private final String[] monthStrings;
042 private final String[] shortMonthStrings;
043 private final String[] weekdayStrings;
044 private final String[] shortWeekdayStrings;
045
046 private final Calendar calendar;
047
048 /** Creates a new instance of DateFormater */
049 public DateFormater(){
050
051 calendar = Calendar.getInstance();
052
053 DateFormatSymbols symbols = new DateFormatSymbols();
054 monthStrings = symbols.getMonths();
055 shortMonthStrings = symbols.getShortMonths();
056 weekdayStrings = symbols.getWeekdays();
057 shortWeekdayStrings = symbols.getShortWeekdays();
058 }
059
060 /** Creates a new instance of DateFormater */
061 public DateFormater(Locale locale){
062
063 calendar = Calendar.getInstance(locale);
064
065 DateFormatSymbols symbols = new DateFormatSymbols(locale);
066 monthStrings = symbols.getMonths();
067 shortMonthStrings = symbols.getShortMonths();
068 weekdayStrings = symbols.getWeekdays();
069 shortWeekdayStrings = symbols.getShortWeekdays();
070 }
071
072 /** Returns a string representation of a month */
073 public String formatMonth (int month)
074 {
075 if (month < Calendar.JANUARY || month > Calendar.DECEMBER)
076 throw new IllegalArgumentException ("Bad month argument: " + month);
077
078 return monthStrings[month];
079 }
080
081 /** Returns a string representation of a month */
082 public String formatMonthShort (int month)
083 {
084 if (month < Calendar.JANUARY || month > Calendar.DECEMBER)
085 throw new IllegalArgumentException ("Bad month argument: " + month);
086
087 return shortMonthStrings[month];
088 }
089
090
091 /** Parses a string representation of a month */
092 public int parseMonth (String str) throws ParseException
093 {
094 while (str.startsWith(" "))
095 str = str.substring(1);
096 while (str.endsWith(" "))
097 str = str.substring(0, str.length()-1);
098
099 for(int month = Calendar.JANUARY; month <= Calendar.DECEMBER; month++)
100 if (
101 str.equalsIgnoreCase(monthStrings[month]) ||
102 str.equalsIgnoreCase(shortMonthStrings[month])
103 )
104 {
105 return month;
106 }
107
108 throw new ParseException("Could not parse month '" + str + "'", 0);
109 }
110
111
112 /** Returns a string representation of a week day */
113 public String formatWeekday (int day)
114 {
115 int min = calendar.getMinimum(Calendar.DAY_OF_WEEK);
116 int max = calendar.getMaximum(Calendar.DAY_OF_WEEK);
117 if (day < min || day > max)
118 throw new IllegalArgumentException ("Bad day argument: " + day);
119
120 return weekdayStrings[day];
121 }
122
123 /** Returns a string representation of a week day */
124 public String formatWeekdayShort (int day)
125 {
126 int min = calendar.getMinimum(Calendar.DAY_OF_WEEK);
127 int max = calendar.getMaximum(Calendar.DAY_OF_WEEK);
128 if (day < min || day > max)
129 throw new IllegalArgumentException ("Bad day argument: " + day);
130
131 return shortWeekdayStrings[day];
132 }
133
134
135 /** Parses a string representation of a weekday */
136 public int parseWeekday (String str) throws ParseException
137 {
138 while (str.startsWith(" "))
139 str = str.substring(1);
140 while (str.endsWith(" "))
141 str = str.substring(0, str.length()-1);
142
143 int min = calendar.getMinimum(Calendar.DAY_OF_WEEK);
144 int max = calendar.getMaximum(Calendar.DAY_OF_WEEK);
145
146 for(int day = min ; day <= max; day++)
147 if (
148 str.equalsIgnoreCase(weekdayStrings[day]) ||
149 str.equalsIgnoreCase(shortWeekdayStrings[day])
150 )
151 {
152 return day;
153 }
154
155 throw new ParseException("Could not parse a weekday string: '" + str + "'", 0);
156 }
157
158
159 }