Java Code Examples for java.time.Instant#with()
The following examples show how to use
java.time.Instant#with() .
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: TemporalRoundingTest.java From phoebus with Eclipse Public License 1.0 | 5 votes |
@Test public void testRandomRoundingWithInstant() { final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss (z, 'UTC' Z)"); final ZoneId zone = ZoneId.systemDefault(); final Instant start = Instant.now(); for (ChronoUnit unit : SUPPORTED_UNITS) { final int amount = (int)(Math.random()*60); final Instant rd = start.with(instanceRoundedToNextOrSame(unit, amount)); System.out.println(formatter.format(ZonedDateTime.ofInstant(start, zone )) + " rounded by " + amount + " " + unit + " -> " + formatter.format(ZonedDateTime.ofInstant(rd, zone))); } }
Example 2
Source File: TimestampColumnInstantMapper.java From jadira with Apache License 2.0 | 5 votes |
@Override public Instant fromNonNullValue(Timestamp value) { Instant instant = Instant.ofEpochMilli(value.getTime()); instant = instant.with(ChronoField.NANO_OF_SECOND, value.getNanos()); return instant; }
Example 3
Source File: TimeTicks.java From phoebus with Eclipse Public License 1.0 | 4 votes |
/** {@inheritDoc} */ @Override public void compute(final Instant low, final Instant high, final Graphics2D gc, final int screen_width) { final Duration range = Duration.between(low, high); if (range.isNegative()) throw new Error("Tick range is not ordered, " + low + " > " + high); // Estimate number of labels that fits on screen final int label_width = gc.getFontMetrics().stringWidth("yyyy-MM-dd"); final int num_that_fits = Math.max(1, screen_width/label_width*FILL_PERCENTAGE/100); final Duration distance = range.dividedBy(num_that_fits); // Which of the available formats suits the visible time range? for (int i=0; i<tick_configs.length; ++i) { final TickConfig option = tick_configs[i]; if (distance.compareTo(option.threshold) >= 0) { config = option; detailed_config = i < tick_configs.length-1 ? tick_configs[i+1] : tick_configs[i]; break; } } start = low.with(config.rounding); logger.log(Level.FINE, "Compute time ticks for {0}, {1} pixels: Tick distance {2}", new Object[] { range, screen_width, config.distance }); final List<MajorTick<Instant>> major_ticks = new ArrayList<>(); final List<MinorTick<Instant>> minor_ticks = new ArrayList<>(); long prev_ms = getPrevious(start).toEpochMilli(); final Instant end = getNext(high); for (Instant value = start; value.isBefore(end); value = getNext(value)) { if (value.compareTo(low) >= 0 && value.compareTo(high) <= 0) major_ticks.add(new MajorTick<>(value, format(value))); final long ms = value.toEpochMilli(); for (int i=1; i<config.minor_ticks; ++i) { final long min_ms = prev_ms + ((ms - prev_ms)*i)/config.minor_ticks; final Instant min_val = Instant.ofEpochMilli(min_ms); if (min_val.isAfter(low) && min_val.isBefore(high)) minor_ticks.add(new MinorTick<>(min_val)); } prev_ms = ms; } if (major_ticks.size() < 2) { // If the best-laid plans of mice and men fail // and we end up with just one or no tick, // add the low and high markers. // Use full format for the low marker. final ZonedDateTime local = ZonedDateTime.ofInstant(low, ZoneId.systemDefault()); major_ticks.add(0, new MajorTick<>(low, config.start_formatter.format(local))); major_ticks.add(new MajorTick<>(high, format(high))); } this.major_ticks = major_ticks; this.minor_ticks = minor_ticks; }