source: ether_ndacc/trunk/common/implementation/com/ether/DateHelper.java @ 432

Last change on this file since 432 was 432, checked in by rboipsl, 12 years ago

clean

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