from datetime import datetime


def _str_to_date(date_str, format_str=None):
    """ Convert a string to a date object

    :param date_str: the input date string
    :param format_str: format of the date string
    :exception: ValueError if string is not a valid date
    :return: the date object
    """
    if format_str is None:
        format_str = "%Y%m%d"
    try:
        ret = datetime.strptime(date_str, format_str).date()
    except ValueError:
        raise ValueError("input is not a valid date: {}".format(date_str))
    return ret