Rev 1814 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
206 | jmachado | 1 | package pt.estgp.estgweb.utils; |
2 | |||
678 | jmachado | 3 | import jomm.utils.MyCalendar; |
995 | jmachado | 4 | import org.json.JSONException; |
5 | import org.json.JSONObject; |
||
6 | import org.json.XML; |
||
206 | jmachado | 7 | |
8 | import java.text.MessageFormat; |
||
678 | jmachado | 9 | import java.text.SimpleDateFormat; |
1827 | jmachado | 10 | import java.util.*; |
206 | jmachado | 11 | |
12 | /** |
||
13 | * @author Jorge Machado |
||
14 | * @date 13/Jun/2008 |
||
15 | * @see pt.estgp.estgweb.utils |
||
16 | */ |
||
17 | public class DatesUtils |
||
18 | { |
||
19 | |||
20 | static 1.5.0/docs/api/java/text/SimpleDateFormat.html">SimpleDateFormat SIGES_importFirstYearFormat = new 1.5.0/docs/api/java/text/SimpleDateFormat.html">SimpleDateFormat(Globals.SIGES_importFirstYearFormat); |
||
21 | static 1.5.0/docs/api/java/text/SimpleDateFormat.html">SimpleDateFormat SIGES_importSecondYearFormat = new 1.5.0/docs/api/java/text/SimpleDateFormat.html">SimpleDateFormat(Globals.SIGES_importSecondYearFormat); |
||
22 | static 1.5.0/docs/api/java/text/MessageFormat.html">MessageFormat SIGES_importFinalFormat = new 1.5.0/docs/api/java/text/MessageFormat.html">MessageFormat(Globals.SIGES_importFinalFormat); |
||
23 | |||
214 | jmachado | 24 | static 1.5.0/docs/api/java/text/SimpleDateFormat.html">SimpleDateFormat SIGES_importFirstYearFormatIntranet = new 1.5.0/docs/api/java/text/SimpleDateFormat.html">SimpleDateFormat(Globals.SIGES_importFirstYearFormatIntranet); |
25 | static 1.5.0/docs/api/java/text/SimpleDateFormat.html">SimpleDateFormat SIGES_importSecondYearFormatIntranet = new 1.5.0/docs/api/java/text/SimpleDateFormat.html">SimpleDateFormat(Globals.SIGES_importSecondYearFormatIntranet); |
||
26 | static 1.5.0/docs/api/java/text/MessageFormat.html">MessageFormat SIGES_importFinalFormatIntranet = new 1.5.0/docs/api/java/text/MessageFormat.html">MessageFormat(Globals.SIGES_importFinalFormatIntranet); |
||
726 | jmachado | 27 | static 1.5.0/docs/api/java/text/MessageFormat.html">MessageFormat SIGES_importFinalFormatProgram = new 1.5.0/docs/api/java/text/MessageFormat.html">MessageFormat(Globals.SIGES_importFinalFormatProgram); |
214 | jmachado | 28 | |
995 | jmachado | 29 | /* public static String getImportYear() |
206 | jmachado | 30 | { |
724 | jmachado | 31 | return ConfigProperties.getProperty("import.year"); |
32 | // MyCalendar mc = new MyCalendar(); |
||
33 | // int month = mc.getMonth(); |
||
34 | // MyCalendar firstYear; |
||
35 | // MyCalendar secondYear; |
||
36 | // if(month <= 9) |
||
37 | // { |
||
38 | // firstYear = new MyCalendar(mc.getYear() - 1,1,1); |
||
39 | // secondYear = new MyCalendar(mc.getYear(),1,1); |
||
40 | // } |
||
41 | // else |
||
42 | // { |
||
43 | // firstYear = new MyCalendar(mc.getYear(),1,1); |
||
44 | // secondYear = new MyCalendar(mc.getYear()+1,1,1); |
||
45 | // } |
||
46 | // |
||
47 | // String firstYearStr = SIGES_importFirstYearFormat.format(firstYear.getTime()); |
||
48 | // String secondYearStr = SIGES_importSecondYearFormat.format(secondYear.getTime()); |
||
49 | // |
||
50 | // return getFormatedImportYear(firstYearStr,secondYearStr,SIGES_importFinalFormat); |
||
206 | jmachado | 51 | } |
995 | jmachado | 52 | */ |
343 | jmachado | 53 | public static 1.5.0/docs/api/java/lang/String.html">String getSemestre() |
54 | { |
||
55 | MyCalendar mc = new MyCalendar(); |
||
56 | int month = mc.getMonth(); |
||
57 | if((month > 8 && month <=12) || (month >= 1 && month <=2)) |
||
58 | { |
||
59 | return "S1"; |
||
60 | } |
||
61 | else |
||
62 | { |
||
63 | return "S2"; |
||
64 | } |
||
65 | } |
||
66 | |||
767 | jmachado | 67 | public static 1.5.0/docs/api/java/lang/String.html">String getImportYearBefore(1.5.0/docs/api/java/lang/String.html">String year) { |
68 | 1.5.0/docs/api/java/lang/String.html">String year1 = year.substring(0,4); |
||
69 | |||
70 | int year1Int = 1.5.0/docs/api/java/lang/Integer.html">Integer.parseInt(year1); |
||
71 | |||
72 | year1Int -= 1; |
||
73 | int year2Int = year1Int + 1; |
||
74 | 1.5.0/docs/api/java/lang/String.html">String part2 = ("" + year2Int).substring(2); |
||
75 | |||
76 | 1.5.0/docs/api/java/lang/String.html">String yearTarget = "" + year1Int + part2; |
||
77 | |||
78 | return yearTarget; |
||
79 | } |
||
80 | |||
787 | jmachado | 81 | |
82 | public static 1.5.0/docs/api/java/lang/String.html">String getImportYearFormatted(1.5.0/docs/api/java/lang/String.html">String year) { |
||
83 | 1.5.0/docs/api/java/lang/String.html">String year1 = year.substring(0,4); |
||
84 | int year1Int = 1.5.0/docs/api/java/lang/Integer.html">Integer.parseInt(year1); |
||
85 | int year2Int = year1Int + 1; |
||
86 | |||
87 | |||
88 | 1.5.0/docs/api/java/lang/String.html">String yearTarget = "" + year1Int + "/" + year2Int; |
||
89 | |||
90 | return yearTarget; |
||
91 | } |
||
92 | |||
1553 | jmachado | 93 | public static int getFirstFromImportYear(1.5.0/docs/api/java/lang/String.html">String year) { |
94 | 1.5.0/docs/api/java/lang/String.html">String year1 = year.substring(0,4); |
||
95 | |||
96 | return 1.5.0/docs/api/java/lang/Integer.html">Integer.parseInt(year1); |
||
97 | } |
||
98 | public static int getSecondFromImportYear(1.5.0/docs/api/java/lang/String.html">String year) { |
||
99 | 1.5.0/docs/api/java/lang/String.html">String year1 = year.substring(0,4); |
||
100 | |||
101 | return 1.5.0/docs/api/java/lang/Integer.html">Integer.parseInt(year1) + 1; |
||
102 | } |
||
103 | |||
1827 | jmachado | 104 | |
105 | |||
371 | jmachado | 106 | public static enum SemestreModerate |
107 | { |
||
108 | S1, |
||
109 | S2, |
||
110 | S1S2 |
||
111 | } |
||
112 | |||
113 | public static SemestreModerate getSemestreModerate() |
||
114 | { |
||
115 | MyCalendar mc = new MyCalendar(); |
||
116 | int month = mc.getMonth(); |
||
117 | if(month > 8 && month <=12) |
||
118 | { |
||
119 | return SemestreModerate.S1; |
||
120 | } |
||
121 | else if(month >= 1 && month <=3) |
||
122 | { |
||
123 | return SemestreModerate.S1S2; |
||
124 | } |
||
125 | else |
||
126 | { |
||
127 | return SemestreModerate.S2; |
||
128 | } |
||
129 | } |
||
130 | |||
995 | jmachado | 131 | public static 1.5.0/docs/api/java/lang/String.html">String getPreviousImportYear(1.5.0/docs/api/java/lang/String.html">String importYear) |
132 | { |
||
133 | 1.5.0/docs/api/java/lang/String.html">String first = importYear.substring(0,4); |
||
134 | 1.5.0/docs/api/java/lang/String.html">String second = importYear.substring(4); |
||
135 | first = "" + (1.5.0/docs/api/java/lang/Integer.html">Integer.parseInt(first)-1); |
||
136 | second = "" + (1.5.0/docs/api/java/lang/Integer.html">Integer.parseInt(second)-1); |
||
137 | return first+second; |
||
138 | } |
||
214 | jmachado | 139 | public static 1.5.0/docs/api/java/lang/String.html">String getImportYearIntranet() |
206 | jmachado | 140 | { |
214 | jmachado | 141 | MyCalendar mc = new MyCalendar(); |
142 | int month = mc.getMonth(); |
||
143 | MyCalendar firstYear; |
||
144 | MyCalendar secondYear; |
||
145 | if(month <= 8) |
||
146 | { |
||
147 | firstYear = new MyCalendar(mc.getYear() - 1,1,1); |
||
148 | secondYear = new MyCalendar(mc.getYear(),1,1); |
||
149 | } |
||
150 | else |
||
151 | { |
||
152 | firstYear = new MyCalendar(mc.getYear(),1,1); |
||
153 | secondYear = new MyCalendar(mc.getYear()+1,1,1); |
||
154 | } |
||
155 | |||
156 | 1.5.0/docs/api/java/lang/String.html">String firstYearStr = SIGES_importFirstYearFormatIntranet.format(firstYear.getTime()); |
||
157 | 1.5.0/docs/api/java/lang/String.html">String secondYearStr = SIGES_importSecondYearFormatIntranet.format(secondYear.getTime()); |
||
158 | |||
159 | return getFormatedImportYear(firstYearStr,secondYearStr,SIGES_importFinalFormatIntranet); |
||
160 | } |
||
161 | |||
345 | jmachado | 162 | public static 1.5.0/docs/api/java/lang/String.html">String getImportYearIntranet(1.5.0/docs/api/java/lang/String.html">String importYear) |
163 | { |
||
164 | 1.5.0/docs/api/java/lang/String.html">String decadeStr = importYear.substring(0,2); |
||
165 | if(importYear.substring(4,6).equals("00")) |
||
166 | { |
||
167 | 1.5.0/docs/api/java/lang/String.html">String decade = importYear.substring(0,2); |
||
168 | int nextdecade= 1.5.0/docs/api/java/lang/Integer.html">Integer.parseInt(decade)+1; |
||
169 | decadeStr = "" + nextdecade; |
||
170 | } |
||
171 | return getFormatedImportYear(importYear.substring(0,4),decadeStr+importYear.substring(4,6),SIGES_importFinalFormatIntranet); |
||
172 | } |
||
173 | |||
726 | jmachado | 174 | public static 1.5.0/docs/api/java/lang/String.html">String getImportYearPrograms(1.5.0/docs/api/java/lang/String.html">String importYear) |
175 | { |
||
176 | |||
177 | return getFormatedImportYear(importYear.substring(0,4),importYear.substring(4,6),SIGES_importFinalFormatProgram); |
||
178 | } |
||
179 | |||
214 | jmachado | 180 | public static 1.5.0/docs/api/java/lang/String.html">String getFormatedImportYear(1.5.0/docs/api/java/lang/String.html">String firstYearStr, 1.5.0/docs/api/java/lang/String.html">String secondYearStr, 1.5.0/docs/api/java/text/MessageFormat.html">MessageFormat finalFormat) |
181 | { |
||
206 | jmachado | 182 | 1.5.0/docs/api/java/lang/String.html">String[] years = {firstYearStr,secondYearStr}; |
214 | jmachado | 183 | return finalFormat.format(years); |
206 | jmachado | 184 | } |
185 | |||
996 | jmachado | 186 | static 1.5.0/docs/api/java/text/SimpleDateFormat.html">SimpleDateFormat formatFileSystem = new 1.5.0/docs/api/java/text/SimpleDateFormat.html">SimpleDateFormat("yyyyMMdd_HHmmss"); |
1333 | jmachado | 187 | static 1.5.0/docs/api/java/text/SimpleDateFormat.html">SimpleDateFormat LogDate = new 1.5.0/docs/api/java/text/SimpleDateFormat.html">SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); |
996 | jmachado | 188 | |
1333 | jmachado | 189 | public static 1.5.0/docs/api/java/lang/String.html">String getLogDateNow() |
190 | { |
||
191 | return LogDate.format(new 5+0%2Fdocs%2Fapi+Date">Date()); |
||
192 | } |
||
193 | |||
996 | jmachado | 194 | /** |
195 | * |
||
196 | * @param d java.util.Date |
||
197 | * @return a string formated like this: |
||
198 | * |
||
199 | * 20151103_043500 |
||
200 | * |
||
201 | * for 2015/11/03 at 04:35:00 time |
||
202 | */ |
||
203 | public static 1.5.0/docs/api/java/lang/String.html">String getFormatedFileSystem(5+0%2Fdocs%2Fapi+Date">Date d) |
||
204 | { |
||
205 | return formatFileSystem.format(d); |
||
206 | } |
||
207 | |||
206 | jmachado | 208 | public static List<String> getImportYears(int size) |
209 | { |
||
210 | MyCalendar mc = new MyCalendar(); |
||
211 | int month = mc.getMonth(); |
||
212 | MyCalendar firstYear; |
||
213 | MyCalendar secondYear; |
||
214 | if(month <= 8) |
||
215 | { |
||
216 | firstYear = new MyCalendar(mc.getYear() - 1,1,1); |
||
217 | secondYear = new MyCalendar(mc.getYear(),1,1); |
||
218 | } |
||
219 | else |
||
220 | { |
||
221 | firstYear = new MyCalendar(mc.getYear(),1,1); |
||
222 | secondYear = new MyCalendar(mc.getYear()+1,1,1); |
||
223 | } |
||
224 | |||
225 | 1.5.0/docs/api/java/lang/String.html">String firstYearStr = SIGES_importFirstYearFormat.format(firstYear.getTime()); |
||
226 | 1.5.0/docs/api/java/lang/String.html">String secondYearStr = SIGES_importSecondYearFormat.format(secondYear.getTime()); |
||
227 | |||
228 | List<String> importYears = new ArrayList<String>(); |
||
214 | jmachado | 229 | importYears.add(getFormatedImportYear(firstYearStr,secondYearStr,SIGES_importFinalFormat)); |
206 | jmachado | 230 | |
231 | for(int i = 0; i < size;i++) |
||
232 | { |
||
233 | firstYear = new MyCalendar(firstYear.getYear() - 1,1,1); |
||
234 | secondYear = new MyCalendar(secondYear.getYear() - 1,1,1); |
||
235 | firstYearStr = SIGES_importFirstYearFormat.format(firstYear.getTime()); |
||
236 | secondYearStr = SIGES_importSecondYearFormat.format(secondYear.getTime()); |
||
214 | jmachado | 237 | importYears.add(getFormatedImportYear(firstYearStr,secondYearStr,SIGES_importFinalFormat)); |
206 | jmachado | 238 | } |
239 | return importYears; |
||
240 | } |
||
241 | |||
215 | jmachado | 242 | public static List<String> getYears(int size) |
243 | { |
||
244 | List<String> years = new ArrayList<String>(); |
||
245 | MyCalendar c = new MyCalendar(); |
||
246 | for(int i = c.getYear(); i > c.getYear() - size;i-- ) |
||
247 | years.add("" + i); |
||
248 | return years; |
||
249 | } |
||
250 | |||
995 | jmachado | 251 | public static void main(1.5.0/docs/api/java/lang/String.html">String [] args) throws JSONException { |
252 | |||
253 | 1.5.0/docs/api/java/lang/System.html">System.out.println(getPreviousImportYear("201516")); |
||
254 | 1.5.0/docs/api/java/lang/String.html">String json = "{\"paises\": {\"pais\": [ {\"populacao\":196655014,\"sigla\":\"BR\",\"nome\":\"Brasil\"}, {\"populacao\":40764561,\"sigla\":\"AR\",\"nome\":\"Argentina\"} ] } }"; |
||
255 | JSONObject obj = new JSONObject(json); |
||
256 | 1.5.0/docs/api/java/lang/System.html">System.out.println(XML.toString(obj)); |
||
257 | |||
258 | /* |
||
799 | jmachado | 259 | System.out.println(getImportYearIntranet("201213")); |
767 | jmachado | 260 | System.out.println(getImportYearBefore("200001")); |
995 | jmachado | 261 | System.out.println(DaoFactory.getConfigurationDaoImpl().getInterfaceImportYearCreateTransaction()); |
787 | jmachado | 262 | System.out.println(getImportYearFormatted("201314")); |
263 | java.util.List<String> years = new java.util.ArrayList<String>(); |
||
264 | years.add("201314"); |
||
265 | years.add("201213"); |
||
266 | |||
267 | java.util.Collections.sort(years, new java.util.Comparator<String>() |
||
268 | { |
||
269 | public int compare(String o1, String o2) { |
||
270 | return o2.compareTo(o1); |
||
271 | } |
||
272 | }); |
||
273 | for(String y: years) |
||
274 | System.out.println(y); |
||
995 | jmachado | 275 | */ |
206 | jmachado | 276 | } |
1827 | jmachado | 277 | |
278 | public static int getHours(5+0%2Fdocs%2Fapi+Date">Date d) |
||
279 | { |
||
280 | 1.5.0/docs/api/java/util/Calendar.html">Calendar calendar = 1.5.0/docs/api/java/util/GregorianCalendar.html">GregorianCalendar.getInstance(); // creates a new calendar instance |
||
281 | calendar.setTime(d); // assigns calendar to given date |
||
282 | return calendar.get(1.5.0/docs/api/java/util/Calendar.html">Calendar.HOUR_OF_DAY); // gets hour in 24h format |
||
283 | } |
||
284 | |||
285 | public static int getMinutes(5+0%2Fdocs%2Fapi+Date">Date d) |
||
286 | { |
||
287 | 1.5.0/docs/api/java/util/Calendar.html">Calendar calendar = 1.5.0/docs/api/java/util/GregorianCalendar.html">GregorianCalendar.getInstance(); // creates a new calendar instance |
||
288 | calendar.setTime(d); // assigns calendar to given date |
||
289 | return calendar.get(1.5.0/docs/api/java/util/Calendar.html">Calendar.MINUTE); |
||
290 | } |
||
291 | |||
292 | public static int getYear(5+0%2Fdocs%2Fapi+Date">Date d) { |
||
293 | 1.5.0/docs/api/java/util/Calendar.html">Calendar calendar = 1.5.0/docs/api/java/util/GregorianCalendar.html">GregorianCalendar.getInstance(); // creates a new calendar instance |
||
294 | calendar.setTime(d); // assigns calendar to given date |
||
295 | return calendar.get(1.5.0/docs/api/java/util/Calendar.html">Calendar.YEAR); |
||
296 | } |
||
297 | |||
298 | public static int getMonth(5+0%2Fdocs%2Fapi+Date">Date d) { |
||
299 | 1.5.0/docs/api/java/util/Calendar.html">Calendar calendar = 1.5.0/docs/api/java/util/GregorianCalendar.html">GregorianCalendar.getInstance(); // creates a new calendar instance |
||
300 | calendar.setTime(d); // assigns calendar to given date |
||
301 | return calendar.get(1.5.0/docs/api/java/util/Calendar.html">Calendar.MONTH) + 1; |
||
302 | } |
||
303 | |||
304 | public static int getDay(5+0%2Fdocs%2Fapi+Date">Date d) { |
||
305 | 1.5.0/docs/api/java/util/Calendar.html">Calendar calendar = 1.5.0/docs/api/java/util/GregorianCalendar.html">GregorianCalendar.getInstance(); // creates a new calendar instance |
||
306 | calendar.setTime(d); // assigns calendar to given date |
||
307 | return calendar.get(1.5.0/docs/api/java/util/Calendar.html">Calendar.DAY_OF_MONTH); |
||
308 | } |
||
309 | |||
206 | jmachado | 310 | } |