protected String addDatetimeToFilename( String filename, boolean addDate, String datePattern, boolean addTime,
                                          String timePattern, boolean specifyFormat, String datetimeFormat ) {

    if ( Utils.isEmpty( filename ) ) {
      return null;
    }

    // Replace possible environment variables...
    String realfilename = environmentSubstitute( filename );
    String filenameNoExtension = FilenameUtils.removeExtension( realfilename );
    String extension = FilenameUtils.getExtension( realfilename );

    // If an extension exists, add the corresponding dot before
    if ( !StringUtil.isEmpty( extension ) ) {
      extension = '.' + extension;
    }

    final SimpleDateFormat sdf = new SimpleDateFormat();
    Date now = new Date();

    if ( specifyFormat && !Utils.isEmpty( datetimeFormat ) ) {
      sdf.applyPattern( datetimeFormat );
      String dt = sdf.format( now );
      filenameNoExtension += dt;
    } else {
      if ( addDate && null != datePattern ) {
        sdf.applyPattern( datePattern );
        String d = sdf.format( now );
        filenameNoExtension += '_' + d;
      }
      if ( addTime && null != timePattern ) {
        sdf.applyPattern( timePattern );
        String t = sdf.format( now );
        filenameNoExtension += '_' + t;
      }
    }

    return filenameNoExtension + extension;
  }
@Test
  public void testAddDatetimeToFilename_ZipWithoutDotsInFolderWithDots() {
    JobEntryBase jobEntryBase = new JobEntryBase();
    String fullFilename;
    String filename = "/folder.with.dots/zip_without_dots_in_folder_with_dots";
    String regexFilename = regexDotEscape( filename );

    // add nothing
    fullFilename = jobEntryBase.addDatetimeToFilename( filename, false, null, false, null, false, null );
    assertNotNull( fullFilename );
    assertTrue( Pattern.matches( regexFilename, fullFilename ) );
    // add date
    fullFilename = jobEntryBase.addDatetimeToFilename( filename, true, "yyyyMMdd", false, null, false, null );
    assertNotNull( fullFilename );
    assertTrue( Pattern.matches( regexFilename + DATE_PATTERN, fullFilename ) );
    fullFilename = jobEntryBase.addDatetimeToFilename( filename, true, null, false, null, false, null );
    assertNotNull( fullFilename );
    assertEquals( filename, fullFilename );
    // add time
    fullFilename = jobEntryBase.addDatetimeToFilename( filename, false, null, true, "HHmmssSSS", false, null );
    assertNotNull( fullFilename );
    assertTrue( Pattern.matches( regexFilename + TIME_PATTERN, fullFilename ) );
    fullFilename = jobEntryBase.addDatetimeToFilename( filename, false, null, true, null, false, null );
    assertNotNull( fullFilename );
    assertEquals( filename, fullFilename );
    // add date and time
    fullFilename = jobEntryBase.addDatetimeToFilename( filename, true, "yyyyMMdd", true, "HHmmssSSS", false, null );
    assertNotNull( fullFilename );
    assertTrue( Pattern.matches( regexFilename + DATE_PATTERN + TIME_PATTERN, fullFilename ) );
    fullFilename = jobEntryBase.addDatetimeToFilename( filename, true, null, true, "HHmmssSSS", false, null );
    assertNotNull( fullFilename );
    assertTrue( Pattern.matches( regexFilename + TIME_PATTERN, fullFilename ) );
    fullFilename = jobEntryBase.addDatetimeToFilename( filename, true, "yyyyMMdd", true, null, false, null );
    assertNotNull( fullFilename );
    assertTrue( Pattern.matches( regexFilename + DATE_PATTERN, fullFilename ) );
    fullFilename = jobEntryBase.addDatetimeToFilename( filename, true, null, true, null, false, null );
    assertNotNull( fullFilename );
    assertEquals( filename, fullFilename );
    // add datetime
    fullFilename =
      jobEntryBase.addDatetimeToFilename( filename, false, null, false, null, true, "(yyyyMMdd_HHmmssSSS)" );
    assertNotNull( fullFilename );
    assertTrue( Pattern.matches( filename + DATE_TIME_PATTERN, fullFilename ) );
    fullFilename =
      jobEntryBase.addDatetimeToFilename( filename, false, null, false, null, true, null );
    assertNotNull( fullFilename );
    assertEquals( filename, fullFilename );
  }