Java Code Examples for java.time.LocalDateTime#minus()
The following examples show how to use
java.time.LocalDateTime#minus() .
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: RangeBasicTests.java From morpheus-core with Apache License 2.0 | 6 votes |
@Test(dataProvider = "localDateTimeRanges") public void testRangeOfLocalDateTimes(LocalDateTime start, LocalDateTime end, Duration step, boolean parallel) { final Range<LocalDateTime> range = Range.of(start, end, step); final Array<LocalDateTime> array = range.toArray(parallel); final boolean ascend = start.isBefore(end); final int expectedLength = (int)Math.ceil(Math.abs((double)ChronoUnit.SECONDS.between(start, end)) / (double)step.getSeconds()); Assert.assertEquals(array.length(), expectedLength); Assert.assertEquals(array.typeCode(), ArrayType.LOCAL_DATETIME); Assert.assertTrue(!array.style().isSparse()); Assert.assertEquals(range.start(), start, "The range start"); Assert.assertEquals(range.end(), end, "The range end"); LocalDateTime expected = null; for (int i=0; i<array.length(); ++i) { final LocalDateTime actual = array.getValue(i); expected = expected == null ? start : ascend ? expected.plus(step) : expected.minus(step); Assert.assertEquals(actual, expected, "Value matches at " + i); Assert.assertTrue(ascend ? actual.compareTo(start) >=0 && actual.isBefore(end) : actual.compareTo(start) <= 0 && actual.isAfter(end), "Value in bounds at " + i); } }
Example 2
Source File: RangeFilterTests.java From morpheus-core with Apache License 2.0 | 6 votes |
@Test(dataProvider = "LocalDateTimeRanges") public void testRangeOfLocalDateTimes(LocalDateTime start, LocalDateTime end, Duration step, boolean parallel) { final boolean ascend = start.isBefore(end); final Range<LocalDateTime> range = Range.of(start, end, step, v -> v.getHour() == 6); final Array<LocalDateTime> array = range.toArray(parallel); final LocalDateTime first = array.first(v -> true).map(ArrayValue::getValue).get(); final LocalDateTime last = array.last(v -> true).map(ArrayValue::getValue).get(); Assert.assertEquals(array.typeCode(), ArrayType.LOCAL_DATETIME); Assert.assertTrue(!array.style().isSparse()); Assert.assertEquals(range.start(), start, "The range start"); Assert.assertEquals(range.end(), end, "The range end"); int index = 0; LocalDateTime value = first; while (ascend ? value.isBefore(last) : value.isAfter(last)) { final LocalDateTime actual = array.getValue(index); Assert.assertEquals(actual, value, "Value matches at " + index); Assert.assertTrue(ascend ? actual.compareTo(start) >= 0 && actual.isBefore(end) : actual.compareTo(start) <= 0 && actual.isAfter(end), "Value in bounds at " + index); value = ascend ? value.plus(step) : value.minus(step); while (value.getHour() == 6) value = ascend ? value.plus(step) : value.minus(step); index++; } }
Example 3
Source File: DateMinusMilliseconds.java From levelup-java-examples with Apache License 2.0 | 6 votes |
@Test public void subtract_milliseconds_from_date_in_java8() { LocalDateTime newYearsDay = LocalDateTime.of(2013, Month.JANUARY, 1, 0, 0, 0, 0); LocalDateTime newYearsEve = newYearsDay.minus(1, ChronoField.MILLI_OF_DAY.getBaseUnit()); java.time.format.DateTimeFormatter formatter = java.time.format.DateTimeFormatter .ofPattern("MM/dd/yyyy HH:mm:ss S"); logger.info(newYearsDay.format(formatter)); logger.info(newYearsEve.format(formatter)); assertTrue(newYearsEve.isBefore(newYearsDay)); }
Example 4
Source File: WindowFunction.java From MeteoInfo with GNU Lesser General Public License v3.0 | 5 votes |
@Override public Object apply(LocalDateTime value) { LocalDateTime ndt = LocalDateTime.now(); ChronoUnit cu = JDateUtil.getChronoUnit(this.period); switch (cu) { case SECONDS: ndt = LocalDateTime.of(value.getYear(), value.getMonthValue(), value.getDayOfMonth(), value.getHour(), value.getMinute(), 0); break; case MINUTES: ndt = LocalDateTime.of(value.getYear(), value.getMonthValue(), value.getDayOfMonth(), value.getHour(), 0, 0); break; case HOURS: ndt = LocalDateTime.of(value.getYear(), value.getMonthValue(), value.getDayOfMonth(), 0, 0, 0); break; case DAYS: ndt = LocalDateTime.of(value.getYear(), value.getMonthValue(), 1, 0, 0, 0); break; case MONTHS: ndt = LocalDateTime.of(value.getYear(), 1, 1, 0, 0, 0); break; case YEARS: int n = ((Period)period).getYears(); ndt = LocalDateTime.of(value.getYear() - n, 1, 1, 0, 0, 0); } while (ndt.isBefore(value)){ ndt = ndt.plus(period); } if (ndt.isAfter(value)) ndt = ndt.minus(period); return ndt; }
Example 5
Source File: LogicTest.java From synthea with Apache License 2.0 | 5 votes |
private void setPatientAge(int age) { LocalDateTime now = LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.of("UTC")); LocalDateTime bday = now.minus(age, ChronoUnit.YEARS); long birthdate = bday.toInstant(ZoneOffset.UTC).toEpochMilli(); person.attributes.put(Person.BIRTHDATE, birthdate); }
Example 6
Source File: TestPointFields.java From lucene-solr with Apache License 2.0 | 5 votes |
public long addTo(long millis) { // Instant.plus() doesn't work with estimated durations (MONTHS and YEARS) LocalDateTime time = LocalDateTime.ofInstant(Instant.ofEpochMilli(millis), ZoneOffset.ofHours(0)); if (negative) { time = time.minus(inCalendarUnits, DateMathParser.CALENDAR_UNITS.get(calendarUnit)); } else { time = time.plus(inCalendarUnits, DateMathParser.CALENDAR_UNITS.get(calendarUnit)); } return time.atZone(ZoneOffset.ofHours(0)).toInstant().toEpochMilli(); }
Example 7
Source File: LocalDateTimeUtils.java From admin-plus with Apache License 2.0 | 4 votes |
public static LocalDateTime minu(LocalDateTime time, long number, TemporalUnit field){ return time.minus(number,field); }
Example 8
Source File: MainPanel.java From java-swing-tips with MIT License | 4 votes |
private MainPanel() { super(new GridLayout(0, 1)); Calendar cal = Calendar.getInstance(); cal.clear(Calendar.MILLISECOND); cal.clear(Calendar.SECOND); cal.clear(Calendar.MINUTE); cal.set(Calendar.HOUR_OF_DAY, 0); Date date = cal.getTime(); cal.add(Calendar.DATE, -2); Date start = cal.getTime(); cal.add(Calendar.DATE, 9); Date end = cal.getTime(); System.out.println(date); System.out.println(start); System.out.println(end); String dateFormat = "yyyy/MM/dd"; JSpinner spinner0 = new JSpinner(new SpinnerDateModel(date, start, end, Calendar.DAY_OF_MONTH)); spinner0.setEditor(new JSpinner.DateEditor(spinner0, dateFormat)); LocalDateTime d = LocalDateTime.now(ZoneId.systemDefault()); LocalDateTime s = d.minus(2, ChronoUnit.DAYS); LocalDateTime e = d.plus(7, ChronoUnit.DAYS); System.out.println(d); System.out.println(s); System.out.println(e); JSpinner spinner1 = new JSpinner(new SpinnerDateModel(toDate(d), toDate(s), toDate(e), Calendar.DAY_OF_MONTH)); spinner1.setEditor(new JSpinner.DateEditor(spinner1, dateFormat)); JSpinner spinner2 = new JSpinner(new SpinnerLocalDateTimeModel(d, s, e, ChronoUnit.DAYS)); spinner2.setEditor(new LocalDateTimeEditor(spinner2, dateFormat)); // JSpinner.DefaultEditor editor = (JSpinner.DefaultEditor) spinner2.getEditor(); // DefaultFormatter formatter = new InternationalFormatter(DateTimeFormatter.ofPattern(dateFormat).toFormat()); // DefaultFormatterFactory factory = new DefaultFormatterFactory(formatter); // JFormattedTextField ftf = editor.getTextField(); // ftf.setHorizontalAlignment(SwingConstants.LEFT); // ftf.setColumns(10); // ftf.setEditable(true); // ftf.setFormatterFactory(factory); add(makeTitledPanel("SpinnerDateModel", spinner0)); add(makeTitledPanel("SpinnerDateModel / toInstant", spinner1)); add(makeTitledPanel("SpinnerLocalDateTimeModel", spinner2)); setBorder(BorderFactory.createEmptyBorder(10, 5, 10, 5)); setPreferredSize(new Dimension(320, 240)); }
Example 9
Source File: RUM.java From raygun4android with MIT License | 4 votes |
/** * Sends a RUM timing event to Raygun. The message is sent on a background thread. * * @param eventType The type of event that occurred. * @param name The name of the event resource such as the activity name or URL of a network call. * @param milliseconds The duration of the event in milliseconds. */ public static void sendRUMTimingEvent(RaygunRUMEventType eventType, String name, long milliseconds) { if (RaygunClient.isRUMEnabled()) { if (sessionId == null) { sessionId = UUID.randomUUID().toString(); sendRUMEvent(RaygunSettings.RUM_EVENT_SESSION_START); } if (eventType == RaygunRUMEventType.ACTIVITY_LOADED) { if (shouldIgnoreView(name)) { return; } } String timestamp; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { LocalDateTime utcDateTime = LocalDateTime.now(ZoneId.of("UTC")); utcDateTime.minus(milliseconds, ChronoUnit.MILLIS); timestamp = utcDateTime.toString(); } else { @SuppressLint("SimpleDateFormat") SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); df.setTimeZone(TimeZone.getTimeZone("UTC")); Calendar c = Calendar.getInstance(); c.add(Calendar.MILLISECOND, -(int) milliseconds); timestamp = df.format(c.getTime()); } RaygunUserInfo user = RaygunClient.getUser() == null ? new RaygunUserInfo(null, null, null, null) : RaygunClient.getUser(); RaygunRUMTimingMessage timingMessage = new RaygunRUMTimingMessage.Builder(eventType == RaygunRUMEventType.ACTIVITY_LOADED ? "p" : "n") .duration(milliseconds) .build(); RaygunRUMData data = new RaygunRUMData.Builder(name) .timing(timingMessage) .build(); RaygunRUMData[] dataArray = new RaygunRUMData[]{data}; String dataStr = new Gson().toJson(dataArray); RaygunRUMDataMessage dataMessage = new RaygunRUMDataMessage.Builder(RaygunSettings.RUM_EVENT_TIMING) .timestamp(timestamp) .sessionId(sessionId) .version(RaygunClient.getVersion()) .os("Android") .osVersion(Build.VERSION.RELEASE) .platform(String.format("%s %s", Build.MANUFACTURER, Build.MODEL)) .user(user) .data(dataStr) .build(); RaygunRUMMessage message = new RaygunRUMMessage(); message.setEventData(new RaygunRUMDataMessage[]{dataMessage}); enqueueWorkForRUMService(RaygunClient.getApiKey(), new Gson().toJson(message)); } else { RaygunLogger.w("RUM is not enabled, please enable to use the sendRUMTimingEvent() function"); } }
Example 10
Source File: LocalDateTimeUtil.java From utils with Apache License 2.0 | 2 votes |
/** * 日期减去一个数,根据field不同减不同值,field参数为ChronoUnit.*. * * @param time * @param number * @param field * @return java.time.LocalDateTime */ public static LocalDateTime minu(LocalDateTime time, long number, TemporalUnit field) { return time.minus(number, field); }