<commit_before>/**
 * Copyright (C) 2000 - 2009 Silverpeas
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * As a special exception to the terms and conditions of version 3.0 of
 * the GPL, you may redistribute this Program in connection with Free/Libre
 * Open Source Software ("FLOSS") applications as described in Silverpeas's
 * FLOSS exception.  You should have received a copy of the text describing
 * the FLOSS exception, and it is also available here:
 * "http://repository.silverpeas.com/legal/licensing"
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.stratelia.webactiv.util;

import com.silverpeas.util.StringUtil;
import com.stratelia.silverpeas.silvertrace.SilverTrace;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang.time.FastDateFormat;

/**
 * DateUtil is an helper class for date manipulation.
 * @author squere
 */
public class DateUtil {

  private static final long millisPerHour = 60l * 60l * 1000l;
  private static final long millisPerMinute = 60l * 1000l;
  private static Map<String, FastDateFormat> outputFormatters =
      new HashMap<String, FastDateFormat>(
          5);
  private static Map<String, SimpleDateFormat> inputParsers =
      new HashMap<String, SimpleDateFormat>(
          5);
  /**
   * Format and parse dates.
   */
  public static final SimpleDateFormat DATE_PARSER;
  public static final FastDateFormat DATE_FORMATTER;
  public static final SimpleDateFormat DATETIME_PARSER;
  public static final FastDateFormat DATETIME_FORMATTER;
  /**
   * Format and parse dates.
   */
  public static final SimpleDateFormat TIME_PARSER;
  public static final FastDateFormat TIME_FORMATTER;

  static {
    DATE_PARSER = new SimpleDateFormat("yyyy/MM/dd");
    DATE_PARSER.setLenient(false);
    DATE_FORMATTER = FastDateFormat.getInstance("yyyy/MM/dd");
    DATETIME_PARSER = new SimpleDateFormat("yyyy/MM/dd HH:mm");
    DATETIME_FORMATTER = FastDateFormat.getInstance("yyyy/MM/dd HH:mm");
    TIME_PARSER = new SimpleDateFormat("HH:mm");
    TIME_PARSER.setLenient(false);
    TIME_FORMATTER = FastDateFormat.getInstance("HH:mm");
  }

  /**
   * Display the date in a language specific standard format.
   * @param date the date to convert.
   * @param language The current user's language
   * @return A String representation of the date in the language specific format.
   */
  public static String dateToString(Date date, String language) {
    if (date == null) {
      return "";
    }
    FastDateFormat format = getDateOutputFormat(language);
    return format.format(date);
  }

  public static String getOutputDate(Date date, String language) {
    String result = "";
    if (date == null) {
      return result;
    }
    FastDateFormat formatter = getOutputFormatter(language);
    return formatter.format(date);
  }

  public static String getOutputDate(String dateDB, String language)
      throws ParseException {
    if (!StringUtil.isDefined(dateDB)) {
      return "";
    }
    Date date = parse(dateDB);
    return getOutputDate(date, language);
  }

  public static String getOutputDateAndHour(String dateDB, String language) throws ParseException {
    if (!StringUtil.isDefined(dateDB)) {
      return "";
    }
    Date date = parseDateTime(dateDB);
    return getOutputDateAndHour(date, language);
  }

  public static String getOutputDateAndHour(Date date, String language) {
    if (date == null) {
      return "";
    }
    FastDateFormat formatter = FastDateFormat.getInstance(getMultilangProperties(
        language).getString("dateOutputFormat")
        + " " + getMultilangProperties(language).getString("hourOutputFormat"));
    return formatter.format(date);
  }

  public static String getInputDate(Date date, String language) {
    if (date == null) {
      return "";
    }
    SimpleDateFormat parser = getInputFormatter(language);
    synchronized (parser) {
      return parser.format(date);
    }
  }

  public static String getInputDate(String dateDB, String language)
      throws ParseException {
    if (!StringUtil.isDefined(dateDB)) {
      return "";
    }
    Date date = parse(dateDB);
    return getInputDate(date, language);
  }

  /**
   * Parse the date in a language specific standard format.
   * @param string The String to convert in Date
   * @param language The current user's language
   * @throws java.text.ParseException if the input String is null, empty, or just in an incorrect
   * format.
   * @return A Date representation of the String in the language specific format.
   */
  public static Date stringToDate(String string, String language)
      throws ParseException {
    SimpleDateFormat format = getDateInputFormat(language);
    try {
      return format.parse(string);
    } catch (ParseException e) {
      throw e;
    } catch (Exception e) {
      throw new ParseException(e.getMessage(), 0);
    }
  }

  public static Date stringToDate(String date, String hour, String language)
      throws ParseException {
    try {
      SimpleDateFormat format;
      if (hour == null || "".equals(hour.trim())) {
        format = getDateInputFormat(language);
        return format.parse(date);
      }
      format = getDateAndHourInputFormat(language);
      return format.parse(date + " " + hour);
    } catch (Exception e) {
      throw new ParseException(e.getMessage(), 0);
    }
  }

  public static Date getDate(Date date, String hour) {
    if (date == null) {
      return null;
    }
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.set(Calendar.HOUR_OF_DAY, extractHour(hour));
    calendar.set(Calendar.MINUTE, extractMinutes(hour));
    return calendar.getTime();

  }

  public static int extractHour(String hour) {
    if (!StringUtil.isDefined(hour)) {
      return 0;
    }
    if (hour.indexOf(':') != -1) {
      return Integer.parseInt(hour.substring(0, hour.indexOf(':')));
    } else if (hour.indexOf('h') != -1) {
      return Integer.parseInt(hour.substring(0, hour.indexOf('h')));
    } else {
      return 0;
    }
  }

  public static int extractMinutes(String hour) {
    if (!StringUtil.isDefined(hour)) {
      return 0;
    }

    if (hour.indexOf(':') != -1) {
      return Integer.parseInt(hour.substring(hour.indexOf(':') + 1));
    } else if (hour.indexOf('h') != -1) {
      return Integer.parseInt(hour.substring(hour.indexOf('h') + 1));
    } else {
      return 0;
    }
  }

  /**
   * Get the date language specific standard output format.
   * @param lang The current user's language
   * @return A SimpleDateFormat initialized with the language specific output format.
   */
  public static FastDateFormat getDateOutputFormat(String lang) {
    return FastDateFormat.getInstance(getMultilangProperties(lang).getString("dateOutputFormat"));
  }

  /**
   * Get the date language specific standard input format.
   * @param language The current user's language
   * @return A SimpleDateFormat initialized with the language specific input format.
   */
  public static SimpleDateFormat getDateInputFormat(String language) {
    return new SimpleDateFormat(getMultilangProperties(language).getString("dateInputFormat"));
  }

  /**
   * Get the date language specific standard input format.
   * @param lang The current user's language
   * @return A SimpleDateFormat initialized with the language specific input format.
   */
  public static SimpleDateFormat getDateAndHourInputFormat(String lang) {
    return new SimpleDateFormat(getMultilangProperties(lang).getString("dateInputFormat") + " "
        + getMultilangProperties(lang).getString("hourOutputFormat"));
  }

  /**
   * Get all date multilang properties. This properties contains day and month labels, common date
   * format, and week first day.
   * @param language The current user's language.
   * @return The ResourceLocator containing all date multilang properties
   * @author squere
   */
  public static ResourceLocator getMultilangProperties(String language) {
    return new ResourceLocator(
        "com.stratelia.webactiv.util.date.multilang.date", language);
  }

  /**
   * Parse a special String into a Date.
   * @param date (String) the format of this date must be yyyy/MM/dd
   * @return a java object Date
   * @throws ParseException
   */
  public static Date parse(String date) throws ParseException {
    synchronized (DATE_PARSER) {
      return DATE_PARSER.parse(date);
    }
  }

  /**
   * Parse a special String into a Date.
   * @param date (String) the format of this date must be yyyy/MM/dd
   * @param format (String) the whished format in according to the date parameter
   * @return a java object Date
   * @throws ParseException
   */
  public static Date parse(String date, String format) throws ParseException {
    SimpleDateFormat sdf = new SimpleDateFormat(format);
    return sdf.parse(date);
  }

  /**
   * Test if two dates are equal
   * @param date1 first date
   * @param date2 second date
   * @return true if both dates defined the same date
   */
  public static boolean datesAreEqual(Date date1, Date date2) {
    Calendar cDate1 = Calendar.getInstance();
    cDate1.setTime(date1);
    Calendar cDate2 = Calendar.getInstance();
    cDate2.setTime(date2);
    if (cDate1.get(Calendar.YEAR) != cDate2.get(Calendar.YEAR)) {
      return false;
    } else {
      if (cDate1.get(Calendar.MONTH) != cDate2.get(Calendar.MONTH)) {
        return false;
      } else {
        if (cDate1.get(Calendar.DATE) != cDate2.get(Calendar.DATE)) {
          return false;
        }
      }
    }
    return true;
  }

  public static String today2SQLDate() {
    return date2SQLDate(new Date());
  }

  public static String date2SQLDate(Date date) {
    return DATE_FORMATTER.format(date);
  }

  public static String date2SQLDate(String date, String language) throws ParseException {
    String result = null;
    Date oDate = null;
    if (StringUtil.isDefined(date)) {
      oDate = stringToDate(date, language);
    }
    if (oDate != null) {
      result = DateUtil.date2SQLDate(oDate);
    }
    return result;
  }

  public static String formatDuration(long duration) {
    long hourDuration = duration / millisPerHour;
    long minuteDuration = (duration % millisPerHour) / millisPerMinute;
    long secondDuration = ((duration % millisPerHour) % millisPerMinute) / 1000;
    StringBuilder result = new StringBuilder(10);
    if (hourDuration > 0) {
      if (hourDuration < 10) {
        result.append('0');
      }
      result.append(hourDuration).append('h');
      if (minuteDuration < 10) {
        result.append('0');
      }
      result.append(minuteDuration).append('m');
    } else if (hourDuration <= 0 && minuteDuration > 0) {
      result.append(minuteDuration).append('m');
    }<commit_msg>Adding new DateUtil's method : String firstDayOfMonth2SQLDate(Date date)<commit_after>/**
 * Copyright (C) 2000 - 2009 Silverpeas
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * As a special exception to the terms and conditions of version 3.0 of
 * the GPL, you may redistribute this Program in connection with Free/Libre
 * Open Source Software ("FLOSS") applications as described in Silverpeas's
 * FLOSS exception.  You should have received a copy of the text describing
 * the FLOSS exception, and it is also available here:
 * "http://repository.silverpeas.com/legal/licensing"
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.stratelia.webactiv.util;

import com.silverpeas.util.StringUtil;
import com.stratelia.silverpeas.silvertrace.SilverTrace;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang.time.FastDateFormat;

/**
 * DateUtil is an helper class for date manipulation.
 * @author squere
 */
public class DateUtil {

  private static final long millisPerHour = 60l * 60l * 1000l;
  private static final long millisPerMinute = 60l * 1000l;
  private static Map<String, FastDateFormat> outputFormatters =
      new HashMap<String, FastDateFormat>(
          5);
  private static Map<String, SimpleDateFormat> inputParsers =
      new HashMap<String, SimpleDateFormat>(
          5);
  /**
   * Format and parse dates.
   */
  public static final SimpleDateFormat DATE_PARSER;
  public static final FastDateFormat DATE_FORMATTER;
  public static final SimpleDateFormat DATETIME_PARSER;
  public static final FastDateFormat DATETIME_FORMATTER;
  /**
   * Format and parse dates.
   */
  public static final SimpleDateFormat TIME_PARSER;
  public static final FastDateFormat TIME_FORMATTER;

  static {
    DATE_PARSER = new SimpleDateFormat("yyyy/MM/dd");
    DATE_PARSER.setLenient(false);
    DATE_FORMATTER = FastDateFormat.getInstance("yyyy/MM/dd");
    DATETIME_PARSER = new SimpleDateFormat("yyyy/MM/dd HH:mm");
    DATETIME_FORMATTER = FastDateFormat.getInstance("yyyy/MM/dd HH:mm");
    TIME_PARSER = new SimpleDateFormat("HH:mm");
    TIME_PARSER.setLenient(false);
    TIME_FORMATTER = FastDateFormat.getInstance("HH:mm");
  }

  /**
   * Display the date in a language specific standard format.
   * @param date the date to convert.
   * @param language The current user's language
   * @return A String representation of the date in the language specific format.
   */
  public static String dateToString(Date date, String language) {
    if (date == null) {
      return "";
    }
    FastDateFormat format = getDateOutputFormat(language);
    return format.format(date);
  }

  public static String getOutputDate(Date date, String language) {
    String result = "";
    if (date == null) {
      return result;
    }
    FastDateFormat formatter = getOutputFormatter(language);
    return formatter.format(date);
  }

  public static String getOutputDate(String dateDB, String language)
      throws ParseException {
    if (!StringUtil.isDefined(dateDB)) {
      return "";
    }
    Date date = parse(dateDB);
    return getOutputDate(date, language);
  }

  public static String getOutputDateAndHour(String dateDB, String language) throws ParseException {
    if (!StringUtil.isDefined(dateDB)) {
      return "";
    }
    Date date = parseDateTime(dateDB);
    return getOutputDateAndHour(date, language);
  }

  public static String getOutputDateAndHour(Date date, String language) {
    if (date == null) {
      return "";
    }
    FastDateFormat formatter = FastDateFormat.getInstance(getMultilangProperties(
        language).getString("dateOutputFormat")
        + " " + getMultilangProperties(language).getString("hourOutputFormat"));
    return formatter.format(date);
  }

  public static String getInputDate(Date date, String language) {
    if (date == null) {
      return "";
    }
    SimpleDateFormat parser = getInputFormatter(language);
    synchronized (parser) {
      return parser.format(date);
    }
  }

  public static String getInputDate(String dateDB, String language)
      throws ParseException {
    if (!StringUtil.isDefined(dateDB)) {
      return "";
    }
    Date date = parse(dateDB);
    return getInputDate(date, language);
  }

  /**
   * Parse the date in a language specific standard format.
   * @param string The String to convert in Date
   * @param language The current user's language
   * @throws java.text.ParseException if the input String is null, empty, or just in an incorrect
   * format.
   * @return A Date representation of the String in the language specific format.
   */
  public static Date stringToDate(String string, String language)
      throws ParseException {
    SimpleDateFormat format = getDateInputFormat(language);
    try {
      return format.parse(string);
    } catch (ParseException e) {
      throw e;
    } catch (Exception e) {
      throw new ParseException(e.getMessage(), 0);
    }
  }

  public static Date stringToDate(String date, String hour, String language)
      throws ParseException {
    try {
      SimpleDateFormat format;
      if (hour == null || "".equals(hour.trim())) {
        format = getDateInputFormat(language);
        return format.parse(date);
      }
      format = getDateAndHourInputFormat(language);
      return format.parse(date + " " + hour);
    } catch (Exception e) {
      throw new ParseException(e.getMessage(), 0);
    }
  }

  public static Date getDate(Date date, String hour) {
    if (date == null) {
      return null;
    }
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.set(Calendar.HOUR_OF_DAY, extractHour(hour));
    calendar.set(Calendar.MINUTE, extractMinutes(hour));
    return calendar.getTime();

  }

  public static int extractHour(String hour) {
    if (!StringUtil.isDefined(hour)) {
      return 0;
    }
    if (hour.indexOf(':') != -1) {
      return Integer.parseInt(hour.substring(0, hour.indexOf(':')));
    } else if (hour.indexOf('h') != -1) {
      return Integer.parseInt(hour.substring(0, hour.indexOf('h')));
    } else {
      return 0;
    }
  }

  public static int extractMinutes(String hour) {
    if (!StringUtil.isDefined(hour)) {
      return 0;
    }

    if (hour.indexOf(':') != -1) {
      return Integer.parseInt(hour.substring(hour.indexOf(':') + 1));
    } else if (hour.indexOf('h') != -1) {
      return Integer.parseInt(hour.substring(hour.indexOf('h') + 1));
    } else {
      return 0;
    }
  }

  /**
   * Get the date language specific standard output format.
   * @param lang The current user's language
   * @return A SimpleDateFormat initialized with the language specific output format.
   */
  public static FastDateFormat getDateOutputFormat(String lang) {
    return FastDateFormat.getInstance(getMultilangProperties(lang).getString("dateOutputFormat"));
  }

  /**
   * Get the date language specific standard input format.
   * @param language The current user's language
   * @return A SimpleDateFormat initialized with the language specific input format.
   */
  public static SimpleDateFormat getDateInputFormat(String language) {
    return new SimpleDateFormat(getMultilangProperties(language).getString("dateInputFormat"));
  }

  /**
   * Get the date language specific standard input format.
   * @param lang The current user's language
   * @return A SimpleDateFormat initialized with the language specific input format.
   */
  public static SimpleDateFormat getDateAndHourInputFormat(String lang) {
    return new SimpleDateFormat(getMultilangProperties(lang).getString("dateInputFormat") + " "
        + getMultilangProperties(lang).getString("hourOutputFormat"));
  }

  /**
   * Get all date multilang properties. This properties contains day and month labels, common date
   * format, and week first day.
   * @param language The current user's language.
   * @return The ResourceLocator containing all date multilang properties
   * @author squere
   */
  public static ResourceLocator getMultilangProperties(String language) {
    return new ResourceLocator(
        "com.stratelia.webactiv.util.date.multilang.date", language);
  }

  /**
   * Parse a special String into a Date.
   * @param date (String) the format of this date must be yyyy/MM/dd
   * @return a java object Date
   * @throws ParseException
   */
  public static Date parse(String date) throws ParseException {
    synchronized (DATE_PARSER) {
      return DATE_PARSER.parse(date);
    }
  }

  /**
   * Parse a special String into a Date.
   * @param date (String) the format of this date must be yyyy/MM/dd
   * @param format (String) the whished format in according to the date parameter
   * @return a java object Date
   * @throws ParseException
   */
  public static Date parse(String date, String format) throws ParseException {
    SimpleDateFormat sdf = new SimpleDateFormat(format);
    return sdf.parse(date);
  }

  /**
   * Test if two dates are equal
   * @param date1 first date
   * @param date2 second date
   * @return true if both dates defined the same date
   */
  public static boolean datesAreEqual(Date date1, Date date2) {
    Calendar cDate1 = Calendar.getInstance();
    cDate1.setTime(date1);
    Calendar cDate2 = Calendar.getInstance();
    cDate2.setTime(date2);
    if (cDate1.get(Calendar.YEAR) != cDate2.get(Calendar.YEAR)) {
      return false;
    } else {
      if (cDate1.get(Calendar.MONTH) != cDate2.get(Calendar.MONTH)) {
        return false;
      } else {
        if (cDate1.get(Calendar.DATE) != cDate2.get(Calendar.DATE)) {
          return false;
        }
      }
    }
    return true;
  }

  public static String today2SQLDate() {
    return date2SQLDate(new Date());
  }

  public static String date2SQLDate(Date date) {
    return DATE_FORMATTER.format(date);
  }

  public static String date2SQLDate(String date, String language) throws ParseException {
    String result = null;
    Date oDate = null;
    if (StringUtil.isDefined(date)) {
      oDate = stringToDate(date, language);
    }
    if (oDate != null) {
      result = DateUtil.date2SQLDate(oDate);
    }
    return result;
  }

  /**
   * Returns the first date of month according to given date (ex. 26/08/2010)
   * @param date any date
   * @return first date of month in sql format (ie. 2010/08/01)
   */
  public static String firstDayOfMonth2SQLDate(Date date) {
    String sqlDate = DATE_FORMATTER.format(date);
    sqlDate = sqlDate.substring(0, sqlDate.length() - 2) + "01";
    return sqlDate;
  }

  public static String formatDuration(long duration) {
    long hourDuration = duration / millisPerHour;
    long minuteDuration = (duration % millisPerHour) / millisPerMinute;
    long secondDuration = ((duration % millisPerHour) % millisPerMinute) / 1000;
    StringBuilder result = new StringBuilder(10);
    if (hourDuration > 0) {
      if (hourDuration < 10) {
        result.append('0');
      }
      result.append(hourDuration).append('h');
      if (minuteDuration < 10) {
        result.append('0');
      }
      result.append(minuteDuration).append('m');
    } else if (hourDuration <= 0 && minuteDuration > 0) {
      result.append(minuteDuration).append('m');
    }
{'fraction_non_alphanumeric': 0.0990103251788698, 'fraction_numerical': 0.007540379589563429, 'mean_word_length': 3.375257731958763, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2768672', 'n_tokens_mistral': 6789, 'n_tokens_neox': 6596, 'n_words': 2045}