예제 #1
0
  /**
   * Formats a date object for use in a mysql statement as a string.
   *
   * @param d - the date object to be converted
   * @return Stringified date in format shown below
   */
  public String formatAsMySQLDate(Date d) {
    String convertedDate = null;

    if (d == null || d.toString().length() == 0) {
      return "";
    }
    if (d.toString().length() <= RsDate.MySQLDateFormat.length()) {
      convertedDate = new SimpleDateFormat("yyyy-MM-dd").format(d);
    } else {
      convertedDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(d);
    }

    return convertedDate;
  }
예제 #2
0
  /**
   * Converts a string from a mysql result to a date.
   *
   * @param s String version of the date
   * @return Date object corresponding to date expressed by s
   * @throws ParseException
   */
  public Date convertStringToDate(String s) throws ParseException {
    Date convertedDate = null;

    if (s == null || s.length() == 0) return null;

    try {
      if (s.length() <= RsDate.MySQLDateFormat.length()) {
        SimpleDateFormat formatter = new SimpleDateFormat(RsDate.MySQLDateFormat);
        convertedDate = formatter.parse(s);
      } else {
        SimpleDateFormat formatter = new SimpleDateFormat(RsDate.MySQLDateTimeFormat);
        convertedDate = formatter.parse(s);
      }
    } catch (ParseException e) {
    }

    return convertedDate;
  }