List of usage examples for org.joda.time DateTime minusMinutes
public DateTime minusMinutes(int minutes)
From source file:edu.illinois.cs.cogcomp.temporal.normalizer.main.timex2interval.RelativeDate.java
License:Open Source License
/** * This function converts phrase that contains a relative date: more than xxx, etc * @param start anchor time/* w w w. j a va2s . com*/ * @param phrase * @return */ public static TimexChunk Relativerule(DateTime start, String phrase) { int numterm; int flag_ago = 0; int i; int year; int month; int day; DateTime finish; String temp1; String temp2; String temp3; int amount; Interval interval; interval = new Interval(start, start); phrase = phrase.toLowerCase(); phrase = phrase.trim(); TimexChunk tc = new TimexChunk(); if (phrase.equals("recently") || phrase.equals("recent") || phrase.equals("past") || phrase.equals("at time") || phrase.equals("previously")) { tc.addAttribute(TimexNames.type, TimexNames.DATE); tc.addAttribute(TimexNames.value, TimexNames.PAST_REF); return tc; } if (phrase.contains("future")) { tc.addAttribute(TimexNames.type, TimexNames.DATE); tc.addAttribute(TimexNames.value, TimexNames.FUTURE_REF); return tc; } // Handle some special cases if (phrase.endsWith("earlier")) { phrase = phrase.replace("earlier", "ago"); phrase = phrase.trim(); } if (phrase.contains("ago")) { phrase = phrase.replaceAll("ago", ""); phrase = "last " + phrase; flag_ago = 1; } if (phrase.contains("later")) { phrase = phrase.replaceAll("later", ""); phrase = "after " + phrase; } if (phrase.contains("after")) { phrase = phrase.replaceAll("after", ""); phrase = "after " + phrase; } if (phrase.contains("more than")) { phrase = phrase.replaceAll("more than", "after"); } if (phrase.contains("less than")) { phrase = phrase.replaceAll("less than", "in"); } if (phrase.contains("last decade")) { phrase = phrase.replaceAll("last decade", "last ten years"); } if (phrase.contains("next decade")) { phrase = phrase.replaceAll("next decade", "next ten years"); } String patternStr = "\\s*(" + positionTerm + "|" + shiftIndicator + ")\\s*((?:\\d{1,4}|" + number + ")\\s*(?:" + number + ")?)\\s*(" + dateUnit + "|" + timeUnit + ")\\s*\\w*"; Pattern pattern = Pattern.compile(patternStr); Matcher matcher = pattern.matcher(phrase); boolean matchFound = matcher.find(); DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd"); DateTimeFormatter ymFmt = DateTimeFormat.forPattern("yyyy-MM"); DateTimeFormatter yFmt = DateTimeFormat.forPattern("yyyy"); if (matchFound) { for (i = 1; i <= 3; i++) { if (matcher.group(i) == null) { i--; break; } } if (i == 4) { i--; } numterm = i; if (numterm == 3) { temp1 = matcher.group(1); temp2 = matcher.group(2); temp3 = matcher.group(3); amount = Integer.parseInt(converter(temp2)); if (temp1.equals("last") || temp1.equals("past") || temp1.equals("previous") || temp1.equals("since") || temp1.equals("this") || temp1.equals("recent")) { if (flag_ago == 0) { if (temp3.equals("years") || temp3.equals("year")) { finish = start.minusYears(amount); year = finish.getYear(); month = finish.getMonthOfYear(); day = finish.getDayOfMonth(); finish = new DateTime(start.getYear(), start.getMonthOfYear(), start.getDayOfMonth(), 0, 0, 0, 0); start = new DateTime(year, month, day, 0, 0, 0, 0); tc.addAttribute(TimexNames.type, TimexNames.DURATION); tc.addAttribute(TimexNames.value, "P" + amount + "Y"); return tc; } else if (temp3.equals("day") || temp3.equals("days")) { finish = start.minusDays(amount); year = finish.getYear(); month = finish.getMonthOfYear(); day = finish.getDayOfMonth(); finish = new DateTime(start.getYear(), start.getMonthOfYear(), start.getDayOfMonth(), 0, 0, 0, 0); start = new DateTime(year, month, day, 0, 0, 0, 0); tc.addAttribute(TimexNames.type, TimexNames.DURATION); tc.addAttribute(TimexNames.value, "P" + amount + "D"); return tc; } else if (temp3.equals("month") || temp3.equals("months")) { finish = start.minusMonths(amount); year = start.getYear(); month = start.getMonthOfYear(); day = start.getDayOfMonth(); start = new DateTime(finish.getYear(), finish.getMonthOfYear(), finish.getDayOfMonth(), 0, 0, 0, 0); finish = new DateTime(year, month, day, 0, 0, 0, 0); tc.addAttribute(TimexNames.type, TimexNames.DURATION); tc.addAttribute(TimexNames.value, "P" + amount + "M"); return tc; } else if (temp3.equals("week") || temp3.equals("weeks")) { finish = start.minusWeeks(amount); year = start.getYear(); month = start.getMonthOfYear(); day = start.getDayOfMonth(); start = new DateTime(finish.getYear(), finish.getMonthOfYear(), finish.getDayOfMonth(), 0, 0, 0, 0); finish = new DateTime(year, month, day, 0, 0, 0, 0); tc.addAttribute(TimexNames.type, TimexNames.DURATION); tc.addAttribute(TimexNames.value, "P" + amount + "W"); return tc; } else if (temp3.equals("decade") || temp3.equals("decades")) { finish = start.minusYears(amount * 10); year = start.getYear(); month = start.getMonthOfYear(); day = start.getDayOfMonth(); start = new DateTime(finish.getYear(), finish.getMonthOfYear(), finish.getDayOfMonth(), 0, 0, 0, 0); finish = new DateTime(year, month, day, 0, 0, 0, 0); tc.addAttribute(TimexNames.type, TimexNames.DURATION); tc.addAttribute(TimexNames.value, "P" + amount + "DE"); return tc; } else if (temp3.equals("century") || temp3.equals("centuries")) { finish = start.minusYears(amount * 100); year = start.getYear(); month = start.getMonthOfYear(); day = start.getDayOfMonth(); start = new DateTime(finish.getYear(), finish.getMonthOfYear(), finish.getDayOfMonth(), 0, 0, 0, 0); finish = new DateTime(year, month, day, 0, 0, 0, 0); tc.addAttribute(TimexNames.type, TimexNames.DURATION); tc.addAttribute(TimexNames.value, "P" + amount + "CE"); return tc; } else if (temp3.equals("hour") || temp3.equals("hours")) { finish = start.minusHours(amount); tc.addAttribute(TimexNames.type, TimexNames.DURATION); tc.addAttribute(TimexNames.value, "PT" + amount + "H"); return tc; } else if (temp3.equals("minute") || temp3.equals("minutes")) { finish = start.minusMinutes(amount); tc.addAttribute(TimexNames.type, TimexNames.DURATION); tc.addAttribute(TimexNames.value, "PT" + amount + "M"); return tc; } else if (temp3.equals("second") || temp3.equals("seconds")) { finish = start.minusSeconds(amount); tc.addAttribute(TimexNames.type, TimexNames.DURATION); tc.addAttribute(TimexNames.value, "PT" + amount + "S"); return tc; } } else { flag_ago = 0; if (temp3.equals("years") || temp3.equals("year")) { finish = start.minusYears(amount); year = finish.getYear(); month = finish.getMonthOfYear(); day = finish.getDayOfMonth(); start = new DateTime(year, 1, 1, 0, 0, 0, 0); finish = new DateTime(year, 12, 31, 23, 59, 59, 59); tc.addAttribute(TimexNames.type, TimexNames.DATE); tc.addAttribute(TimexNames.value, yFmt.print(finish)); return tc; } else if (temp3.equals("day") || temp3.equals("days")) { finish = start.minusDays(amount); year = finish.getYear(); month = finish.getMonthOfYear(); day = finish.getDayOfMonth(); finish = new DateTime(year, month, day, 23, 59, 59, 59); start = new DateTime(year, month, day, 0, 0, 0, 0); tc.addAttribute(TimexNames.type, TimexNames.DATE); tc.addAttribute(TimexNames.value, fmt.print(finish)); return tc; } else if (temp3.equals("month") || temp3.equals("months")) { finish = start.minusMonths(amount); year = finish.getYear(); month = finish.getMonthOfYear(); day = finish.dayOfMonth().getMaximumValue(); start = new DateTime(year, month, 1, 0, 0, 0, 0); finish = new DateTime(year, month, day, 23, 59, 59, 59); tc.addAttribute(TimexNames.type, TimexNames.DATE); tc.addAttribute(TimexNames.value, ymFmt.print(finish)); return tc; } else if (temp3.equals("week") || temp3.equals("weeks")) { finish = start.minusWeeks(amount); start = finish.minusWeeks(amount + 1); year = start.getYear(); month = start.getMonthOfYear(); day = start.getDayOfMonth(); finish = new DateTime(finish.getYear(), finish.getMonthOfYear(), finish.getDayOfMonth(), 23, 59, 59, 59); start = new DateTime(year, month, day, 0, 0, 0, 0); tc.addAttribute(TimexNames.type, TimexNames.DATE); tc.addAttribute(TimexNames.value, yFmt.print(finish) + "-W" + String.valueOf(finish.getWeekOfWeekyear())); return tc; } else if (temp3.equals("decade") || temp3.equals("decades")) { finish = start.minusYears(amount * 10); year = finish.getYear(); month = finish.getMonthOfYear(); day = finish.getDayOfMonth(); start = new DateTime(year, 1, 1, 0, 0, 0, 0); finish = new DateTime(year, 12, 31, 23, 59, 59, 59); tc.addAttribute(TimexNames.type, TimexNames.DATE); tc.addAttribute(TimexNames.value, String.valueOf(finish.getYear() / 10) + "X"); return tc; } else if (temp3.equals("century") || temp3.equals("centuries")) { finish = start.minusYears(amount * 100); year = finish.getYear(); month = start.getMonthOfYear(); day = start.getDayOfMonth(); start = new DateTime(year, 1, 1, 0, 0, 0, 0); finish = new DateTime(year, 12, 31, 23, 59, 59, 59); tc.addAttribute(TimexNames.type, TimexNames.DATE); tc.addAttribute(TimexNames.value, String.valueOf(finish.getCenturyOfEra())); return tc; } else if (temp3.equals("hour") || temp3.equals("hours")) { finish = start.minusHours(amount); start = new DateTime(finish.getYear(), finish.getMonthOfYear(), finish.getDayOfMonth(), finish.getHourOfDay(), 0, 0, 0); finish = new DateTime(finish.getYear(), finish.getMonthOfYear(), finish.getDayOfMonth(), finish.getHourOfDay(), 59, 59, 59); tc.addAttribute(TimexNames.type, TimexNames.DURATION); tc.addAttribute(TimexNames.value, "PT" + amount + "H"); return tc; } else if (temp3.equals("minute") || temp3.equals("minutes")) { finish = start.minusMinutes(amount); start = new DateTime(finish.getYear(), finish.getMonthOfYear(), finish.getDayOfMonth(), finish.getHourOfDay(), finish.minuteOfHour().get(), 0, 0); finish = new DateTime(finish.getYear(), finish.getMonthOfYear(), finish.getDayOfMonth(), finish.getHourOfDay(), finish.minuteOfHour().get(), 59, 59); tc.addAttribute(TimexNames.type, TimexNames.DURATION); tc.addAttribute(TimexNames.value, "PT" + amount + "M"); return tc; } } } else if (temp1.equals("in") || temp1.equals("upcoming") || temp1.equals("next") || temp1.equals("from") || temp1.equals("following") || temp1.equals("during") || temp1.equals("additional")) { if (temp3.equals("years") || temp3.equals("year")) { finish = start.minusYears((-1) * amount); year = finish.getYear(); month = finish.getMonthOfYear(); day = finish.getDayOfMonth(); finish = new DateTime(start.getYear(), start.getMonthOfYear(), start.getDayOfMonth(), 23, 59, 59, 59); start = new DateTime(year, month, day, 23, 59, 59, 59); interval = new Interval(finish, start); tc.addAttribute(TimexNames.type, TimexNames.DURATION); tc.addAttribute(TimexNames.value, "P" + amount + "Y"); return tc; } else if (temp3.equals("day") || temp3.equals("days")) { finish = start.minusDays((-1) * amount); year = finish.getYear(); month = finish.getMonthOfYear(); day = finish.getDayOfMonth(); finish = new DateTime(start.getYear(), start.getMonthOfYear(), start.getDayOfMonth(), 23, 59, 59, 59); start = new DateTime(year, month, day, 23, 59, 59, 59); tc.addAttribute(TimexNames.type, TimexNames.DURATION); tc.addAttribute(TimexNames.value, "P" + amount + "D"); return tc; } else if (temp3.equals("month") || temp3.equals("months")) { finish = start.minusMonths((-1) * amount); year = start.getYear(); month = start.getMonthOfYear(); day = start.getDayOfMonth(); start = new DateTime(finish.getYear(), finish.getMonthOfYear(), finish.getDayOfMonth(), 23, 59, 59, 59); finish = new DateTime(year, month, day, 23, 59, 59, 59); tc.addAttribute(TimexNames.type, TimexNames.DURATION); tc.addAttribute(TimexNames.value, "P" + amount + "M"); return tc; } else if (temp3.equals("week") || temp3.equals("weeks")) { finish = start.minusWeeks((-1) * amount); year = start.getYear(); month = start.getMonthOfYear(); day = start.getDayOfMonth(); start = new DateTime(finish.getYear(), finish.getMonthOfYear(), finish.getDayOfMonth(), 23, 59, 59, 59); finish = new DateTime(year, month, day, 23, 59, 59, 59); tc.addAttribute(TimexNames.type, TimexNames.DURATION); tc.addAttribute(TimexNames.value, "P" + amount + "W"); return tc; } else if (temp3.equals("decade") || temp3.equals("decades")) { finish = start.minusYears((-1) * amount * 10); year = start.getYear(); month = start.getMonthOfYear(); day = start.getDayOfMonth(); start = new DateTime(finish.getYear(), finish.getMonthOfYear(), finish.getDayOfMonth(), 23, 59, 59, 59); finish = new DateTime(year, month, day, 23, 59, 59, 59); tc.addAttribute(TimexNames.type, TimexNames.DURATION); amount = amount * 10; tc.addAttribute(TimexNames.value, "P" + amount + "Y"); return tc; } else if (temp3.equals("century") || temp3.equals("centuries")) { finish = start.minusYears((-1) * amount * 100); year = start.getYear(); month = start.getMonthOfYear(); day = start.getDayOfMonth(); start = new DateTime(finish.getYear(), finish.getMonthOfYear(), finish.getDayOfMonth(), 23, 59, 59, 59); finish = new DateTime(year, month, day, 23, 59, 59, 59); tc.addAttribute(TimexNames.type, TimexNames.DURATION); amount *= 100; tc.addAttribute(TimexNames.value, "P" + amount + "Y"); return tc; } else if (temp3.equals("hour") || temp3.equals("hours")) { finish = start.minusHours((-1) * amount); tc.addAttribute(TimexNames.type, TimexNames.DURATION); tc.addAttribute(TimexNames.value, "PT" + amount + "H"); return tc; } else if (temp3.equals("minute") || temp3.equals("minutes")) { finish = start.minusMinutes((-1) * amount); tc.addAttribute(TimexNames.type, TimexNames.DURATION); tc.addAttribute(TimexNames.value, "PT" + amount + "M"); return tc; } else if (temp3.equals("second") || temp3.equals("seconds")) { finish = start.minusSeconds((-1) * amount); tc.addAttribute(TimexNames.type, TimexNames.DURATION); tc.addAttribute(TimexNames.value, "PT" + amount + "S"); return tc; } } else if (temp1.equals("before") || temp1.equals("prior to") || temp1.equals("preceding")) { if (temp3.equals("years") || temp3.equals("year")) { finish = start.minusYears(amount); finish = finish.minusDays(1); year = finish.getYear(); month = finish.getMonthOfYear(); day = finish.getDayOfMonth(); start = new DateTime(0000, 1, 1, 0, 0, 0, 0); finish = new DateTime(year, month, day, 23, 59, 59, 59); tc.addAttribute(TimexNames.type, TimexNames.DURATION); tc.addAttribute(TimexNames.value, "P" + amount + "Y"); return tc; } else if (temp3.equals("day") || temp3.equals("days")) { finish = start.minusDays(amount); finish = finish.minusDays(1); year = finish.getYear(); month = finish.getMonthOfYear(); day = finish.getDayOfMonth(); start = new DateTime(0000, 1, 1, 0, 0, 0, 0); finish = new DateTime(year, month, day, 23, 59, 59, 59); tc.addAttribute(TimexNames.type, TimexNames.DURATION); tc.addAttribute(TimexNames.value, "P" + amount + "D"); return tc; } else if (temp3.equals("month") || temp3.equals("months")) { finish = start.minusMonths(amount); finish = finish.minusDays(1); year = finish.getYear(); month = finish.getMonthOfYear(); day = finish.getDayOfMonth(); start = new DateTime(0000, 1, 1, 0, 0, 0, 0); finish = new DateTime(year, month, day, 23, 59, 59, 59); tc.addAttribute(TimexNames.type, TimexNames.DURATION); tc.addAttribute(TimexNames.value, "P" + amount + "M"); return tc; } else if (temp3.equals("week") || temp3.equals("weeks")) { finish = start.minusWeeks(amount); finish = finish.minusDays(1); year = finish.getYear(); month = finish.getMonthOfYear(); day = finish.getDayOfMonth(); start = new DateTime(0000, 1, 1, 0, 0, 0, 0); finish = new DateTime(year, month, day, 23, 59, 59, 59); tc.addAttribute(TimexNames.type, TimexNames.DURATION); tc.addAttribute(TimexNames.value, "P" + amount + "W"); return tc; } else if (temp3.equals("decade") || temp3.equals("decades")) { finish = start.minusYears(amount * 10); finish = finish.minusDays(1); year = finish.getYear(); month = finish.getMonthOfYear(); day = finish.getDayOfMonth(); start = new DateTime(0000, 1, 1, 0, 0, 0, 0); finish = new DateTime(year, month, day, 23, 59, 59, 59); amount *= 10; tc.addAttribute(TimexNames.type, TimexNames.DURATION); tc.addAttribute(TimexNames.value, "P" + amount + "Y"); return tc; } else if (temp3.equals("century") || temp3.equals("centuries")) { finish = start.minusYears(amount * 100); finish = finish.minusDays(1); year = finish.getYear(); month = finish.getMonthOfYear(); day = finish.getDayOfMonth(); start = new DateTime(0000, 1, 1, 0, 0, 0, 0); finish = new DateTime(year, month, day, 23, 59, 59, 59); amount *= 100; tc.addAttribute(TimexNames.type, TimexNames.DURATION); tc.addAttribute(TimexNames.value, "P" + amount + "Y"); return tc; } else if (temp3.equals("hour") || temp3.equals("hours")) { finish = start.minusHours(amount); start = new DateTime(0000, 1, 1, 0, 0, 0, 0); //interval = new Interval(start, finish); tc.addAttribute(TimexNames.type, TimexNames.DURATION); tc.addAttribute(TimexNames.value, "PT" + amount + "H"); return tc; } else if (temp3.equals("minute") || temp3.equals("minutes")) { finish = start.minusMinutes(amount); start = new DateTime(0000, 1, 1, 0, 0, 0, 0); tc.addAttribute(TimexNames.type, TimexNames.DURATION); tc.addAttribute(TimexNames.value, "PT" + amount + "M"); return tc; } else if (temp3.equals("second") || temp3.equals("seconds")) { finish = start.minusSeconds(amount); start = new DateTime(0000, 1, 1, 0, 0, 0, 0); tc.addAttribute(TimexNames.type, TimexNames.DURATION); tc.addAttribute(TimexNames.value, "PT" + amount + "S"); return tc; } } else if (temp1.equals("after") || temp1.equals("over")) { if (temp3.equals("years") || temp3.equals("year")) { finish = start.minusYears((-1) * amount); finish = finish.minusDays(-1); year = finish.getYear(); month = finish.getMonthOfYear(); day = finish.getDayOfMonth(); finish = new DateTime(year, month, day, 0, 0, 0, 0); start = new DateTime(9999, 12, 31, 23, 59, 59, 59); tc.addAttribute(TimexNames.type, TimexNames.DURATION); tc.addAttribute(TimexNames.value, "P" + amount + "Y"); return tc; } else if (temp3.equals("day") || temp3.equals("days")) { finish = start.minusDays((-1) * amount); finish = finish.minusDays(-1); year = finish.getYear(); month = finish.getMonthOfYear(); day = finish.getDayOfMonth(); finish = new DateTime(year, month, day, 0, 0, 0, 0); start = new DateTime(9999, 12, 31, 23, 59, 59, 59); tc.addAttribute(TimexNames.type, TimexNames.DURATION); tc.addAttribute(TimexNames.value, "P" + amount + "D"); return tc; } else if (temp3.equals("month") || temp3.equals("months")) { finish = start.minusMonths((-1) * amount); finish = finish.minusDays(-1); year = finish.getYear(); month = finish.getMonthOfYear(); day = finish.getDayOfMonth(); finish = new DateTime(year, month, day, 0, 0, 0, 0); start = new DateTime(9999, 12, 31, 23, 59, 59, 59); tc.addAttribute(TimexNames.type, TimexNames.DURATION); tc.addAttribute(TimexNames.value, "P" + amount + "M"); return tc; } else if (temp3.equals("week") || temp3.equals("weeks")) { finish = start.minusWeeks((-1) * amount); finish = finish.minusDays(-1); year = finish.getYear(); month = finish.getMonthOfYear(); day = finish.getDayOfMonth(); finish = new DateTime(year, month, day, 0, 0, 0, 0); start = new DateTime(9999, 12, 31, 23, 59, 59, 59); tc.addAttribute(TimexNames.type, TimexNames.DURATION); tc.addAttribute(TimexNames.value, "P" + amount + "W"); return tc; } else if (temp3.equals("decade") || temp3.equals("decades")) { finish = start.minusYears((-1) * amount * 10); finish = finish.minusDays(-1); year = finish.getYear(); month = finish.getMonthOfYear(); day = finish.getDayOfMonth(); finish = new DateTime(year, month, day, 0, 0, 0, 0); start = new DateTime(9999, 12, 31, 23, 59, 59, 59); amount *= 10; tc.addAttribute(TimexNames.type, TimexNames.DURATION); tc.addAttribute(TimexNames.value, "P" + amount + "Y"); return tc; } else if (temp3.equals("century") || temp3.equals("centuries")) { finish = start.minusYears((-1) * amount * 100); finish = finish.minusDays(-1); year = finish.getYear(); month = finish.getMonthOfYear(); day = finish.getDayOfMonth(); finish = new DateTime(year, month, day, 0, 0, 0, 0); start = new DateTime(9999, 12, 31, 23, 59, 59, 59); interval = new Interval(finish, start); amount *= 100; tc.addAttribute(TimexNames.type, TimexNames.DURATION); tc.addAttribute(TimexNames.value, "P" + amount + "Y"); return tc; } else if (temp3.equals("hour") || temp3.equals("hours")) { finish = start.minusHours((-1) * amount); start = new DateTime(9999, 12, 31, 23, 59, 59, 59); interval = new Interval(finish, start); tc.addAttribute(TimexNames.type, TimexNames.DURATION); tc.addAttribute(TimexNames.value, "PT" + amount + "H"); return tc; } else if (temp3.equals("minute") || temp3.equals("minutes")) { finish = start.minusMinutes((-1) * amount); start = new DateTime(9999, 12, 31, 23, 59, 59, 59); interval = new Interval(finish, start); tc.addAttribute(TimexNames.type, TimexNames.DURATION); tc.addAttribute(TimexNames.value, "PT" + amount + "M"); return tc; } else if (temp3.equals("second") || temp3.equals("seconds")) { finish = start.minusSeconds((-1) * amount); start = new DateTime(9999, 12, 31, 23, 59, 59, 59); interval = new Interval(finish, start); tc.addAttribute(TimexNames.type, TimexNames.DURATION); tc.addAttribute(TimexNames.value, "PT" + amount + "S"); return tc; } } } } return null; }
From source file:es.usc.citius.servando.calendula.activities.ConfirmActivity.java
License:Open Source License
public Pair<DateTime, DateTime> getCheckMarginInterval(DateTime intakeTime) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); String checkMarginStr = prefs.getString("check_window_margin", "" + DEFAULT_CHECK_MARGIN); int checkMargin = Integer.parseInt(checkMarginStr); DateTime start = intakeTime.minusMinutes(30); DateTime end = intakeTime.plusHours(checkMargin); return new Pair<>(start, end); }
From source file:eu.hydrologis.jgrass.geonotes.GeonotesHandler.java
License:Open Source License
/** * Fetches a gps coordinate from the database nearest to a supplied time and date. * //from ww w .j a v a2 s. com * @param dateTime the time to search for. * @return the coordinate of the nearest time. * @throws Exception */ public static Coordinate getGpsCoordinateForTimeStamp(DateTime dateTime, int minutesThreshold) throws Exception { DateTime from = dateTime.minusMinutes(minutesThreshold); DateTime to = dateTime.plusMinutes(minutesThreshold); Session session = null; try { session = DatabasePlugin.getDefault().getActiveDatabaseConnection().openSession(); Criteria criteria = session.createCriteria(GpsLogTable.class); String utcTimeStr = "utcTime"; criteria.add(between(utcTimeStr, from, to)); criteria.addOrder(asc(utcTimeStr)); List<GpsLogTable> resultsList = criteria.list(); for (int i = 0; i < resultsList.size() - 1; i++) { GpsLogTable gpsLog1 = resultsList.get(i); GpsLogTable gpsLog2 = resultsList.get(i + 1); DateTime utcTimeBefore = gpsLog1.getUtcTime(); DateTime utcTimeAfter = gpsLog2.getUtcTime(); Interval interval = new Interval(utcTimeBefore, utcTimeAfter); if (interval.contains(dateTime)) { // take the nearest Interval intervalBefore = new Interval(utcTimeBefore, dateTime); Interval intervalAfter = new Interval(dateTime, utcTimeAfter); long beforeMillis = intervalBefore.toDurationMillis(); long afterMillis = intervalAfter.toDurationMillis(); if (beforeMillis < afterMillis) { Coordinate coord = new Coordinate(gpsLog1.getEast(), gpsLog1.getNorth()); return coord; } else { Coordinate coord = new Coordinate(gpsLog2.getEast(), gpsLog2.getNorth()); return coord; } } } } finally { session.close(); } return null; }
From source file:google.registry.tools.server.GenerateZoneFilesAction.java
License:Open Source License
@Override public Map<String, Object> handleJsonRequest(Map<String, ?> json) { @SuppressWarnings("unchecked") ImmutableSet<String> tlds = ImmutableSet.copyOf((List<String>) json.get("tlds")); final DateTime exportTime = DateTime.parse(json.get("exportTime").toString()); // We disallow exporting within the past 2 minutes because there might be outstanding writes. // We can only reliably call loadAtPointInTime at times that are UTC midnight and > // datastoreRetention ago in the past. DateTime now = clock.nowUtc(); if (exportTime.isAfter(now.minusMinutes(2))) { throw new BadRequestException("Invalid export time: must be > 2 minutes ago"); }//from w w w . j a v a2 s. c o m if (exportTime.isBefore(now.minus(datastoreRetention))) { throw new BadRequestException(String.format("Invalid export time: must be < %d days ago", datastoreRetention.getStandardDays())); } if (!exportTime.equals(exportTime.toDateTime(UTC).withTimeAtStartOfDay())) { throw new BadRequestException("Invalid export time: must be midnight UTC"); } String jobId = mrRunner.setJobName("Generate bind file stanzas").setModuleName("tools") .setDefaultReduceShards(tlds.size()).runMapreduce(new GenerateBindFileMapper(tlds, exportTime), new GenerateBindFileReducer(bucket, exportTime, gcsBufferSize), ImmutableList.of(new NullInput<EppResource>(), createEntityInput(DomainResource.class, HostResource.class))); ImmutableList<String> filenames = FluentIterable.from(tlds).transform(new Function<String, String>() { @Override public String apply(String tld) { return String.format(GCS_PATH_FORMAT, bucket, String.format(FILENAME_FORMAT, tld, exportTime)); } }).toList(); return ImmutableMap.<String, Object>of("jobPath", createJobPath(jobId), "filenames", filenames); }
From source file:Implement.DAO.BookingDAOImpl.java
@Override public void insertOfflineEngineBooking(String bookingDate, String tripDate, String tripTime, int noPackage, int packageID, String resourceNote, String customerName, String customerPhone, String customerEmail, String durationType, int duration, int smallestInterval, int providerID) { // construct dynamic sql for used resources DateTimeFormatter formatterDateAndHour = DateTimeFormat.forPattern("MM/dd/yyyy HH:mm:ss"); String travelTimeStr = tripDate; if (!durationType.equals("days")) { travelTimeStr += " " + tripTime + ":00"; } else {//from w w w .j av a 2 s . c o m travelTimeStr += " 00:00:00"; } DateTime travelTime = formatterDateAndHour.parseDateTime(travelTimeStr); DateTime endTime = new DateTime(travelTime); if (!durationType.equals("days")) { // change hours to minutes if (durationType.equals("hours")) { duration *= 60; } travelTime = travelTime.minusMinutes(duration); endTime = endTime.plusMinutes(duration); } else { travelTime = travelTime.minusDays(duration - 1); endTime = endTime.plusDays(duration + 1); } // loop each 5 minutes DateTimeFormatter fmtDate = DateTimeFormat.forPattern("MM/dd/YYYY"); DateTimeFormatter fmtTime = DateTimeFormat.forPattern("HH:mm"); String valueStr = "VALUES "; DateTime eachTime = new DateTime(travelTime); String date = fmtDate.print(eachTime); String time = fmtTime.print(eachTime); valueStr += " (@ResourceIDVar,'" + date + "','" + time + "',@NoUsedResourcesVar, @providerIDVar)"; eachTime = eachTime.plusMinutes(smallestInterval); while (eachTime.isBefore(endTime)) { date = fmtDate.print(eachTime); time = fmtTime.print(eachTime); valueStr += ",(@ResourceIDVar,'" + date + "','" + time + "',@NoUsedResourcesVar, @providerIDVar)"; eachTime = eachTime.plusMinutes(smallestInterval); } String insertStmt = "INSERT INTO UsedResources(ResourceID, TripDate, TripTime, NoUsedResources, ProviderID) "; insertStmt += valueStr; // Build Param Condition For Procedure String paramCondition = "@ResourceIDVar INT, @NoUsedResourcesVar INT, @providerIDVar INT"; simpleJdbcCall = new SimpleJdbcCall(dataSource).withProcedureName("InsertOfflineBooking"); SqlParameterSource in = new MapSqlParameterSource().addValue("bookingDate", bookingDate) .addValue("providerID", providerID).addValue("tripDate", tripDate).addValue("tripTime", tripTime) .addValue("noPackage", noPackage).addValue("packageID", packageID) .addValue("resourceNote", resourceNote).addValue("customerName", customerName) .addValue("customerPhone", customerPhone).addValue("customerEmail", customerEmail) .addValue("InsertStatement", insertStmt).addValue("ParmDefinition", paramCondition); simpleJdbcCall.execute(in); }
From source file:it.cineca.pst.huborcid.service.OrcidService.java
License:Open Source License
void notify(Integer num) { Pageable topXXX = new PageRequest(0, num); DateTime dt = new DateTime(); dt.minusMinutes(15); List<RelPersonApplication> relPersonApp = relPersonApplicationRepository .findAllByLastIsTrueAndNotifiedIsFalse(dt, topXXX); System.out.println("Notify size: " + relPersonApp.size()); for (int i = 0; i < relPersonApp.size(); i++) { try {//from w w w.j a va2 s .com System.out.println(i + " Notify: notify " + relPersonApp.get(i).getId()); Future<String> result = sendNotify(relPersonApp.get(i)); result.get(); System.out.println(i + " Notify: notified " + relPersonApp.get(i).getId()); } catch (Exception e) { } } }
From source file:kr.debop4j.timeperiod.tools.Times.java
License:Apache License
/** ? (Minute) . */ public static List<ITimePeriod> foreachMinutes(ITimePeriod period) { shouldNotBeNull(period, "period"); if (isTraceEnabled) log.trace("[{}]? (Minute) ...", period); List<ITimePeriod> minutes = Lists.newArrayList(); if (period.isAnytime()) return minutes; assertHasPeriod(period);/*from w w w.jav a2s . c om*/ if (Times.isSameMinute(period.getStart(), period.getEnd())) { minutes.add(new TimeRange(period)); return minutes; } minutes.add(new TimeRange(period.getStart(), Times.endTimeOfMinute(period.getStart()))); DateTime endMinute = period.getEnd(); DateTime current = Times.trimToMinute(period.getStart(), period.getStart().getMinuteOfHour() + 1); ITimeCalendar calendar = TimeCalendar.getDefault(); DateTime maxMinute = endMinute.minusMinutes(1); while (current.compareTo(maxMinute) <= 0) { minutes.add(Times.getMinuteRange(current, calendar)); current = current.plusMinutes(1); } if (endMinute.minusMinutes(endMinute.getMinuteOfHour()).getMillisOfDay() > 0) { minutes.add(new TimeRange(Times.startTimeOfMinute(endMinute), endMinute)); } return minutes; }
From source file:net.link.util.saml.Saml1Utils.java
License:Open Source License
/** * Checks whether the assertion is well formed and validates against some given data. * * @param assertion the assertion to validate * @param now Check whether the assertion is valid at this instant. * @param expectedAudience expected audience matching the optional audience restriction * * @throws ValidationFailedException validation failed. *///w ww . j av a 2 s. co m public static void validateAssertion(@NotNull Assertion assertion, @Nullable ReadableDateTime now, @Nullable String expectedAudience) throws ValidationFailedException { Conditions conditions = assertion.getConditions(); DateTime notBefore = conditions.getNotBefore(); DateTime notOnOrAfter = conditions.getNotOnOrAfter(); logger.dbg("now: %s", now); logger.dbg("notBefore: %s", notBefore); logger.dbg("notOnOrAfter : %s", notOnOrAfter); if (now != null) { if (now.isBefore(notBefore)) { // time skew DateTime nowDt = now.toDateTime(); if (nowDt.plusMinutes(5).isBefore(notBefore) || nowDt.minusMinutes(5).isAfter(notOnOrAfter)) throw new ValidationFailedException("SAML2 assertion validation audience=" + expectedAudience + " : invalid SAML message timeframe"); } else if (now.isBefore(notBefore) || now.isAfter(notOnOrAfter)) throw new ValidationFailedException("SAML2 assertion validation audience=" + expectedAudience + " : invalid SAML message timeframe"); } if (assertion.getAuthenticationStatements().isEmpty()) throw new ValidationFailedException( "SAML2 assertion validation audience=" + expectedAudience + " : missing AuthnStatement"); AuthenticationStatement authnStatement = assertion.getAuthenticationStatements().get(0); Subject subject = authnStatement.getSubject(); if (null == subject) throw new ValidationFailedException( "SAML2 assertion validation audience=" + expectedAudience + " : missing Assertion Subject"); SubjectConfirmation subjectConfirmation = subject.getSubjectConfirmation(); if (null == authnStatement.getAuthenticationMethod()) throw new ValidationFailedException( "SAML2 assertion validation audience=" + expectedAudience + " : missing AuthenticationMethod"); if (expectedAudience != null) // Check whether the audience of the response corresponds to the original audience restriction validateAudienceRestriction(conditions, expectedAudience); }
From source file:net.link.util.saml.Saml2Utils.java
License:Open Source License
/** * Checks whether the assertion is well formed and validates against some given data. * * @param assertion the assertion to validate * @param now Check whether the assertion is valid at this instant. * @param expectedAudience expected audience matching the optional audience restriction * * @throws ValidationFailedException validation failed. *//*from www . j ava 2 s. c o m*/ public static void validateAssertion(@NotNull Assertion assertion, @Nullable ReadableDateTime now, @Nullable String expectedAudience) throws ValidationFailedException { Conditions conditions = assertion.getConditions(); DateTime notBefore = conditions.getNotBefore(); DateTime notOnOrAfter = conditions.getNotOnOrAfter(); logger.dbg("now: %s", now); logger.dbg("notBefore: %s", notBefore); logger.dbg("notOnOrAfter : %s", notOnOrAfter); if (now != null) { if (now.isBefore(notBefore)) { // time skew DateTime nowDt = now.toDateTime(); if (nowDt.plusMinutes(5).isBefore(notBefore) || nowDt.minusMinutes(5).isAfter(notOnOrAfter)) throw new ValidationFailedException("SAML2 assertion validation audience=" + expectedAudience + " : invalid SAML message timeframe"); } else if (now.isBefore(notBefore) || now.isAfter(notOnOrAfter)) throw new ValidationFailedException("SAML2 assertion validation audience=" + expectedAudience + " : invalid SAML message timeframe"); } Subject subject = assertion.getSubject(); if (null == subject) throw new ValidationFailedException( "SAML2 assertion validation audience=" + expectedAudience + " : missing Assertion Subject"); if (subject.getSubjectConfirmations().isEmpty()) throw new ValidationFailedException( "SAML2 assertion validation audience=" + expectedAudience + " : missing SubjectConfirmation"); SubjectConfirmation subjectConfirmation = subject.getSubjectConfirmations().get(0); SubjectConfirmationData subjectConfirmationData = subjectConfirmation.getSubjectConfirmationData(); if (!subjectConfirmationData.getUnknownXMLObjects(KeyInfo.DEFAULT_ELEMENT_NAME).isEmpty()) { // meaning a PublicKey is attached if (subjectConfirmationData.getUnknownXMLObjects(KeyInfo.DEFAULT_ELEMENT_NAME).size() != 1) throw new ValidationFailedException("SAML2 assertion validation audience=" + expectedAudience + " : more then 1 KeyInfo element in SubjectConfirmationData"); } if (assertion.getAuthnStatements().isEmpty()) throw new ValidationFailedException( "SAML2 assertion validation audience=" + expectedAudience + " : missing AuthnStatement"); AuthnStatement authnStatement = assertion.getAuthnStatements().get(0); if (null == authnStatement.getAuthnContext()) throw new ValidationFailedException( "SAML2 assertion validation audience=" + expectedAudience + " : missing AuthnContext"); if (null == authnStatement.getAuthnContext().getAuthnContextClassRef()) throw new ValidationFailedException( "SAML2 assertion validation audience=" + expectedAudience + " : missing AuthnContextClassRef"); if (expectedAudience != null) // Check whether the audience of the response corresponds to the original audience restriction validateAudienceRestriction(conditions, expectedAudience); }
From source file:net.sourceforge.fenixedu.presentationTier.Action.ICalendarSyncPoint.java
License:Open Source License
private Calendar getClassCalendar(User user, DateTime validity, HttpServletRequest request) { List<EventBean> allEvents = getClasses(user); String url = CoreConfiguration.getConfiguration().applicationUrl() + "/login"; EventBean event = new EventBean("Renovar a chave do calendario.", validity.minusMinutes(30), validity.plusMinutes(30), false, null, url, "A sua chave de sincronizao do calendario vai expirar. Diriga-se ao Fnix para gerar nova chave"); allEvents.add(event);// w w w .j a v a2 s . com return CalendarFactory.createCalendar(allEvents); }