Java Code Examples for java.time.format.DateTimeFormatter#toFormat()

The following examples show how to use java.time.format.DateTimeFormatter#toFormat() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TCKDateTimeFormatter.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
@Test(expectedExceptions=ParseException.class)
public void test_toFormat_parseObject_String_parseErrorLongText() throws Exception {
    DateTimeFormatter test = fmt.withLocale(Locale.ENGLISH).withDecimalStyle(DecimalStyle.STANDARD);
    Format format = test.toFormat();
    try {
        format.parseObject("ONEXXX67890123456789012345678901234567890123456789012345678901234567890123456789");
    } catch (DateTimeParseException ex) {
        assertEquals(ex.getMessage().contains("ONEXXX6789012345678901234567890123456789012345678901234567890123..."), true);
        assertEquals(ex.getParsedString(), "ONEXXX67890123456789012345678901234567890123456789012345678901234567890123456789");
        assertEquals(ex.getErrorIndex(), 3);
        throw ex;
    }
}
 
Example 2
Source File: TCKDateTimeFormatter.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_toFormat_parseObject_String() throws Exception {
    DateTimeFormatter test = fmt.withLocale(Locale.ENGLISH).withDecimalStyle(DecimalStyle.STANDARD);
    Format format = test.toFormat();
    TemporalAccessor result = (TemporalAccessor) format.parseObject("ONE30");
    assertEquals(result.isSupported(DAY_OF_MONTH), true);
    assertEquals(result.getLong(DAY_OF_MONTH), 30L);
}
 
Example 3
Source File: TCKDateTimeFormatter.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Test(expectedExceptions=ParseException.class)
public void test_toFormat_parseObject_String_parseErrorLongText() throws Exception {
    DateTimeFormatter test = fmt.withLocale(Locale.ENGLISH).withDecimalStyle(DecimalStyle.STANDARD);
    Format format = test.toFormat();
    try {
        format.parseObject("ONEXXX67890123456789012345678901234567890123456789012345678901234567890123456789");
    } catch (DateTimeParseException ex) {
        assertEquals(ex.getMessage().contains("ONEXXX6789012345678901234567890123456789012345678901234567890123..."), true);
        assertEquals(ex.getParsedString(), "ONEXXX67890123456789012345678901234567890123456789012345678901234567890123456789");
        assertEquals(ex.getErrorIndex(), 3);
        throw ex;
    }
}
 
Example 4
Source File: TCKDateTimeFormatter.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_toFormat_parseObject_StringParsePosition_parseError() throws Exception {
    DateTimeFormatter test = fmt.withLocale(Locale.ENGLISH).withDecimalStyle(DecimalStyle.STANDARD);
    Format format = test.toFormat();
    ParsePosition pos = new ParsePosition(0);
    TemporalAccessor result = (TemporalAccessor) format.parseObject("ONEXXX", pos);
    assertEquals(pos.getIndex(), 0);  // TODO: is this right?
    assertEquals(pos.getErrorIndex(), 3);
    assertEquals(result, null);
}
 
Example 5
Source File: TCKDateTimeFormatter.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Test(expectedExceptions=ParseException.class)
public void test_toFormat_parseObject_String_parseError() throws Exception {
    DateTimeFormatter test = fmt.withLocale(Locale.ENGLISH).withDecimalStyle(DecimalStyle.STANDARD);
    Format format = test.toFormat();
    try {
        format.parseObject("ONEXXX");
    } catch (ParseException ex) {
        assertEquals(ex.getMessage().contains("ONEXXX"), true);
        assertEquals(ex.getErrorOffset(), 3);
        throw ex;
    }
}
 
Example 6
Source File: TCKDateTimeFormatter.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_toFormat_parseObject_StringParsePosition_parseError() throws Exception {
    DateTimeFormatter test = fmt.withLocale(Locale.ENGLISH).withDecimalStyle(DecimalStyle.STANDARD);
    Format format = test.toFormat();
    ParsePosition pos = new ParsePosition(0);
    TemporalAccessor result = (TemporalAccessor) format.parseObject("ONEXXX", pos);
    assertEquals(pos.getIndex(), 0);  // TODO: is this right?
    assertEquals(pos.getErrorIndex(), 3);
    assertEquals(result, null);
}
 
Example 7
Source File: TCKDateTimeFormatter.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_toFormat_format() throws Exception {
    DateTimeFormatter test = fmt.withLocale(Locale.ENGLISH).withDecimalStyle(DecimalStyle.STANDARD);
    Format format = test.toFormat();
    String result = format.format(LocalDate.of(2008, 6, 30));
    assertEquals(result, "ONE30");
}
 
Example 8
Source File: TCKDateTimeFormatter.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_toFormat_parseObject_String() throws Exception {
    DateTimeFormatter test = fmt.withLocale(Locale.ENGLISH).withDecimalStyle(DecimalStyle.STANDARD);
    Format format = test.toFormat();
    TemporalAccessor result = (TemporalAccessor) format.parseObject("ONE30");
    assertEquals(result.isSupported(DAY_OF_MONTH), true);
    assertEquals(result.getLong(DAY_OF_MONTH), 30L);
}
 
Example 9
Source File: TCKDateTimeFormatter.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_toFormat_parseObject_StringParsePosition_parseError() throws Exception {
    DateTimeFormatter test = fmt.withLocale(Locale.ENGLISH).withDecimalStyle(DecimalStyle.STANDARD);
    Format format = test.toFormat();
    ParsePosition pos = new ParsePosition(0);
    TemporalAccessor result = (TemporalAccessor) format.parseObject("ONEXXX", pos);
    assertEquals(pos.getIndex(), 0);  // TODO: is this right?
    assertEquals(pos.getErrorIndex(), 3);
    assertEquals(result, null);
}
 
Example 10
Source File: TCKDateTimeFormatter.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_toFormat_parseObject_StringParsePosition() throws Exception {
    DateTimeFormatter test = fmt.withLocale(Locale.ENGLISH).withDecimalStyle(DecimalStyle.STANDARD);
    Format format = test.toFormat();
    ParsePosition pos = new ParsePosition(0);
    TemporalAccessor result = (TemporalAccessor) format.parseObject("ONE30XXX", pos);
    assertEquals(pos.getIndex(), 5);
    assertEquals(pos.getErrorIndex(), -1);
    assertEquals(result.isSupported(DAY_OF_MONTH), true);
    assertEquals(result.getLong(DAY_OF_MONTH), 30L);
}
 
Example 11
Source File: TCKDateTimeFormatter.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test(expected=NullPointerException.class)
public void test_toFormat_parseObject_StringParsePosition_nullParsePosition() throws Exception {
    // SimpleDateFormat has this behavior
    DateTimeFormatter test = fmt.withLocale(Locale.ENGLISH).withDecimalStyle(DecimalStyle.STANDARD);
    Format format = test.toFormat();
    format.parseObject("ONE30", (ParsePosition) null);
}
 
Example 12
Source File: TCKDateTimeFormatter.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
@Test(expectedExceptions=ParseException.class)
public void test_toFormat_parseObject_String_parseError() throws Exception {
    DateTimeFormatter test = fmt.withLocale(Locale.ENGLISH).withDecimalStyle(DecimalStyle.STANDARD);
    Format format = test.toFormat();
    try {
        format.parseObject("ONEXXX");
    } catch (ParseException ex) {
        assertEquals(ex.getMessage().contains("ONEXXX"), true);
        assertEquals(ex.getErrorOffset(), 3);
        throw ex;
    }
}
 
Example 13
Source File: TCKDateTimeFormatter.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_toFormat_parseObject_StringParsePosition() throws Exception {
    DateTimeFormatter test = fmt.withLocale(Locale.ENGLISH).withDecimalStyle(DecimalStyle.STANDARD);
    Format format = test.toFormat();
    ParsePosition pos = new ParsePosition(0);
    TemporalAccessor result = (TemporalAccessor) format.parseObject("ONE30XXX", pos);
    assertEquals(pos.getIndex(), 5);
    assertEquals(pos.getErrorIndex(), -1);
    assertEquals(result.isSupported(DAY_OF_MONTH), true);
    assertEquals(result.getLong(DAY_OF_MONTH), 30L);
}
 
Example 14
Source File: TCKDateTimeFormatter.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test(expectedExceptions=ParseException.class)
public void test_toFormat_parseObject_String_parseErrorLongText() throws Exception {
    DateTimeFormatter test = fmt.withLocale(Locale.ENGLISH).withDecimalStyle(DecimalStyle.STANDARD);
    Format format = test.toFormat();
    try {
        format.parseObject("ONEXXX67890123456789012345678901234567890123456789012345678901234567890123456789");
    } catch (DateTimeParseException ex) {
        assertEquals(ex.getMessage().contains("ONEXXX6789012345678901234567890123456789012345678901234567890123..."), true);
        assertEquals(ex.getParsedString(), "ONEXXX67890123456789012345678901234567890123456789012345678901234567890123456789");
        assertEquals(ex.getErrorIndex(), 3);
        throw ex;
    }
}
 
Example 15
Source File: TCKDateTimeFormatter.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_toFormat_parseObject_StringParsePosition_invalidPosition_tooBig() throws Exception {
    // SimpleDateFormat has this behavior
    DateTimeFormatter dtf = fmt.withLocale(Locale.ENGLISH).withDecimalStyle(DecimalStyle.STANDARD);
    ParsePosition pos = new ParsePosition(6);
    Format test = dtf.toFormat();
    assertNull(test.parseObject("ONE30", pos));
    assertTrue(pos.getErrorIndex() >= 0);
}
 
Example 16
Source File: TCKDateTimeFormatter.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_toFormat_parseObject_String() throws Exception {
    DateTimeFormatter test = fmt.withLocale(Locale.ENGLISH).withDecimalStyle(DecimalStyle.STANDARD);
    Format format = test.toFormat();
    TemporalAccessor result = (TemporalAccessor) format.parseObject("ONE30");
    assertEquals(result.isSupported(DAY_OF_MONTH), true);
    assertEquals(result.getLong(DAY_OF_MONTH), 30L);
}
 
Example 17
Source File: TCKDateTimeFormatter.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=NullPointerException.class)
public void test_toFormat_format_null() throws Exception {
    DateTimeFormatter test = fmt.withLocale(Locale.ENGLISH).withDecimalStyle(DecimalStyle.STANDARD);
    Format format = test.toFormat();
    format.format(null);
}
 
Example 18
Source File: TCKDateTimeFormatter.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=IllegalArgumentException.class)
public void test_toFormat_format_notTemporal() throws Exception {
    DateTimeFormatter test = fmt.withLocale(Locale.ENGLISH).withDecimalStyle(DecimalStyle.STANDARD);
    Format format = test.toFormat();
    format.format("Not a Temporal");
}
 
Example 19
Source File: TCKDateTimeFormatter.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=IllegalArgumentException.class)
public void test_toFormat_format_notTemporal() throws Exception {
    DateTimeFormatter test = fmt.withLocale(Locale.ENGLISH).withDecimalStyle(DecimalStyle.STANDARD);
    Format format = test.toFormat();
    format.format("Not a Temporal");
}
 
Example 20
Source File: TCKDateTimeFormatter.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=NullPointerException.class)
public void test_toFormat_format_null() throws Exception {
    DateTimeFormatter test = fmt.withLocale(Locale.ENGLISH).withDecimalStyle(DecimalStyle.STANDARD);
    Format format = test.toFormat();
    format.format(null);
}