1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package triptracker.core;
21
22 import static java.util.Calendar.*;
23
24 import java.net.URL;
25 import java.text.ParseException;
26 import java.text.SimpleDateFormat;
27 import java.util.Calendar;
28 import java.util.Date;
29
30 import javax.swing.ImageIcon;
31
32 /***
33 * Miscellaneous utility methods.
34 */
35 public class Utils {
36 /***
37 * Return the file extension of a file. Everything after the last "." is
38 * returned.
39 *
40 * @param fileName filename to check
41 * @return file extension
42 */
43 public static String getExtension(String fileName) {
44 String ext = "";
45 int i = fileName.lastIndexOf(".");
46
47 if (i > 0 && i < fileName.length() - 1) {
48 ext = fileName.substring(i + 1).toLowerCase();
49 }
50 return ext;
51 }
52
53 /***
54 * Retrieve an URL for a resource. This method ensures universal resource
55 * retrieval regardless if the application is packaged as a JAR file or not.
56 *
57 * @param file resource to retrieve
58 * @return resource URL
59 */
60 public static URL getResource(String file) {
61 return Utils.class.getResource(file);
62 }
63
64 /***
65 * Retrieve an ImageIcon from a resource file located in the "/resources/"
66 * folder. This method also works when the application is packaged as a JAR
67 * file.
68 *
69 * @param file resource to retrieve
70 * @return ImageIcon object
71 */
72 public static ImageIcon getImageIcon(String file) {
73 return (new ImageIcon(getResource("/resources/" + file)));
74 }
75
76 /***
77 * Rounds a number according to {@link Math#round(double)} with the
78 * specified number of decimals. It also works to set decimals to a negative
79 * number to round down to whole hundreds, thousands, etc.
80 *
81 * @param number number to round
82 * @param decimals number of decimals to preserve
83 * @return rounded number
84 */
85 public static double round(double number, int decimals) {
86 double mult = Math.pow(10, decimals);
87
88 return (Math.round(number * mult) / mult);
89 }
90
91 /***
92 * Rounds a number according to {@link Math#round(double)} with the
93 * specified number of decimals and returns a string with the exact number
94 * of decimals.
95 *
96 * @param number number to round
97 * @param decimals number of decimals to preserve
98 * @return rounded number
99 */
100 public static String roundStr(double number, int decimals) {
101 if (decimals < 0) {
102 throw new IllegalArgumentException("decimals < 0");
103 }
104
105
106 return Double.toString(round(number, decimals));
107 }
108
109 public static Date parseDate(String dateString) {
110 return parseDate(dateString, "yyyy-MM-dd HH:mm:ss");
111 }
112
113 public static Date parseDate(String dateString, String datePattern) {
114 SimpleDateFormat inFormat = new SimpleDateFormat(datePattern);
115 Date date;
116
117 try {
118 date = inFormat.parse(dateString);
119 } catch (ParseException e) {
120 date = null;
121 }
122
123 return date;
124 }
125
126 public static String dateToString(Date date) {
127 return dateToString(date, "yyyy-MM-dd HH:mm:ss");
128 }
129
130 public static String dateToString(Date date, String datePattern) {
131 return new SimpleDateFormat(datePattern).format(date);
132 }
133
134 public static String friendlyString(Date date1, Date date2) {
135 StringBuilder str = new StringBuilder();
136 Calendar cal = Calendar.getInstance();
137
138 cal.setTimeInMillis(Math.abs(date2.getTime() - date1.getTime()));
139
140
141 int years = cal.get(YEAR) - 1970;
142 int months = cal.get(MONTH);
143 int days = cal.get(DAY_OF_MONTH) - 1;
144 int hours = cal.get(HOUR_OF_DAY) - 1;
145 int min = cal.get(MINUTE);
146 int sec = cal.get(SECOND);
147 int ms = cal.get(MILLISECOND);
148
149 if (years > 0) {
150 str.append(years + (years == 1 ? " year " : " years "));
151 }
152 if (months > 0) {
153 str.append(months + (months == 1 ? " month " : " months "));
154 }
155 if (days > 0) {
156 str.append(days + (days == 1 ? " day " : " days "));
157 }
158 if (hours > 0) {
159 str.append(hours + (hours == 1 ? " hour " : " hours "));
160 }
161 if (min > 0) {
162 str.append(min + (min == 1 ? " minute " : " minutes "));
163 }
164 if (sec > 0) {
165 str.append(sec + (sec == 1 ? " second " : " seconds "));
166 }
167 if (ms > 0) {
168 str.append(ms + (ms == 1 ? " millisecond " : " milliseconds "));
169 }
170
171 if (str.length() > 0) {
172 str.setLength(str.length() - 1);
173 }
174
175 return str.toString();
176 }
177
178 /***
179 * Returns a boolean value from a string. The return value is true if, and
180 * only if the value is 1. In all other cases it returns false. It has the
181 * same effect as calling {@link #strToBool(String, boolean)} with the
182 * default value set to false.
183 *
184 * @param string string with the integer to parse
185 * @return true if string == 1, false if string != 1
186 * @throws NumberFormatException if the string is bogus
187 * @see #strToBool(String, boolean)
188 */
189 public static boolean strToBool(String string) {
190 return strToBool(string, false);
191 }
192
193 /***
194 * Returns a boolean value from a string. The return value is false if the
195 * value is 0, and true if the value is 1. If the string is any other number
196 * then the returned value is defined by the def parameter.
197 *
198 * @param string string with the integer to parse
199 * @param def default value if the value is neither 0 nor 1.
200 * @return true if string == 1, false if string != 1
201 * @throws NumberFormatException if the string is bogus
202 * @see #strToBool(String, boolean)
203 */
204 public static boolean strToBool(String string, boolean def) {
205 boolean result;
206 int val = Integer.parseInt(string);
207
208 switch (val) {
209 case 0: result = false; break;
210 case 1: result = true; break;
211 default: result = def; break;
212 }
213
214 return result;
215 }
216
217 }