Subversion Repositories bacoAlunos

Rev

Rev 1306 | Rev 1553 | 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
import pt.estgp.estgweb.Globals;
8
 
9
import java.text.MessageFormat;
678 jmachado 10
import java.text.SimpleDateFormat;
11
import java.util.ArrayList;
996 jmachado 12
import java.util.Date;
678 jmachado 13
import java.util.List;
206 jmachado 14
 
15
/**
16
 * @author Jorge Machado
17
 * @date 13/Jun/2008
18
 * @see pt.estgp.estgweb.utils
19
 */
20
public class DatesUtils
21
{
22
 
23
    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);
24
    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);
25
    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);
26
 
214 jmachado 27
    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);
28
    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);
29
    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 30
    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 31
 
995 jmachado 32
  /*  public static String getImportYear()
206 jmachado 33
    {
724 jmachado 34
        return ConfigProperties.getProperty("import.year");
35
//        MyCalendar mc = new MyCalendar();
36
//        int month = mc.getMonth();
37
//        MyCalendar firstYear;
38
//        MyCalendar secondYear;
39
//        if(month <= 9)
40
//        {
41
//            firstYear = new MyCalendar(mc.getYear() - 1,1,1);
42
//            secondYear = new MyCalendar(mc.getYear(),1,1);
43
//        }
44
//        else
45
//        {
46
//            firstYear = new MyCalendar(mc.getYear(),1,1);
47
//            secondYear = new MyCalendar(mc.getYear()+1,1,1);
48
//        }
49
//
50
//        String firstYearStr = SIGES_importFirstYearFormat.format(firstYear.getTime());
51
//        String secondYearStr = SIGES_importSecondYearFormat.format(secondYear.getTime());
52
//
53
//        return getFormatedImportYear(firstYearStr,secondYearStr,SIGES_importFinalFormat);
206 jmachado 54
    }
995 jmachado 55
*/
343 jmachado 56
    public static 1.5.0/docs/api/java/lang/String.html">String getSemestre()
57
    {
58
        MyCalendar mc = new MyCalendar();
59
        int month = mc.getMonth();
60
        if((month > 8 && month <=12) || (month >= 1 && month <=2))
61
        {
62
            return "S1";
63
        }
64
        else
65
        {
66
            return "S2";
67
        }
68
    }
69
 
767 jmachado 70
    public static 1.5.0/docs/api/java/lang/String.html">String getImportYearBefore(1.5.0/docs/api/java/lang/String.html">String year) {
71
        1.5.0/docs/api/java/lang/String.html">String year1 = year.substring(0,4);
72
 
73
        int year1Int = 1.5.0/docs/api/java/lang/Integer.html">Integer.parseInt(year1);
74
 
75
        year1Int -= 1;
76
        int year2Int = year1Int + 1;
77
        1.5.0/docs/api/java/lang/String.html">String part2 = ("" + year2Int).substring(2);
78
 
79
        1.5.0/docs/api/java/lang/String.html">String yearTarget = "" + year1Int + part2;
80
 
81
        return yearTarget;
82
    }
83
 
787 jmachado 84
 
85
    public static 1.5.0/docs/api/java/lang/String.html">String getImportYearFormatted(1.5.0/docs/api/java/lang/String.html">String year) {
86
        1.5.0/docs/api/java/lang/String.html">String year1 = year.substring(0,4);
87
        int year1Int = 1.5.0/docs/api/java/lang/Integer.html">Integer.parseInt(year1);
88
        int year2Int = year1Int + 1;
89
 
90
 
91
        1.5.0/docs/api/java/lang/String.html">String yearTarget = "" + year1Int + "/" + year2Int;
92
 
93
        return yearTarget;
94
    }
95
 
371 jmachado 96
    public static enum SemestreModerate
97
    {
98
        S1,
99
        S2,
100
        S1S2
101
    }
102
 
103
    public static SemestreModerate getSemestreModerate()
104
    {
105
        MyCalendar mc = new MyCalendar();
106
        int month = mc.getMonth();
107
        if(month > 8 && month <=12)
108
        {
109
            return SemestreModerate.S1;
110
        }
111
        else if(month >= 1 && month <=3)
112
        {
113
            return SemestreModerate.S1S2;
114
        }
115
        else
116
        {
117
            return SemestreModerate.S2;
118
        }
119
    }
120
 
995 jmachado 121
    public static 1.5.0/docs/api/java/lang/String.html">String getPreviousImportYear(1.5.0/docs/api/java/lang/String.html">String importYear)
122
    {
123
        1.5.0/docs/api/java/lang/String.html">String first = importYear.substring(0,4);
124
        1.5.0/docs/api/java/lang/String.html">String second = importYear.substring(4);
125
        first = ""  + (1.5.0/docs/api/java/lang/Integer.html">Integer.parseInt(first)-1);
126
        second = ""  + (1.5.0/docs/api/java/lang/Integer.html">Integer.parseInt(second)-1);
127
        return first+second;
128
    }
214 jmachado 129
    public static 1.5.0/docs/api/java/lang/String.html">String getImportYearIntranet()
206 jmachado 130
    {
214 jmachado 131
        MyCalendar mc = new MyCalendar();
132
        int month = mc.getMonth();
133
        MyCalendar firstYear;
134
        MyCalendar secondYear;
135
        if(month <= 8)
136
        {
137
            firstYear = new MyCalendar(mc.getYear() - 1,1,1);
138
            secondYear = new MyCalendar(mc.getYear(),1,1);
139
        }
140
        else
141
        {
142
            firstYear = new MyCalendar(mc.getYear(),1,1);
143
            secondYear = new MyCalendar(mc.getYear()+1,1,1);
144
        }
145
 
146
        1.5.0/docs/api/java/lang/String.html">String firstYearStr = SIGES_importFirstYearFormatIntranet.format(firstYear.getTime());
147
        1.5.0/docs/api/java/lang/String.html">String secondYearStr = SIGES_importSecondYearFormatIntranet.format(secondYear.getTime());
148
 
149
        return getFormatedImportYear(firstYearStr,secondYearStr,SIGES_importFinalFormatIntranet);
150
    }
151
 
345 jmachado 152
    public static 1.5.0/docs/api/java/lang/String.html">String getImportYearIntranet(1.5.0/docs/api/java/lang/String.html">String importYear)
153
    {
154
        1.5.0/docs/api/java/lang/String.html">String decadeStr = importYear.substring(0,2);
155
        if(importYear.substring(4,6).equals("00"))
156
        {
157
            1.5.0/docs/api/java/lang/String.html">String decade = importYear.substring(0,2);
158
            int nextdecade= 1.5.0/docs/api/java/lang/Integer.html">Integer.parseInt(decade)+1;
159
            decadeStr = "" + nextdecade;
160
        }
161
        return getFormatedImportYear(importYear.substring(0,4),decadeStr+importYear.substring(4,6),SIGES_importFinalFormatIntranet);
162
    }
163
 
726 jmachado 164
    public static 1.5.0/docs/api/java/lang/String.html">String getImportYearPrograms(1.5.0/docs/api/java/lang/String.html">String importYear)
165
    {
166
 
167
        return getFormatedImportYear(importYear.substring(0,4),importYear.substring(4,6),SIGES_importFinalFormatProgram);
168
    }
169
 
214 jmachado 170
    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)
171
    {
206 jmachado 172
        1.5.0/docs/api/java/lang/String.html">String[] years = {firstYearStr,secondYearStr};
214 jmachado 173
        return finalFormat.format(years);
206 jmachado 174
    }
175
 
996 jmachado 176
    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");
177
 
178
    /**
179
     *
180
     * @param d java.util.Date
181
     * @return a string formated like this:
182
     *
183
     * 20151103_043500
184
     *
185
     * for 2015/11/03 at 04:35:00 time
186
     */
187
    public static 1.5.0/docs/api/java/lang/String.html">String getFormatedFileSystem(5+0%2Fdocs%2Fapi+Date">Date d)
188
    {
189
        return formatFileSystem.format(d);
190
    }
191
 
206 jmachado 192
    public static List<String> getImportYears(int size)
193
    {
194
        MyCalendar mc = new MyCalendar();
195
        int month = mc.getMonth();
196
        MyCalendar firstYear;
197
        MyCalendar secondYear;
198
        if(month <= 8)
199
        {
200
            firstYear = new MyCalendar(mc.getYear() - 1,1,1);
201
            secondYear = new MyCalendar(mc.getYear(),1,1);
202
        }
203
        else
204
        {
205
            firstYear = new MyCalendar(mc.getYear(),1,1);
206
            secondYear = new MyCalendar(mc.getYear()+1,1,1);
207
        }
208
 
209
        1.5.0/docs/api/java/lang/String.html">String firstYearStr = SIGES_importFirstYearFormat.format(firstYear.getTime());
210
        1.5.0/docs/api/java/lang/String.html">String secondYearStr = SIGES_importSecondYearFormat.format(secondYear.getTime());
211
 
212
        List<String> importYears = new ArrayList<String>();
214 jmachado 213
        importYears.add(getFormatedImportYear(firstYearStr,secondYearStr,SIGES_importFinalFormat));
206 jmachado 214
 
215
        for(int i = 0; i < size;i++)
216
        {
217
            firstYear = new MyCalendar(firstYear.getYear() - 1,1,1);
218
            secondYear = new MyCalendar(secondYear.getYear() - 1,1,1);
219
            firstYearStr = SIGES_importFirstYearFormat.format(firstYear.getTime());
220
            secondYearStr = SIGES_importSecondYearFormat.format(secondYear.getTime());
214 jmachado 221
            importYears.add(getFormatedImportYear(firstYearStr,secondYearStr,SIGES_importFinalFormat));
206 jmachado 222
        }
223
        return importYears;
224
    }
225
 
215 jmachado 226
    public static List<String> getYears(int size)
227
    {
228
        List<String> years = new ArrayList<String>();
229
        MyCalendar c = new MyCalendar();
230
        for(int i = c.getYear(); i > c.getYear() - size;i-- )
231
            years.add("" + i);
232
        return years;
233
    }
234
 
995 jmachado 235
    public static void main(1.5.0/docs/api/java/lang/String.html">String [] args) throws JSONException {
236
 
237
        1.5.0/docs/api/java/lang/System.html">System.out.println(getPreviousImportYear("201516"));
238
        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\"} ] } }";
239
        JSONObject obj = new JSONObject(json);
240
        1.5.0/docs/api/java/lang/System.html">System.out.println(XML.toString(obj));
241
 
242
        /*
799 jmachado 243
        System.out.println(getImportYearIntranet("201213"));
767 jmachado 244
        System.out.println(getImportYearBefore("200001"));
995 jmachado 245
        System.out.println(DaoFactory.getConfigurationDaoImpl().getInterfaceImportYearCreateTransaction());
787 jmachado 246
        System.out.println(getImportYearFormatted("201314"));
247
        java.util.List<String> years = new java.util.ArrayList<String>();
248
        years.add("201314");
249
        years.add("201213");
250
 
251
        java.util.Collections.sort(years, new java.util.Comparator<String>()
252
        {
253
            public int compare(String o1, String o2) {
254
                return o2.compareTo(o1);
255
            }
256
        });
257
        for(String y: years)
258
            System.out.println(y);
995 jmachado 259
            */
206 jmachado 260
    }
261
}