Java Code Examples for java.time.ZonedDateTime#minusHours()

The following examples show how to use java.time.ZonedDateTime#minusHours() . 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: DateTimeWithinPeriodTest.java    From rya with Apache License 2.0 6 votes vote down vote up
@Test
public void testHours() throws DatatypeConfigurationException, ValueExprEvaluationException {
    DatatypeFactory dtf = DatatypeFactory.newInstance();

    ZonedDateTime zTime = testThisTimeDate;
    String time = zTime.format(DateTimeFormatter.ISO_INSTANT);

    ZonedDateTime zTime1 = zTime.minusHours(1);
    String time1 = zTime1.format(DateTimeFormatter.ISO_INSTANT);

    Literal now = VF.createLiteral(dtf.newXMLGregorianCalendar(time));
    Literal nowMinusOne = VF.createLiteral(dtf.newXMLGregorianCalendar(time1));

    DateTimeWithinPeriod func = new DateTimeWithinPeriod();

    assertEquals(TRUE, func.evaluate(VF, now, now,VF.createLiteral(1),OWLTime.HOURS_URI));
    assertEquals(FALSE, func.evaluate(VF, now, nowMinusOne,VF.createLiteral(1),OWLTime.HOURS_URI));
    assertEquals(TRUE, func.evaluate(VF, now, nowMinusOne,VF.createLiteral(2),OWLTime.HOURS_URI));
}
 
Example 2
Source File: HotTest.java    From expper with GNU General Public License v3.0 5 votes vote down vote up
public static void initialize(long size) {
    for (long i = 0; i < size; ++i) {
        PostMeta post = new PostMeta();
        post.id = i;
        post.ups = (int) (Math.random() * 10000);
        post.downs = (int) (Math.random() * 10000);
        ZonedDateTime date = ZonedDateTime.now();
        date = date.minusHours((int) (Math.random() * 24 * 30 * 6));
        date = date.minusSeconds((int) (Math.random() * 3600));
        post.createdAt = date;
        post.hot = (int) (Math.random() * 2000);

        posts.add(post);
    }
}
 
Example 3
Source File: TimeIterator.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
/**
 * Decrease the given time by {@code units}, which must be positive, of {@code granularity}
 */
public static ZonedDateTime dec(ZonedDateTime time, Granularity granularity, long units) {
  switch (granularity) {
    case MINUTE:
      return time.minusMinutes(units);
    case HOUR:
      return time.minusHours(units);
    case DAY:
      return time.minusDays(units);
    case MONTH:
      return time.minusMonths(units);
  }
  throw new RuntimeException("Unsupported granularity: " + granularity);
}
 
Example 4
Source File: ExecutionTimeQuartzIntegrationTest.java    From cron-utils with Apache License 2.0 5 votes vote down vote up
/**
 * Test for issue #18.
 */
@Test
public void testHourlyIntervalTimeFromLastExecution() {
    final ZonedDateTime now = ZonedDateTime.now();
    final ZonedDateTime previousHour = now.minusHours(1);
    final String quartzCronExpression = String.format("0 0 %s * * ?", previousHour.getHour());
    final ExecutionTime executionTime = ExecutionTime.forCron(parser.parse(quartzCronExpression));
    final Optional<Duration> duration = executionTime.timeFromLastExecution(now);
    if (duration.isPresent()) {
        assertTrue(duration.get().toMinutes() <= 120);
    } else {
        fail(DURATION_NOT_PRESENT_ERROR);
    }
}
 
Example 5
Source File: InsightsUtils.java    From Insights with Apache License 2.0 4 votes vote down vote up
public static Long subtractTimeInHours(long inputTime, long hours) {
	ZonedDateTime fromInput = ZonedDateTime.ofInstant(Instant.ofEpochSecond(inputTime), zoneId);
	fromInput = fromInput.minusHours(hours);
	return fromInput.toEpochSecond();
}
 
Example 6
Source File: ConsolePane.java    From xframium-java with GNU General Public License v3.0 4 votes vote down vote up
public ConsolePane (Screen screen, ISite server, PluginsStage pluginsStage)
{
  this.screen = screen;
  this.telnetState = screen.getTelnetState ();
  this.server = server;

  this.fontManager = screen.getFontManager ();
  pluginsStage.setConsolePane (this);

  screen.setConsolePane (this);
  screen.getScreenCursor ().addFieldChangeListener (this);
  screen.getScreenCursor ().addCursorMoveListener (this);

  setMargin (screen, new Insets (MARGIN, MARGIN, 0, MARGIN));

  menuBar.getMenus ().addAll (getCommandsMenu (), fontManager.getFontMenu ());

  // allow null for replay testing
  if (server == null || server.getPlugins ())
    menuBar.getMenus ().add (pluginsStage.getMenu (server));

  setTop (menuBar);
  setCenter (screen);
  setBottom (statusPane = getStatusBar ());
  menuBar.setUseSystemMenuBar (SYSTEM_MENUBAR);

  setHistoryBar ();

  if (server != null)
  {
    Optional<SiteParameters> sp = parameters.getSiteParameters (server.getName ());
    if (sp.isPresent ())
    {
      String offset = sp.get ().getParameter ("offset");
      if (!offset.isEmpty () && offset.length () > 4)
      {
        char direction = offset.charAt (3);
        int value = Integer.parseInt (offset.substring (4));
        ZonedDateTime now = ZonedDateTime.now (ZoneOffset.UTC);
        if (direction == '+')
          now = now.plusHours (value);
        else
          now = now.minusHours (value);
    
      }
    }
  }

  screen.requestFocus ();
}
 
Example 7
Source File: TvMazeUtils.java    From drftpd with GNU General Public License v2.0 4 votes vote down vote up
private static String calculateAge(ZonedDateTime epDate) {
    // Get now
    ZonedDateTime now = ZonedDateTime.now();

    // Get the (positive) time between now and epData
    ZonedDateTime t1 = now;
    ZonedDateTime t2 = epDate;
    if (epDate.isBefore(now)) {
        t1 = epDate;
        t2 = now;
    }

    String age = "";
    long years = ChronoUnit.YEARS.between(t1, t2);
    if (years > 0) {
        age += years+"y";
        t2 = t2.minusYears(years);
    }

    long months = ChronoUnit.MONTHS.between(t1, t2);
    if (months > 0) {
        age += months+"m";
        t2 = t2.minusMonths(months);
    }

    long weeks = ChronoUnit.WEEKS.between(t1, t2);
    if (weeks > 0) {
        age += weeks+"w";
        t2 = t2.minusWeeks(weeks);
    }

    long days = ChronoUnit.DAYS.between(t1, t2);
    if (days > 0) {
        age += days+"d";
        t2 = t2.minusDays(days);
    }
    boolean spaceadded = false;

    long hours = ChronoUnit.HOURS.between(t1, t2);
    if (hours > 0) {
        age += " "+hours+"h";
        spaceadded = true;
        t2 = t2.minusHours(hours);
    }

    long minutes = ChronoUnit.MINUTES.between(t1, t2);
    if (minutes > 0) {
        if (!spaceadded) { age += " ";  }
        age += minutes+"m";
    }
    if (age.length() == 0) { age = "0"; }

    return age;
}
 
Example 8
Source File: ConsolePane.java    From dm3270 with Apache License 2.0 4 votes vote down vote up
public ConsolePane (Screen screen, Site server, PluginsStage pluginsStage)
{
  this.screen = screen;
  this.telnetState = screen.getTelnetState ();
  this.server = server;

  this.fontManager = screen.getFontManager ();
  pluginsStage.setConsolePane (this);

  screen.setConsolePane (this);
  screen.getScreenCursor ().addFieldChangeListener (this);
  screen.getScreenCursor ().addCursorMoveListener (this);

  setMargin (screen, new Insets (MARGIN, MARGIN, 0, MARGIN));

  menuBar.getMenus ().addAll (getCommandsMenu (), fontManager.getFontMenu ());

  // allow null for replay testing
  if (server == null || server.getPlugins ())
    menuBar.getMenus ().add (pluginsStage.getMenu (server));

  setTop (menuBar);
  setCenter (screen);
  setBottom (statusPane = getStatusBar ());
  menuBar.setUseSystemMenuBar (SYSTEM_MENUBAR);

  setHistoryBar ();

  if (server != null)
  {
    Optional<SiteParameters> sp = parameters.getSiteParameters (server.getName ());
    System.out.println (server.getName ());
    if (sp.isPresent ())
    {
      String offset = sp.get ().getParameter ("offset");
      if (!offset.isEmpty () && offset.length () > 4)
      {
        char direction = offset.charAt (3);
        int value = Integer.parseInt (offset.substring (4));
        System.out.printf ("Time offset : %s %d%n", direction, value);
        ZonedDateTime now = ZonedDateTime.now (ZoneOffset.UTC);
        System.out.println ("UTC         : " + now);
        if (direction == '+')
          now = now.plusHours (value);
        else
          now = now.minusHours (value);
        System.out.println ("Site        : " + now);
        System.out.println ();
      }
    }
  }

  screen.requestFocus ();
}
 
Example 9
Source File: TvMazeUtils.java    From drftpd with GNU General Public License v2.0 4 votes vote down vote up
private static String calculateAge(ZonedDateTime epDate) {
    // Get now
    ZonedDateTime now = ZonedDateTime.now();

    // Get the (positive) time between now and epData
    ZonedDateTime t1 = now;
    ZonedDateTime t2 = epDate;
    if (epDate.isBefore(now)) {
        t1 = epDate;
        t2 = now;
    }

    String age = "";
    long years = ChronoUnit.YEARS.between(t1, t2);
    if (years > 0) {
        age += years+"y";
        t2 = t2.minusYears(years);
    }

    long months = ChronoUnit.MONTHS.between(t1, t2);
    if (months > 0) {
        age += months+"m";
        t2 = t2.minusMonths(months);
    }

    long weeks = ChronoUnit.WEEKS.between(t1, t2);
    if (weeks > 0) {
        age += weeks+"w";
        t2 = t2.minusWeeks(weeks);
    }

    long days = ChronoUnit.DAYS.between(t1, t2);
    if (days > 0) {
        age += days+"d";
        t2 = t2.minusDays(days);
    }
    boolean spaceadded = false;

    long hours = ChronoUnit.HOURS.between(t1, t2);
    if (hours > 0) {
        age += " "+hours+"h";
        spaceadded = true;
        t2 = t2.minusHours(hours);
    }

    long minutes = ChronoUnit.MINUTES.between(t1, t2);
    if (minutes > 0) {
        if (!spaceadded) { age += " ";  }
        age += minutes+"m";
    }
    if (age.length() == 0) { age = "0"; }

    return age;
}
 
Example 10
Source File: QueryIT.java    From rya with Apache License 2.0 4 votes vote down vote up
@Test
public void dateTimeWithin() throws Exception {

    final ValueFactory vf = SimpleValueFactory.getInstance();
    final DatatypeFactory dtf = DatatypeFactory.newInstance();
    FunctionRegistry.getInstance().add(new DateTimeWithinPeriod());

    final String sparql = "PREFIX fn: <" + FN.NAMESPACE +">"
            + "SELECT ?event ?startTime ?endTime WHERE { ?event <uri:startTime> ?startTime; <uri:endTime> ?endTime. "
            + "FILTER(fn:dateTimeWithin(?startTime, ?endTime, 2,<" + OWLTime.HOURS_URI + "> ))}";

    final ZonedDateTime zTime = ZonedDateTime.now();
    final String time = zTime.format(DateTimeFormatter.ISO_INSTANT);

    final ZonedDateTime zTime1 = zTime.minusHours(1);
    final String time1 = zTime1.format(DateTimeFormatter.ISO_INSTANT);

    final ZonedDateTime zTime2 = zTime.minusHours(2);
    final String time2 = zTime2.format(DateTimeFormatter.ISO_INSTANT);

    final Literal lit = vf.createLiteral(dtf.newXMLGregorianCalendar(time));
    final Literal lit1 = vf.createLiteral(dtf.newXMLGregorianCalendar(time1));
    final Literal lit2 = vf.createLiteral(dtf.newXMLGregorianCalendar(time2));

    // Create the Statements that will be loaded into Rya.
    final Collection<Statement> statements = Sets.newHashSet(
            vf.createStatement(vf.createIRI("uri:event1"), vf.createIRI("uri:startTime"), lit),
            vf.createStatement(vf.createIRI("uri:event1"), vf.createIRI("uri:endTime"), lit1),
            vf.createStatement(vf.createIRI("uri:event2"), vf.createIRI("uri:startTime"), lit),
            vf.createStatement(vf.createIRI("uri:event2"), vf.createIRI("uri:endTime"), lit2)
           );

    // Create the expected results of the SPARQL query once the PCJ has been computed.
    final Set<BindingSet> expectedResults = new HashSet<>();

    final MapBindingSet bs = new MapBindingSet();
    bs.addBinding("event", vf.createIRI("uri:event1"));
    bs.addBinding("startTime", lit);
    bs.addBinding("endTime", lit1);
    expectedResults.add(bs);

    // Verify the end results of the query match the expected results.
    runTest(sparql, statements, expectedResults, ExportStrategy.RYA);
}
 
Example 11
Source File: AddHoursToDate.java    From tutorials with MIT License 4 votes vote down vote up
public ZonedDateTime subtractHoursToZonedDateTime(ZonedDateTime zonedDateTime, int hours) {
    return zonedDateTime.minusHours(hours);
}