@CanIgnoreReturnValue
  public GsonBuilder setDateFormat(String pattern) {
    if (pattern != null) {
      try {
        new SimpleDateFormat(pattern);
      } catch (IllegalArgumentException e) {
        // Throw exception if it is an invalid date format
        throw new IllegalArgumentException("The date pattern '" + pattern + "' is not valid", e);
      }
    }
    this.datePattern = pattern;
    return this;
  }
@SuppressWarnings("deprecation") // for GsonBuilder.setDateFormat(int)
  @Test
  public void testSetDateFormatInvalidStyle() {
    GsonBuilder builder = new GsonBuilder();

    IllegalArgumentException e =
        assertThrows(IllegalArgumentException.class, () -> builder.setDateFormat(-1));
    assertThat(e).hasMessageThat().isEqualTo("Invalid style: -1");

    e = assertThrows(IllegalArgumentException.class, () -> builder.setDateFormat(4));
    assertThat(e).hasMessageThat().isEqualTo("Invalid style: 4");

    e =
        assertThrows(
            IllegalArgumentException.class, () -> builder.setDateFormat(-1, DateFormat.FULL));
    assertThat(e).hasMessageThat().isEqualTo("Invalid style: -1");

    e =
        assertThrows(
            IllegalArgumentException.class, () -> builder.setDateFormat(DateFormat.FULL, -1));
    assertThat(e).hasMessageThat().isEqualTo("Invalid style: -1");
  }