DateUtil.java

  1. /*
  2.  * Copyright (c) 2002-2022, City of Paris
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  *  1. Redistributions of source code must retain the above copyright notice
  10.  *     and the following disclaimer.
  11.  *
  12.  *  2. Redistributions in binary form must reproduce the above copyright notice
  13.  *     and the following disclaimer in the documentation and/or other materials
  14.  *     provided with the distribution.
  15.  *
  16.  *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
  17.  *     contributors may be used to endorse or promote products derived from
  18.  *     this software without specific prior written permission.
  19.  *
  20.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21.  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23.  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
  24.  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27.  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28.  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30.  * POSSIBILITY OF SUCH DAMAGE.
  31.  *
  32.  * License 1.0
  33.  */
  34. package fr.paris.lutece.util.date;

  35. import java.sql.Timestamp;
  36. import java.text.DateFormat;
  37. import java.text.ParseException;
  38. import java.text.SimpleDateFormat;
  39. import java.util.Date;
  40. import java.util.Locale;

  41. import org.apache.commons.lang3.StringUtils;

  42. import fr.paris.lutece.portal.service.i18n.I18nService;
  43. import fr.paris.lutece.portal.web.l10n.LocaleService;

  44. /**
  45.  * This class provides date utils.
  46.  */
  47. public final class DateUtil
  48. {
  49.     public static final String ISO_PATTERN_DATE = "yyyy-MM-dd HH:mm:ss";
  50.     private static final String CONSTANTE_PATTERN_DATE = "dd/MM/yyyy";
  51.     private static final long CONSTANT_NUMBER_MILISECONDS_IN_DAY = 86400000;

  52.     /**
  53.      * Creates a new DateUtil object
  54.      */
  55.     private DateUtil( )
  56.     {
  57.     }

  58.     /**
  59.      * Get the date from String date The format pattern is specified internaly
  60.      *
  61.      * @param strDate
  62.      *            the date to format
  63.      * @param locale
  64.      *            The Locale
  65.      * @return The Date or null else
  66.      */
  67.     public static Date formatDate( String strDate, Locale locale )
  68.     {
  69.         Date date = null;

  70.         if ( strDate != null )
  71.         {
  72.             DateFormat dateFormat = getDateFormat( locale );
  73.             try
  74.             {
  75.                 date = dateFormat.parse( strDate );
  76.             }
  77.             catch( ParseException e )
  78.             {
  79.                 return null;
  80.             }
  81.         }

  82.         return date;
  83.     }

  84.     /**
  85.      * Get the date from String date
  86.      *
  87.      * @param strDate
  88.      *            the date to format
  89.      * @param locale
  90.      *            The Locale
  91.      * @return The Date or null else
  92.      */
  93.     public static Date formatDateLongYear( String strDate, Locale locale )
  94.     {
  95.         Date date = null;

  96.         if ( ( strDate != null ) && ( strDate.trim( ).length( ) == CONSTANTE_PATTERN_DATE.length( ) ) )
  97.         {
  98.             date = formatDate( strDate, locale );
  99.         }

  100.         return date;
  101.     }

  102.     /**
  103.      * Get the {@link Timestamp} from String date The format pattern is specified internaly
  104.      *
  105.      * @param strDate
  106.      *            the date to format
  107.      * @param locale
  108.      *            The Locale
  109.      * @return The {@link Timestamp} or null else
  110.      */
  111.     public static Timestamp formatTimestamp( String strDate, Locale locale )
  112.     {
  113.         Date date = formatDate( strDate, locale );

  114.         if ( date == null )
  115.         {
  116.             return null;
  117.         }

  118.         return new Timestamp( date.getTime( ) );
  119.     }

  120.     /**
  121.      * Get the {@link java.sql.Date} from String date The format pattern is specified internaly
  122.      *
  123.      * @param strDate
  124.      *            the date to format
  125.      * @param locale
  126.      *            The Locale
  127.      * @return The {@link java.sql.Date} or null else
  128.      */
  129.     public static java.sql.Date formatDateSql( String strDate, Locale locale )
  130.     {
  131.         Date date = formatDate( strDate, locale );

  132.         if ( date == null )
  133.         {
  134.             return null;
  135.         }

  136.         return new java.sql.Date( date.getTime( ) );
  137.     }

  138.     /**
  139.      * Convert the date to String with a standard pattern
  140.      *
  141.      * @param date
  142.      *            The date to convert
  143.      * @param locale
  144.      *            The Locale
  145.      * @return The formated String
  146.      */
  147.     public static String getDateString( Date date, Locale locale )
  148.     {
  149.         DateFormat dateFormat = null;

  150.         if ( locale != null )
  151.         {
  152.             String strLocalizedDateFormat = I18nService.getDateFormatShortPattern( locale );

  153.             if ( ( strLocalizedDateFormat != null ) && !strLocalizedDateFormat.equals( "" ) )
  154.             {
  155.                 dateFormat = new SimpleDateFormat( strLocalizedDateFormat );
  156.             }
  157.             else
  158.             {
  159.                 dateFormat = DateFormat.getDateInstance( DateFormat.SHORT, locale );
  160.             }
  161.         }
  162.         else
  163.         {
  164.             dateFormat = DateFormat.getDateInstance( DateFormat.SHORT, LocaleService.getDefault( ) );
  165.         }

  166.         return dateFormat.format( date );
  167.     }

  168.     /**
  169.      * Converts a long value to a String date
  170.      *
  171.      * @param lTime
  172.      *            The long value to convert
  173.      * @param locale
  174.      *            the locale
  175.      * @return The formatted string
  176.      */
  177.     public static String getDateString( long lTime, Locale locale )
  178.     {
  179.         return getDateString( new Date( lTime ), locale );
  180.     }

  181.     /**
  182.      * Converts a Timestamp value to a String date
  183.      *
  184.      * @param date
  185.      *            The date
  186.      * @param locale
  187.      *            the locale
  188.      * @return The formatted string
  189.      */
  190.     public static String getDateString( java.sql.Timestamp date, Locale locale )
  191.     {
  192.         if ( date == null )
  193.         {
  194.             return "";
  195.         }

  196.         return getDateString( new Date( date.getTime( ) ), locale );
  197.     }

  198.     /**
  199.      * Converts the current Date to a String date
  200.      *
  201.      * @param locale
  202.      *            the locale
  203.      * @return The formatted string
  204.      */
  205.     public static String getCurrentDateString( Locale locale )
  206.     {
  207.         return getDateString( new Date( ), locale );
  208.     }

  209.     /**
  210.      * Return the pattern for date
  211.      *
  212.      * @param locale
  213.      *            the Locale
  214.      * @return The pattern as a String
  215.      */
  216.     public static String getDefaultPattern( Locale locale )
  217.     {
  218.         if ( locale != null )
  219.         {
  220.             String strLocalizedDateFormat = I18nService.getDateFormatShortPattern( locale );

  221.             if ( ( strLocalizedDateFormat != null ) && !strLocalizedDateFormat.equals( "" ) )
  222.             {
  223.                 return strLocalizedDateFormat;
  224.             }

  225.             DateFormat df = DateFormat.getDateInstance( DateFormat.SHORT, locale );

  226.             if ( df instanceof SimpleDateFormat )
  227.             {
  228.                 SimpleDateFormat sdf = (SimpleDateFormat) df;

  229.                 return sdf.toPattern( );
  230.             }
  231.         }

  232.         DateFormat df = DateFormat.getDateInstance( DateFormat.SHORT, LocaleService.getDefault( ) );

  233.         if ( df instanceof SimpleDateFormat )
  234.         {
  235.             SimpleDateFormat sdf = (SimpleDateFormat) df;

  236.             return sdf.toPattern( );
  237.         }

  238.         return null;
  239.     }

  240.     /**
  241.      * Get the number of milliseconds in a given number of days
  242.      *
  243.      * @param lDays
  244.      *            The number of days
  245.      * @return The number of milliseconds in the given number of days
  246.      */
  247.     public static long convertDaysInMiliseconds( long lDays )
  248.     {
  249.         return CONSTANT_NUMBER_MILISECONDS_IN_DAY * lDays;
  250.     }

  251.     /**
  252.      * Get the Date format for the locale
  253.      *
  254.      * @param locale
  255.      * @return
  256.      */
  257.     public static DateFormat getDateFormat( Locale locale )
  258.     {
  259.         DateFormat dateFormat = null;

  260.         if ( locale != null )
  261.         {
  262.             String strLocalizedDateFormat = I18nService.getDateFormatShortPattern( locale );

  263.             if ( StringUtils.isNotEmpty( strLocalizedDateFormat ) )
  264.             {
  265.                 dateFormat = new SimpleDateFormat( strLocalizedDateFormat );
  266.             }
  267.             else
  268.             {
  269.                 dateFormat = DateFormat.getDateInstance( DateFormat.SHORT, locale );
  270.             }
  271.         }
  272.         else
  273.         {
  274.             dateFormat = DateFormat.getDateInstance( DateFormat.SHORT, LocaleService.getDefault( ) );
  275.         }

  276.         dateFormat.setLenient( false );

  277.         return dateFormat;
  278.     }

  279.     /**
  280.      * Parse a date from ISO format ( yyyy-MM-dd HH:mm:ss )
  281.      *
  282.      * @param strDate
  283.      * @since 7.0.1
  284.      * @return parsed {@link Date} or null if the string can't be parsed
  285.      */
  286.     public static Date parseIsoDate( String strDate )
  287.     {
  288.         Date date = null;
  289.         if ( StringUtils.isNotBlank( strDate ) )
  290.         {
  291.             SimpleDateFormat format = new SimpleDateFormat( ISO_PATTERN_DATE );
  292.             try
  293.             {
  294.                 date = format.parse( strDate );
  295.             }
  296.             catch( ParseException e )
  297.             {
  298.                 return null;
  299.             }
  300.         }
  301.         return date;
  302.     }

  303.     /**
  304.      * Get the ISO format ( yyyy-MM-dd HH:mm:ss ) of a date
  305.      *
  306.      * @param date
  307.      * @since 7.0.1
  308.      * @return string of the date in iso format, or null if param is null
  309.      */
  310.     public static String getIsoDateString( Date date )
  311.     {
  312.         String strDate = null;
  313.         if ( date != null )
  314.         {
  315.             SimpleDateFormat format = new SimpleDateFormat( ISO_PATTERN_DATE );
  316.             strDate = format.format( date );
  317.         }
  318.         return strDate;
  319.     }
  320. }