Rev 206 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
18 | jmachado | 1 | package jomm.utils; |
2 | |||
3 | import java.util.Date; |
||
206 | jmachado | 4 | import java.text.SimpleDateFormat; |
18 | jmachado | 5 | |
6 | /** |
||
7 | * @author Jorge Machado |
||
8 | * @date 15/Mar/2008 |
||
9 | * @time 15:04:18 |
||
10 | * @see jomm.utils |
||
11 | */ |
||
12 | public class DatesUtils |
||
13 | { |
||
14 | /** |
||
15 | * @param untilYear until Year |
||
16 | * @param untilMonth until Month |
||
17 | * @param untilDay until Day |
||
18 | * @param days to go back |
||
19 | * @return Return the start date calculating the go back days in choosed until date |
||
20 | */ |
||
21 | public static 5+0%2Fdocs%2Fapi+Date">Date getStartDate(int untilYear, int untilMonth, int untilDay, int days) |
||
22 | { |
||
23 | MyCalendar c = new MyCalendar(untilYear,untilMonth, untilDay); |
||
24 | 5+0%2Fdocs%2Fapi+Date">Date endDate = c.getTime(); |
||
25 | return new 5+0%2Fdocs%2Fapi+Date">Date(c.getTimeInMillis() - ((long)days * 24 * 60 * 60 * 1000)); |
||
26 | } |
||
27 | |||
28 | /** |
||
29 | * |
||
30 | * @param untilYear until Year |
||
31 | * @param untilMonth until Month |
||
32 | * @param months to go back |
||
33 | * @return the start date going back m months. The returned date allays is placed in first day of the month |
||
34 | */ |
||
35 | public static 5+0%2Fdocs%2Fapi+Date">Date getStartDate(int untilYear, int untilMonth, int months) |
||
36 | { |
||
37 | MyCalendar c = new MyCalendar(untilYear,untilMonth, 1); |
||
38 | int dif = untilMonth - months; |
||
39 | if(dif == 0) |
||
40 | { |
||
41 | return new MyCalendar(untilYear,1,1).getTime(); |
||
42 | } |
||
43 | else if(dif > 0) |
||
44 | { |
||
45 | return new MyCalendar(untilYear,untilMonth-dif,1).getTime(); |
||
46 | } |
||
47 | else |
||
48 | { |
||
49 | int div = dif / 12; |
||
50 | int rest = dif % 12; |
||
51 | if(rest < 0) |
||
52 | rest *= -1; |
||
53 | if(div < 0) |
||
54 | div *= -1; |
||
55 | int year = untilYear - 1 - div; |
||
56 | int month = 12 - rest; |
||
57 | return new MyCalendar(year,month,1).getTime(); |
||
58 | } |
||
59 | } |
||
60 | |||
206 | jmachado | 61 | |
62 | |||
18 | jmachado | 63 | } |