List of usage examples for org.joda.time DateTime plusMillis
public DateTime plusMillis(int millis)
From source file:jp.furplag.util.time.JodaPrettifier.java
License:Apache License
/** * Return the prettified String if the period includes specified moment. * * <pre>// w ww. j a va2 s.c o m * prettify(DateTime.now().minusHours(1), null, null, null, null) = "one hour ago." prettify(DateTime.now(), DateTime.now().plusYears(1), null, null, null) = "one year ago." prettify(DateTime.now().minusHours(1), null, null, null, new Period().withDays(1)) = "one hour ago." prettify(DateTime.now().minusHours(1), null, null, null, new Period().withMinites(10)) = * DateTime.now().withZone(DateTimeZone.UTC).minusHours(1).toString(DateTimeFormat.forStyle("-M")) * * <pre> * * @param then the datetime object, null means current date-time. * @param reference the moment of a starting point ( {@link org.joda.time.ReadableInstant} and {@link Long} specifiable ). Use {@code DateTime.now()} as a start point if {@code reference} is null. * @param locale the language for Localization ( {@code String} and {@code Locale} specifiable ). Use ROOT if {@code locale} is null. * @param limit if the moment is in the specified period, return prettified String ( {@code Period} and {@code Interval} specifiable ). Prettify all, if null. * @return the prettified String if the period includes specified moment. In other situation, return stringified date-time. */ public static String prettify(final Object then, final Object reference, final Locale locale, final DateTimeZone zone, final Object limit) { DateTime temporary = DateTimeUtils.toDT(then, zone, true); if (temporary == null) return StringUtils.EMPTY; DateTime ref = DateTimeUtils.toDT(reference, temporary.getZone(), true); if (ref == null) return doPrettify(temporary, null, locale); if (ref.isEqual(temporary)) ref = ref.plusMillis(1); if (limit == null) return doPrettify(temporary, ref, locale); Interval limitter = null; if (Interval.class.equals(limit)) limitter = (Interval) limit; if (limit instanceof Period) { limitter = new Interval(ref.minus((Period) limit), ref.plusMillis(1).plus((Period) limit)); } if (limit instanceof BaseSingleFieldPeriod) { limitter = new Interval(ref.minus(new Period(limit)), ref.plusMillis(1).plus(new Period(limit))); } if (ObjectUtils.isAny(ClassUtils.primitiveToWrapper(limit.getClass()), Double.class, Float.class)) { limitter = new Interval(toDT(toAJD(ref) - NumberUtils.valueOf(limit, double.class), ref), toDT(toAJD(ref) + NumberUtils.valueOf(limit, double.class), ref)); } else if (BigDecimal.class.equals(limit.getClass())) { if (NumberUtils.compareTo((BigDecimal) limit, NumberUtils.down(limit)) == 0) { limitter = new Interval(ref.minusMillis(NumberUtils.valueOf(limit, int.class)), ref.plusMillis(NumberUtils.valueOf(limit, int.class) + 1)); } else { limitter = new Interval(toDT(toAJD(ref) - NumberUtils.valueOf(limit, double.class), ref), toDT(toAJD(ref) + NumberUtils.valueOf(limit, double.class), ref)); } } else if (Number.class.isAssignableFrom(ClassUtils.primitiveToWrapper(limit.getClass()))) { limitter = new Interval(ref.minusMillis(NumberUtils.valueOf(limit, int.class)), ref.plusMillis(NumberUtils.valueOf(limit, int.class) + 1)); } if (DateTime.class.equals(limit.getClass())) { limitter = new Interval(ref.minus(((DateTime) limit).getMillis()), ref.plus(((DateTime) limit).getMillis() + 1L)); } if (Boolean.class.equals(limit.getClass())) { limitter = new Interval(temporary.minusMillis(1), ((Boolean) limit) ? temporary.plusMillis(1) : temporary.minusMillis(1)); } if (limitter == null) return doPrettify(temporary, ref, locale); if (limitter.contains(temporary)) return doPrettify(temporary, ref, locale); return toDT(temporary, GJChronology.getInstance(temporary.getZone())) .toString(DateTimeFormat.forStyle(isToday(temporary, temporary.getZone()) ? "-M" : "MS") .withLocale(locale == null ? Locale.ROOT : locale)); }
From source file:jp.furplag.util.time.JodaPrettifier.java
License:Apache License
/** * substitute for {@link org.ocpsoft.prettytime.PrettyTime#format(Date)} (Does not format Decade, in Japanese situation.) . * * @param then the moment, must not be null. * @param reference the moment of starting point. * @param locale the language for localization. * @return the prettified string./*from w w w . j a v a2 s . c om*/ */ private static String doPrettify(final DateTime then, final DateTime reference, final Locale locale) { if (then == null) throw new IllegalArgumentException("arguments[0]: then is null."); DateTime ref = reference == null ? then.plusMillis(1) : reference; PrettyTime prettyTime = new PrettyTime(ref.toDate()); if (locale != null && Locale.JAPANESE.getLanguage().equals(locale.getLanguage())) prettyTime.removeUnit(Decade.class); return prettyTime.setLocale(locale == null ? Locale.ROOT : locale).format(then.toDate()); }
From source file:li.klass.fhem.activities.graph.ChartData.java
License:Open Source License
private List<GraphEntry> handleShowDiscreteValues(List<GraphEntry> data) { if (!isDiscreteChart()) { return data; }/*from w w w . ja v a 2 s . c om*/ float previousValue = -1; List<GraphEntry> newData = newArrayList(); for (GraphEntry entry : data) { DateTime date = entry.getDate(); float value = entry.getValue(); if (previousValue == -1) { previousValue = value; } newData.add(new GraphEntry(date.minusMillis(1), previousValue)); newData.add(new GraphEntry(date, value)); newData.add(new GraphEntry(date.plusMillis(1), value)); previousValue = value; } return newData; }
From source file:li.klass.fhem.activities.graph.ChartingActivity.java
License:Open Source License
private void handleDiscreteValues(Map<GPlotSeries, List<GraphEntry>> graphData) { for (Map.Entry<GPlotSeries, List<GraphEntry>> entry : graphData.entrySet()) { if (!isDiscreteSeries(entry.getKey())) { continue; }/* w w w . jav a 2 s . co m*/ float previousValue = -1; List<GraphEntry> newData = newArrayList(); List<GraphEntry> values = entry.getValue(); for (GraphEntry graphEntry : values) { DateTime date = graphEntry.getDate(); float value = graphEntry.getValue(); if (previousValue == -1) { previousValue = value; } newData.add(new GraphEntry(date.minusMillis(1), previousValue)); newData.add(new GraphEntry(date, value)); newData.add(new GraphEntry(date.plusMillis(1), value)); previousValue = value; } values.clear(); values.addAll(newData); } }
From source file:loci.formats.in.BaseZeissReader.java
License:Open Source License
/** * Parse timestamp from string. Note this may be ZVI-specific * due to the use of locale-specific date formats in the TIFF XML. * @param s//from ww w . j a va 2s. c om * @return a timestamp */ private long parseTimestamp(String s) { try { double dstamp = Double.parseDouble(s); // Time in days since the ZVI epoch DateTime epoch = new DateTime(1900, 1, 1, 0, 0, DateTimeZone.UTC); // 1900-01-01 00:00:00 long millisInDay = 24L * 60L * 60L * 1000L; int days = (int) Math.floor(dstamp); int millis = (int) ((dstamp - days) * millisInDay + 0.5); days -= 1; // We start on day 1 if (days > 60) { // Date prior to 1900-03-01 days -= 1; // 1900-02-29 is considered a valid date by Excel; correct for this. } DateTime dtstamp = epoch.plusDays(days); dtstamp = dtstamp.plusMillis(millis); return dtstamp.getMillis(); } catch (NumberFormatException e) { // Not a Double; one file (BY4741NQ2.zvi) contains a string in // place of the float64 defined in the spec, so try parsing // using US date format. May be a historical AxioVision bug. String us_format = "MM/dd/yyyy hh:mm:ss aa"; return DateTools.getTime(s, us_format, null); } }
From source file:niche.newres.timedevents2owl.randomizer.TimedEvents2OWLRandomizer.java
public static DateTime plusRandomMilliSeconds(DateTime dateTime, int minRange, int maxRange) { int randomMilliSeconds = TimedEvents2OWLRandomizer.randInt(minRange, maxRange); return dateTime.plusMillis(randomMilliSeconds); }
From source file:org.apache.cxf.ws.security.sts.provider.token.Saml1TokenProvider.java
License:Apache License
private org.opensaml.saml1.core.Assertion createAuthnAssertionSAML1(org.opensaml.saml1.core.Subject subject) { org.opensaml.saml1.core.AuthenticationStatement authnStatement = (new org.opensaml.saml1.core.impl.AuthenticationStatementBuilder()) .buildObject();//w w w . j a v a2 s . co m authnStatement.setSubject(subject); // authnStatement.setAuthenticationMethod(strAuthMethod); DateTime now = new DateTime(); authnStatement.setAuthenticationInstant(now); org.opensaml.saml1.core.Conditions conditions = (new org.opensaml.saml1.core.impl.ConditionsBuilder()) .buildObject(); conditions.setNotBefore(now.minusMillis(3600000)); conditions.setNotOnOrAfter(now.plusMillis(3600000)); String issuerURL = "http://www.sopera.de/SAML1"; org.opensaml.saml1.core.Assertion assertion = (new org.opensaml.saml1.core.impl.AssertionBuilder()) .buildObject(); try { SecureRandomIdentifierGenerator generator = new SecureRandomIdentifierGenerator(); assertion.setID(generator.generateIdentifier()); } catch (NoSuchAlgorithmException e) { LOG.error(e); } assertion.setIssuer(issuerURL); assertion.setIssueInstant(now); assertion.setVersion(SAMLVersion.VERSION_11); assertion.getAuthenticationStatements().add(authnStatement); // assertion.getAttributeStatements().add(attrStatement); assertion.setConditions(conditions); return assertion; }
From source file:org.apache.cxf.ws.security.sts.provider.token.Saml2TokenProvider.java
License:Apache License
private Assertion createAssertion(Subject subject) { Assertion assertion = (new AssertionBuilder()).buildObject(); try {/*from www .jav a 2s. c om*/ SecureRandomIdentifierGenerator generator = new SecureRandomIdentifierGenerator(); assertion.setID(generator.generateIdentifier()); } catch (NoSuchAlgorithmException e) { LOG.error(e); } DateTime now = new DateTime(); assertion.setIssueInstant(now); String issuerURL = "http://www.sopera.de/SAML2"; if (issuerURL != null) { Issuer issuer = (new IssuerBuilder()).buildObject(); issuer.setValue(issuerURL); assertion.setIssuer(issuer); } assertion.setSubject(subject); Conditions conditions = (new ConditionsBuilder()).buildObject(); conditions.setNotBefore(now.minusMillis(3600000)); conditions.setNotOnOrAfter(now.plusMillis(3600000)); assertion.setConditions(conditions); return assertion; }
From source file:org.apache.pig.pen.AugmentBaseDataVisitor.java
License:Apache License
Object GetLargerValue(Object v) { byte type = DataType.findType(v); if (type == DataType.BAG || type == DataType.TUPLE || type == DataType.MAP) return null; switch (type) { case DataType.CHARARRAY: return (String) v + "0"; case DataType.BYTEARRAY: String str = ((DataByteArray) v).toString(); str = str + "0"; return new DataByteArray(str); case DataType.INTEGER: return Integer.valueOf((Integer) v + 1); case DataType.LONG: return Long.valueOf((Long) v + 1); case DataType.FLOAT: return Float.valueOf((Float) v + 1); case DataType.DOUBLE: return Double.valueOf((Double) v + 1); case DataType.BIGINTEGER: return ((BigInteger) v).add(BigInteger.ONE); case DataType.BIGDECIMAL: return ((BigDecimal) v).add(BigDecimal.ONE); case DataType.DATETIME: DateTime dt = (DateTime) v; if (dt.getMillisOfSecond() != 0) { return dt.plusMillis(1); } else if (dt.getSecondOfMinute() != 0) { return dt.plusSeconds(1); } else if (dt.getMinuteOfHour() != 0) { return dt.plusMinutes(1); } else if (dt.getHourOfDay() != 0) { return dt.plusHours(1); } else {// w w w.j av a 2s . c o m return dt.plusDays(1); } default: return null; } }
From source file:org.apache.storm.st.tests.window.WindowVerifier.java
License:Apache License
/** * Run the topology and verify that the number and contents of time based windows is as expected * once the spout and bolt have emitted sufficient tuples. * The spout and bolt are required to log exactly one log line per emit/window using {@link StringDecorator} *///from w w w.j a va2 s.co m public void runAndVerifyTime(int windowSec, int slideSec, TestableTopology testable, TopoWrap topo) throws IOException, TException, java.net.MalformedURLException { topo.submitSuccessfully(); final int minSpoutEmits = 100; final int minBoltEmits = 5; String boltName = testable.getBoltName(); String spoutName = testable.getSpoutName(); //Waiting for spout tuples isn't strictly necessary since we also wait for bolt emits, but do it anyway //Allow two minutes for topology startup, then wait for at most the time it should take to produce 10 windows topo.assertProgress(minSpoutEmits, testable.getSpoutExecutors(), spoutName, 180 + 10 * slideSec); topo.assertProgress(minBoltEmits, testable.getBoltExecutors(), boltName, 180 + 10 * slideSec); final List<TimeData> allSpoutLogLines = topo.getDeserializedDecoratedLogLines(spoutName, TimeData::fromJson); final List<TimeDataWindow> allBoltLogLines = topo.getDeserializedDecoratedLogLines(boltName, TimeDataWindow::fromJson); Assert.assertTrue(allBoltLogLines.size() >= minBoltEmits, "Expecting min " + minBoltEmits + " bolt emits, found: " + allBoltLogLines.size() + " \n\t" + allBoltLogLines); final DateTime firstWindowEndTime = TimeUtil .ceil(new DateTime(allSpoutLogLines.get(0).getDate()).withZone(DateTimeZone.UTC), slideSec); final int numberOfWindows = allBoltLogLines.size(); /* * Windows should be aligned to the slide size, starting at firstWindowEndTime - windowSec. * Because all windows are aligned to the slide size, we can partition the spout emitted timestamps by which window they should fall in. * This checks that the partitioned spout emits fall in the expected windows, based on the logs from the spout and bolt. */ for (int i = 0; i < numberOfWindows; ++i) { final DateTime windowEnd = firstWindowEndTime.plusSeconds(i * slideSec); final DateTime windowStart = windowEnd.minusSeconds(windowSec); LOG.info("Comparing window: " + windowStart + " to " + windowEnd + " iter " + (i + 1) + "/" + numberOfWindows); final List<TimeData> expectedSpoutEmitsInWindow = allSpoutLogLines.stream().filter(spoutLog -> { DateTime spoutLogTime = new DateTime(spoutLog.getDate()); //The window boundaries are )windowStart, windowEnd) return spoutLogTime.isAfter(windowStart) && spoutLogTime.isBefore(windowEnd.plusMillis(1)); }).collect(Collectors.toList()); TimeDataWindow expectedWindow = new TimeDataWindow(expectedSpoutEmitsInWindow); final TimeDataWindow actualWindow = allBoltLogLines.get(i); LOG.info("Actual window: " + actualWindow.getDescription()); LOG.info("Expected window: " + expectedWindow.getDescription()); for (TimeData oneLog : expectedWindow.getTimeData()) { Assertions.assertTrue(actualWindow.getTimeData().contains(oneLog), () -> String.format("Missing: '%s' \n\tActual: '%s' \n\tComputed window: '%s'", oneLog, actualWindow, expectedWindow)); } for (TimeData oneLog : actualWindow.getTimeData()) { Assertions.assertTrue(expectedWindow.getTimeData().contains(oneLog), () -> String.format("Extra: '%s' \n\tActual: '%s' \n\tComputed window: '%s'", oneLog, actualWindow, expectedWindow)); } } }