def replace_template(template, cur_date):
    """Replace format strings in template with appropriate date fields.

    Args:
        template (str): Template string with format identifiers
        cur_date (datetime): Date being harvested

    Returns:
        str: Template string with format substrings replaced by the
             appropriate fields in the specified date.
    """
    trans_key = {"%Y": "{:04d}".format(cur_date.year),
                 "%m": "{:02d}".format(cur_date.month),
                 "%d": "{:02d}".format(cur_date.day),
                 "%H": "{:02d}".format(cur_date.hour),
                 "%M": "{:02d}".format(cur_date.minute),
                 "%S": "{:02d}".format(cur_date.second),
                 "%j": "{:03d}".format(cur_date.timetuple().tm_yday)}
    formatted_str = template
    for key, val in trans_key.items():
        formatted_str = formatted_str.replace(key, val)
    return formatted_str