source: ether_ndacc/trunk/implementation/com/ether/DateUtils.java @ 97

Last change on this file since 97 was 97, checked in by rboipsl, 13 years ago

Import du projet NDACC _ module common

File size: 4.9 KB
Line 
1package com.ether;
2
3import java.text.DateFormat;
4import java.text.SimpleDateFormat;
5import java.util.Calendar;
6import java.util.Date;
7import java.util.Locale;
8
9public abstract class DateUtils
10{
11    /**
12     * Set the date with a default locale (french)
13     *
14     * @param date date to formate
15     * @return a new date set to midnight
16     */
17    public static Date to00h00m00s( final Date date )
18    {
19        assert ( null != date );
20        final Calendar calendar = Calendar.getInstance( Locale.FRENCH );
21
22        return setMinimumFields( date, calendar );
23    }
24
25    /**
26     * Set the date with a default locale (french)
27     *
28     * @param date date to formate
29     * @return a new date set to 23h 59m 59s
30     */
31    public static Date to23h59m59s( final Date date )
32    {
33        assert ( null != date );
34        final Calendar calendar = Calendar.getInstance( Locale.FRENCH );
35
36        return setMaximumFields( date, calendar );
37    }
38
39    /**
40     * Set the date using the given locale
41     *
42     * @param locale locale to use
43     * @param date   date to formate
44     * @return a new date set to midnight
45     */
46    public static Date to00h00m00s( final Locale locale, final Date date )
47    {
48        assert ( null != date );
49        final Calendar calendar = Calendar.getInstance( locale );
50
51        return setMinimumFields( date, calendar );
52    }
53
54    /**
55     * Set the date using the given locale
56     *
57     * @param locale locale to use
58     * @param date   date to formate
59     * @return a new date set to 23h 59m 59s
60     */
61    public static Date to23h59m59s( final Locale locale, final Date date )
62    {
63        assert ( null != date );
64        final Calendar calendar = Calendar.getInstance( locale );
65
66        return setMaximumFields( date, calendar );
67    }
68
69
70    private static Date setMinimumFields( final Date date, final Calendar calendar )
71    {
72        calendar.setTime( date );
73        calendar.set( Calendar.HOUR_OF_DAY, calendar.getMinimum( Calendar.HOUR_OF_DAY ) );
74        calendar.set( Calendar.MINUTE, calendar.getMinimum( Calendar.MINUTE ) );
75        calendar.set( Calendar.SECOND, calendar.getMinimum( Calendar.SECOND ) );
76
77        return calendar.getTime();
78    }
79
80    private static Date setMaximumFields( final Date date, final Calendar calendar )
81    {
82        calendar.setTime( date );
83        calendar.set( Calendar.HOUR_OF_DAY, calendar.getMaximum( Calendar.HOUR_OF_DAY ) );
84        calendar.set( Calendar.MINUTE, calendar.getMaximum( Calendar.MINUTE ) );
85        calendar.set( Calendar.SECOND, calendar.getMaximum( Calendar.SECOND ) );
86
87        return calendar.getTime();
88    }
89
90    public static String shortFormatFrench( final Date date )
91    {
92        return formatDate( date, FRENCH_DATE_PATTERN_SHORT );
93    }
94
95    public static String shortFormat( final Date date, final Locale locale )
96    {
97        if( date == null )
98            return "";
99
100        final DateFormat dateFormat = DateFormat.getDateInstance( DateFormat.SHORT, locale );
101
102        return dateFormat.format( date );
103    }
104
105    public static String orderedFormat( final Date date )
106    {
107        return formatDate( date, ORDERED_FORMAT );
108    }
109
110    public static String orderedFormatPlusGMT( final Date date )
111    {
112        return formatDate( date, ORDERED_FORMAT_PLUS_GMT );
113    }
114
115    public static String formatDate( final Date date, final String format )
116    {
117        if( date == null )
118            return "";
119
120        final SimpleDateFormat simpleDateFormat = new SimpleDateFormat( format );
121        return simpleDateFormat.format( date );
122    }
123   
124    public static Date datesAddMonth(final Date inputDate, final int number) 
125    {
126        /*final DateFormat shortDateFormat = new SimpleDateFormat("MMyyyy");
127        final DateFormat completDateFormat = new SimpleDateFormat("dd-MM-yyyy");             
128        final Date firstMois = shortDateFormat.parse("102008");
129        final String firstMoisString = completDateFormat.format(firstMois);*/
130       
131        final Calendar calendar = Calendar.getInstance();
132        calendar.setTime(inputDate);
133        calendar.add(Calendar.MONTH, number);
134         
135        return calendar.getTime();
136        //final String secondMoisString = completDateFormat.format(secondMois);
137                 
138    } 
139
140    public static Date datesAddYear(final Date inputDate, final int number) 
141    {
142       
143        final Calendar calendar = Calendar.getInstance();
144        calendar.setTime(inputDate);
145        calendar.add(Calendar.YEAR, number);
146         
147        return calendar.getTime();
148        //final String secondMoisString = completDateFormat.format(secondMois);
149                 
150    } 
151    public static final String FRENCH_DATE_PATTERN_SHORT = "dd/MM/yyyy";
152    public static final String ENGLISH_DATE_PATTERN_SHORT = "MM/dd/yyyy";
153    public static final String CHINESE_DATE_PATTERN_SHORT = "yyyy/MM/dd";
154
155    public static final String ORDERED_FORMAT_PLUS_GMT = "yyyy-MM-dd HH:mm:ss.SSSS Z";
156    public static final String ORDERED_FORMAT = "yyyy-MM-dd HH:mm:ss.SSSS";
157}
Note: See TracBrowser for help on using the repository browser.