Search in sources :

Example 71 with DateTimeException

use of java.time.DateTimeException in project jdk8u_jdk by JetBrains.

the class TCKDateTimeFormatter method test_resolverFields_ignoreCrossCheck.

@Test
public void test_resolverFields_ignoreCrossCheck() throws Exception {
    DateTimeFormatter base = new DateTimeFormatterBuilder().appendValue(YEAR).appendLiteral('-').appendValue(DAY_OF_YEAR).appendLiteral('-').appendValue(DAY_OF_WEEK).toFormatter();
    DateTimeFormatter f = base.withResolverFields(YEAR, DAY_OF_YEAR);
    try {
        // wrong day-of-week
        base.parse("2012-321-1", LocalDate::from);
        fail();
    } catch (DateTimeException ex) {
    // expected, should fail in cross-check of day-of-week
    }
    // ignored wrong day-of-week
    LocalDate parsed = f.parse("2012-321-1", LocalDate::from);
    assertEquals(parsed, LocalDate.of(2012, 11, 16));
}
Also used : DateTimeException(java.time.DateTimeException) DateTimeFormatter(java.time.format.DateTimeFormatter) LocalDate(java.time.LocalDate) DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder) Test(org.testng.annotations.Test)

Example 72 with DateTimeException

use of java.time.DateTimeException in project jdk8u_jdk by JetBrains.

the class TCKWeekFields method test_parse_resolve_localizedWoWBY_strict.

@Test(dataProvider = "weekFields")
public void test_parse_resolve_localizedWoWBY_strict(DayOfWeek firstDayOfWeek, int minDays) {
    WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
    TemporalField wowbyField = week.weekOfWeekBasedYear();
    TemporalField yowbyField = week.weekBasedYear();
    DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(yowbyField).appendLiteral(':').appendValue(wowbyField).appendLiteral(':').appendValue(DAY_OF_WEEK).toFormatter().withResolverStyle(STRICT);
    String str = "2012:0:1";
    try {
        LocalDate date = LocalDate.parse(str, f);
        assertEquals(date.get(yowbyField), 2012);
        assertEquals(date.get(wowbyField), 0);
        assertEquals(date.get(DAY_OF_WEEK), 1);
    } catch (DateTimeException ex) {
    // expected
    }
}
Also used : TemporalField(java.time.temporal.TemporalField) DateTimeException(java.time.DateTimeException) WeekFields(java.time.temporal.WeekFields) DateTimeFormatter(java.time.format.DateTimeFormatter) LocalDate(java.time.LocalDate) DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder) Test(org.testng.annotations.Test) AbstractTCKTest(tck.java.time.AbstractTCKTest)

Example 73 with DateTimeException

use of java.time.DateTimeException in project jdk8u_jdk by JetBrains.

the class TestUmmAlQuraChronology method test_hijrahDateLimits.

// Check the date limits
@Test
public void test_hijrahDateLimits() {
    HijrahChronology chrono = HijrahChronology.INSTANCE;
    ValueRange yearRange = chrono.range(YEAR);
    ValueRange monthRange = chrono.range(MONTH_OF_YEAR);
    ValueRange dayRange = chrono.range(DAY_OF_MONTH);
    HijrahDate xx = chrono.date(1434, 1, 1);
    HijrahDate minDate = chrono.date((int) yearRange.getLargestMinimum(), (int) monthRange.getMinimum(), (int) dayRange.getMinimum());
    try {
        HijrahDate before = minDate.minus(1, ChronoUnit.DAYS);
        fail("Exception did not occur, minDate: " + minDate + ".minus(1, DAYS) = " + before);
    } catch (DateTimeException ex) {
    // ignore, this exception was expected
    }
    HijrahDate maxDate = chrono.date((int) yearRange.getSmallestMaximum(), (int) monthRange.getMaximum(), 1);
    int monthLen = maxDate.lengthOfMonth();
    maxDate = maxDate.with(DAY_OF_MONTH, monthLen);
    try {
        HijrahDate after = maxDate.plus(1, ChronoUnit.DAYS);
        fail("Exception did not occur, maxDate: " + maxDate + ".plus(1, DAYS) = " + after);
    } catch (DateTimeException ex) {
    // ignore, this exception was expected
    }
}
Also used : ValueRange(java.time.temporal.ValueRange) HijrahDate(java.time.chrono.HijrahDate) DateTimeException(java.time.DateTimeException) HijrahChronology(java.time.chrono.HijrahChronology) Test(org.testng.annotations.Test)

Example 74 with DateTimeException

use of java.time.DateTimeException in project jdk8u_jdk by JetBrains.

the class TestDateTimeBuilderCombinations method test_normalized.

@Test(dataProvider = "normalized")
public void test_normalized(final TemporalField field1, final Number value1, TemporalField expectedField, Number expectedVal) {
    // mock for testing that does not fully comply with TemporalAccessor contract
    TemporalAccessor test = new TemporalAccessor() {

        @Override
        public boolean isSupported(TemporalField field) {
            return field == field1;
        }

        @Override
        public long getLong(TemporalField field) {
            if (field == field1) {
                return value1.longValue();
            }
            throw new DateTimeException("Unsupported");
        }
    };
    DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(field1).toFormatter();
    String str = value1.toString();
    TemporalAccessor temporal = f.parse(str);
    if (expectedVal != null) {
        assertEquals(temporal.getLong(expectedField), expectedVal.longValue());
    } else {
        assertEquals(temporal.isSupported(expectedField), false);
    }
}
Also used : TemporalAccessor(java.time.temporal.TemporalAccessor) TemporalField(java.time.temporal.TemporalField) DateTimeException(java.time.DateTimeException) DateTimeFormatter(java.time.format.DateTimeFormatter) DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder) Test(org.testng.annotations.Test)

Example 75 with DateTimeException

use of java.time.DateTimeException in project jgnash by ccavanaugh.

the class YahooEventParser method retrieveNew.

public static Set<SecurityHistoryEvent> retrieveNew(final SecurityNode securityNode, final LocalDate endDate) {
    final Set<SecurityHistoryEvent> events = new HashSet<>();
    LocalDate startDate = LocalDate.now().minusDays(1);
    final List<SecurityHistoryNode> historyNodeList = securityNode.getHistoryNodes();
    if (historyNodeList.size() > 0) {
        startDate = historyNodeList.get(0).getLocalDate();
    }
    /*final List<SecurityHistoryEvent> historyEvents = new ArrayList<>(securityNode.getHistoryEvents());
        if (historyEvents.size() > 0) {
            Collections.sort(historyEvents);

            startDate = historyEvents.get(historyEvents.size() - 1).getDate().plusDays(1);
        }*/
    // s = symbol
    // a = start month -1
    // b = start day
    // c = start year
    // d = end month -1
    // e = end day
    // f = end year
    // g=v&y=0&z=30000 dividends only
    // http://ichart.finance.yahoo.com/x?s=IBM&a=00&b=2&c=1962&d=04&e=25&f=2011&g=v&y=0&z=30000
    /*
        Date,Dividends
        DIVIDEND, 20110506,0.750000
        DIVIDEND, 20110208,0.650000
        DIVIDEND, 20101108,0.650000
        DIVIDEND, 19971106,0.100000
        DIVIDEND, 19970807,0.100000
        SPLIT, 19970528,2:1
        DIVIDEND, 19970206,0.087500
        DIVIDEND, 19961106,0.087500
        STARTDATE, 19620102
        ENDDATE, 20110525
        TOTALSIZE, 195
        STATUS, 0
        */
    final String s = securityNode.getSymbol();
    final String a = Integer.toString(startDate.getMonthValue());
    final String b = Integer.toString(startDate.getDayOfMonth());
    final String c = Integer.toString(startDate.getYear());
    final String d = Integer.toString(endDate.getMonthValue());
    final String e = Integer.toString(endDate.getDayOfMonth());
    final String f = Integer.toString(endDate.getYear());
    //http://ichart.finance.yahoo.com/x?s=IBM&a=00&b=2&c=1962&d=04&e=25&f=2011&g=v&y=0&z=30000
    final StringBuilder url = new StringBuilder("https://ichart.finance.yahoo.com/x?s=").append(s);
    url.append("&a=").append(a).append("&b=").append(b).append("&c=").append(c);
    url.append("&d=").append(d).append("&e=").append(e);
    url.append("&f=").append(f);
    url.append("&g=v&y=0&z=30000");
    URLConnection connection = null;
    try {
        connection = ConnectionFactory.openConnection(url.toString());
        if (connection != null) {
            try (final BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
                String line = in.readLine();
                if (RESPONSE_HEADER.equals(line)) {
                    // prime the first read
                    line = in.readLine();
                    while (line != null) {
                        if (Thread.currentThread().isInterrupted()) {
                            Thread.currentThread().interrupt();
                        }
                        final String[] fields = COMMA_DELIMITER_PATTERN.split(line);
                        if (fields.length == 3) {
                            try {
                                final LocalDate date = LocalDate.of(Integer.parseInt(fields[1].trim().substring(0, 4)), Integer.parseInt(fields[1].trim().substring(4, 6)), Integer.parseInt(fields[1].trim().substring(6, 8)));
                                switch(fields[0]) {
                                    case "DIVIDEND":
                                        final BigDecimal dividend = new BigDecimal(fields[2]);
                                        events.add(new SecurityHistoryEvent(SecurityHistoryEventType.DIVIDEND, date, dividend));
                                        break;
                                    case "SPLIT":
                                        final String[] fraction = fields[2].split(":");
                                        final BigDecimal value = new BigDecimal(fraction[0]).divide(new BigDecimal(fraction[1]), MathContext.DECIMAL32);
                                        events.add(new SecurityHistoryEvent(SecurityHistoryEventType.SPLIT, date, value));
                                        break;
                                    default:
                                        Logger.getLogger(YahooEventParser.class.getName()).log(Level.SEVERE, "Unknown event: " + fields[0]);
                                        break;
                                }
                            } catch (final DateTimeException | NumberFormatException ex) {
                                Logger.getLogger(YahooEventParser.class.getName()).log(Level.INFO, line);
                                Logger.getLogger(YahooEventParser.class.getName()).log(Level.SEVERE, ex.getLocalizedMessage(), ex);
                            }
                        }
                        line = in.readLine();
                    }
                }
            }
        }
    } catch (final NullPointerException | IOException ex) {
        Logger.getLogger(YahooEventParser.class.getName()).log(Level.SEVERE, ex.getLocalizedMessage(), ex);
    } finally {
        if (connection != null) {
            if (connection instanceof HttpURLConnection) {
                ((HttpURLConnection) connection).disconnect();
            }
        }
    }
    return events;
}
Also used : InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) LocalDate(java.time.LocalDate) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) BigDecimal(java.math.BigDecimal) SecurityHistoryEvent(jgnash.engine.SecurityHistoryEvent) DateTimeException(java.time.DateTimeException) HttpURLConnection(java.net.HttpURLConnection) BufferedReader(java.io.BufferedReader) SecurityHistoryNode(jgnash.engine.SecurityHistoryNode) HashSet(java.util.HashSet)

Aggregations

DateTimeException (java.time.DateTimeException)75 Test (org.testng.annotations.Test)58 TemporalField (java.time.temporal.TemporalField)48 HashMap (java.util.HashMap)34 LocalDate (java.time.LocalDate)18 TemporalAccessor (java.time.temporal.TemporalAccessor)13 DateTimeFormatter (java.time.format.DateTimeFormatter)11 DateTimeFormatterBuilder (java.time.format.DateTimeFormatterBuilder)10 MinguoDate (java.time.chrono.MinguoDate)7 ThaiBuddhistDate (java.time.chrono.ThaiBuddhistDate)7 MockFieldValue (test.java.time.temporal.MockFieldValue)7 HijrahDate (java.time.chrono.HijrahDate)6 ChronoLocalDate (java.time.chrono.ChronoLocalDate)5 JapaneseDate (java.time.chrono.JapaneseDate)5 Chronology (java.time.chrono.Chronology)4 IOException (java.io.IOException)3 WeekFields (java.time.temporal.WeekFields)3 AbstractTCKTest (tck.java.time.AbstractTCKTest)3 ResolverStyle (java.time.format.ResolverStyle)2 TemporalQuery (java.time.temporal.TemporalQuery)2