public String getDateString(final Date date) {
   notNull("date", date);
   final DateFormat format;
   if (isMidnightDate(date, timeZoneManager)) {
     format = new SimpleDateFormat(YYYY_MM_DD2);
     format.setTimeZone(timeZoneManager.getLoggedInUserTimeZone());
   } else {
     format = new SimpleDateFormat(YYYY_MM_DD_HH_MM2);
     format.setTimeZone(timeZoneManager.getLoggedInUserTimeZone());
   }
   return format.format(date);
 }
  private Date parseDateForFormat(final String format, final String date) {
    // test date string matches format structure using regex
    // - weed out illegal characters and enforce 4-digit year
    // - create the regex based on the local format string
    String reFormat =
        Pattern.compile("d+|M+|H+|m+")
            .matcher(Matcher.quoteReplacement(format))
            .replaceAll("\\\\d{1,2}");
    reFormat = Pattern.compile("y+").matcher(reFormat).replaceAll("\\\\d{4,}");
    if (Pattern.compile(reFormat).matcher(date).matches()) {

      // date string matches format structure,
      // - now test it can be converted to a valid date
      SimpleDateFormat sdf = (SimpleDateFormat) DateFormat.getDateInstance();
      sdf.applyPattern(format);
      sdf.setTimeZone(timeZoneManager.getLoggedInUserTimeZone());
      sdf.setLenient(false);
      try {
        return sdf.parse(date);
      } catch (ParseException e) {
        return null;
      }
    }
    return null;
  }
 private static boolean isMidnightDate(final Date value, TimeZoneManager timeZoneManager) {
   Calendar cal = Calendar.getInstance();
   cal.setTime(value);
   cal.setTimeZone(timeZoneManager.getLoggedInUserTimeZone());
   return cal.get(Calendar.HOUR_OF_DAY) == 0
       && cal.get(Calendar.MINUTE) == 0
       && cal.get(Calendar.SECOND) == 0
       && cal.get(Calendar.MILLISECOND) == 0;
 }
  /**
   * This will round out a date to a precision based on the input string. We need to do this to be
   * more meaningful to the user when the type in x = '2011-10-13'.
   *
   * @param returnDate the parsed data in its specific form
   * @param acceptedFormat the format we used to
   * @param dateString
   * @return
   */
  private DateRange toPrecision(Date returnDate, String acceptedFormat, String dateString) {
    Precision precision = null;

    if (YYYY_MM_DD_HH_MM1.equals(acceptedFormat) || YYYY_MM_DD_HH_MM2.equals(acceptedFormat)) {
      precision = Precision.MINUTES;
    } else if (YYYY_MM_DD1.equals(acceptedFormat) || YYYY_MM_DD2.equals(acceptedFormat)) {
      precision = Precision.DAYS;
    } else if (ATLASSIAN_DURATION.equals(acceptedFormat)) {
      // ok its an atlassian duration
      precision = reverseParseAtlassianDuration(dateString.toLowerCase(Locale.ENGLISH));
    }

    if (precision != null) {
      return precision.createRange(returnDate, timeZoneManager.getLoggedInUserTimeZone());
    } else {
      // Is this even possible? probably not but here we are so something sensible
      return new DateRange(returnDate, returnDate);
    }
  }