Example usage for org.joda.time DateTime minusHours

List of usage examples for org.joda.time DateTime minusHours

Introduction

In this page you can find the example usage for org.joda.time DateTime minusHours.

Prototype

public DateTime minusHours(int hours) 

Source Link

Document

Returns a copy of this datetime minus the specified number of hours.

Usage

From source file:niche.newres.timedevents2owl.randomizer.TimedEvents2OWLRandomizer.java

public static DateTime shiftToPreviousNightTimeDateTime(DateTime dateTime) {
    if (dateTime.getHourOfDay() < 8 || dateTime.getHourOfDay() > 22) {
        return dateTime;
    } else {//from  w w  w . j a va  2  s. co m
        return dateTime.minusHours(dateTime.getHourOfDay() - 5);
    }
}

From source file:org.apache.cloudstack.utils.security.CertUtils.java

License:Apache License

public static X509Certificate generateV3Certificate(final X509Certificate caCert, final KeyPair caKeyPair,
        final PublicKey clientPublicKey, final String subject, final String signatureAlgorithm,
        final int validityDays, final List<String> dnsNames, final List<String> publicIPAddresses)
        throws IOException, NoSuchAlgorithmException, CertificateException, NoSuchProviderException,
        InvalidKeyException, SignatureException, OperatorCreationException {

    final DateTime now = DateTime.now(DateTimeZone.UTC);
    final BigInteger serial = generateRandomBigInt();
    final JcaX509ExtensionUtils extUtils = new JcaX509ExtensionUtils();
    final X509v3CertificateBuilder certBuilder;
    if (caCert == null) {
        // Generate CA certificate
        certBuilder = new JcaX509v3CertificateBuilder(new X500Name(subject), serial,
                now.minusHours(12).toDate(), now.plusDays(validityDays).toDate(), new X500Name(subject),
                clientPublicKey);//ww w  . j ava2s .  c o m

        certBuilder.addExtension(Extension.basicConstraints, true, new BasicConstraints(true));
        certBuilder.addExtension(Extension.keyUsage, true,
                new KeyUsage(KeyUsage.keyCertSign | KeyUsage.cRLSign));
    } else {
        // Generate client certificate
        certBuilder = new JcaX509v3CertificateBuilder(caCert, serial, now.minusHours(12).toDate(),
                now.plusDays(validityDays).toDate(), new X500Principal(subject), clientPublicKey);

        certBuilder.addExtension(Extension.authorityKeyIdentifier, false,
                extUtils.createAuthorityKeyIdentifier(caCert));
    }

    certBuilder.addExtension(Extension.subjectKeyIdentifier, false,
            extUtils.createSubjectKeyIdentifier(clientPublicKey));

    final List<ASN1Encodable> subjectAlternativeNames = new ArrayList<ASN1Encodable>();
    if (publicIPAddresses != null) {
        for (final String publicIPAddress : publicIPAddresses) {
            if (Strings.isNullOrEmpty(publicIPAddress)) {
                continue;
            }
            subjectAlternativeNames.add(new GeneralName(GeneralName.iPAddress, publicIPAddress));
        }
    }
    if (dnsNames != null) {
        for (final String dnsName : dnsNames) {
            if (Strings.isNullOrEmpty(dnsName)) {
                continue;
            }
            subjectAlternativeNames.add(new GeneralName(GeneralName.dNSName, dnsName));
        }
    }
    if (subjectAlternativeNames.size() > 0) {
        final GeneralNames subjectAltNames = GeneralNames
                .getInstance(new DERSequence(subjectAlternativeNames.toArray(new ASN1Encodable[] {})));
        certBuilder.addExtension(Extension.subjectAlternativeName, false, subjectAltNames);
    }

    final ContentSigner signer = new JcaContentSignerBuilder(signatureAlgorithm).setProvider("BC")
            .build(caKeyPair.getPrivate());
    final X509CertificateHolder certHolder = certBuilder.build(signer);
    final X509Certificate cert = new JcaX509CertificateConverter().setProvider("BC").getCertificate(certHolder);
    if (caCert != null) {
        cert.verify(caCert.getPublicKey());
    } else {
        cert.verify(caKeyPair.getPublic());
    }
    return cert;
}

From source file:org.apache.pig.pen.AugmentBaseDataVisitor.java

License:Apache License

Object GetSmallerValue(Object v) {
    byte type = DataType.findType(v);

    if (type == DataType.BAG || type == DataType.TUPLE || type == DataType.MAP)
        return null;

    switch (type) {
    case DataType.CHARARRAY:
        String str = (String) v;
        if (str.length() > 0)
            return str.substring(0, str.length() - 1);
        else//  ww  w.ja  v a2  s .  c o  m
            return null;
    case DataType.BYTEARRAY:
        DataByteArray data = (DataByteArray) v;
        if (data.size() > 0)
            return new DataByteArray(data.get(), 0, data.size() - 1);
        else
            return null;
    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).subtract(BigInteger.ONE);
    case DataType.BIGDECIMAL:
        return ((BigDecimal) v).subtract(BigDecimal.ONE);
    case DataType.DATETIME:
        DateTime dt = (DateTime) v;
        if (dt.getMillisOfSecond() != 0) {
            return dt.minusMillis(1);
        } else if (dt.getSecondOfMinute() != 0) {
            return dt.minusSeconds(1);
        } else if (dt.getMinuteOfHour() != 0) {
            return dt.minusMinutes(1);
        } else if (dt.getHourOfDay() != 0) {
            return dt.minusHours(1);
        } else {
            return dt.minusDays(1);
        }
    default:
        return null;
    }

}

From source file:org.countandra.utils.CountandraUtils.java

License:Apache License

private long getStartMillis(String timeQuery) {
    DateTime dt = new DateTime(DateTimeZone.UTC);

    TimePeriod tq = hshSupportedTimePeriods.get(timeQuery);

    if (tq != null) {
        switch (tq) {
        case THISHOUR:
            return dt.minusHours(1).getMillis();
        case LASTHOUR:
            return dt.minusHours(2).getMillis();
        case LASTTFHOURS:
            return dt.minusHours(24).getMillis();
        case TODAY:
            return dt.minusHours(24).getMillis();
        case YESTERDAY:
            return dt.minusHours(48).getMillis();
        case THISWEEK:
            return dt.minusWeeks(1).getMillis();
        case LASTWEEK:
            return dt.minusWeeks(2).getMillis();
        case THISMONTH:
            return dt.minusMonths(1).getMillis();
        case LASTMONTH:
            return dt.minusMonths(2).getMillis();
        case THISYEAR:
            return dt.minusYears(1).getMillis();
        case LASTYEAR:
            return dt.minusYears(2).getMillis();

        }//from  w ww .j  ava2s.  c o m

    } else {

        return 0L;
    }

    return 0;

}

From source file:org.countandra.utils.CountandraUtils.java

License:Apache License

private long getEndMillis(String timeQuery) {
    DateTime dt = new DateTime(DateTimeZone.UTC);

    TimePeriod tq = hshSupportedTimePeriods.get(timeQuery);

    if (tq != null) {
        switch (tq) {
        case THISHOUR:
            return dt.getMillis();
        case LASTHOUR:
            return dt.minusHours(1).getMillis();
        case LASTTFHOURS:
            return dt.getMillis();
        case TODAY:
            return dt.getMillis();
        case YESTERDAY:
            return dt.minusHours(24).getMillis();
        case THISWEEK:
            return dt.getMillis();
        case LASTWEEK:
            return dt.minusWeeks(1).getMillis();
        case THISMONTH:
            return dt.getMillis();
        case LASTMONTH:
            return dt.minusMonths(1).getMillis();
        case THISYEAR:
            return dt.getMillis();
        case LASTYEAR:
            return dt.minusYears(1).getMillis();

        }//ww  w.  j av  a 2 s  .co  m

    } else {

        return 0L;
    }

    return 0L;

}

From source file:org.emonocot.portal.view.Functions.java

License:Open Source License

public static String printTimeOnly(DateTime dateTime) {
    if (dateTime == null) {
        return null;
    } else {/*from   w w w  .j av a 2  s. com*/
        return timeOnlyFormatter.print(dateTime.minusHours(1));
    }
}

From source file:org.epics.archiverappliance.common.TimeUtils.java

public static Timestamp minusHours(Timestamp ts, int hours) {
    DateTime dateTime = new DateTime(ts.getTime(), DateTimeZone.UTC);
    DateTime retval = dateTime.minusHours(hours);
    Timestamp retts = new Timestamp(retval.getMillis());
    retts.setNanos(ts.getNanos());//from w  w  w .  ja  v a 2  s .  com
    return retts;
}

From source file:org.epics.archiverappliance.common.TimeUtils.java

/**
 * Given an epoch seconds and a granularity, this method gives you the last second in the previous partition as epoch seconds.
 * @param epochSeconds/*ww w . jav  a 2  s .c  om*/
 * @param granularity
 * @return
 */
public static long getPreviousPartitionLastSecond(long epochSeconds, PartitionGranularity granularity) {
    DateTime dateTime = new DateTime(epochSeconds * 1000, DateTimeZone.UTC);
    DateTime previousPartitionLastSecond = null;
    switch (granularity) {
    case PARTITION_YEAR:
        previousPartitionLastSecond = dateTime.minusYears(1).withMonthOfYear(12).withDayOfMonth(31)
                .withHourOfDay(23).withMinuteOfHour(59).withSecondOfMinute(59);
        return previousPartitionLastSecond.getMillis() / 1000;
    case PARTITION_MONTH:
        previousPartitionLastSecond = dateTime.withDayOfMonth(1).minusDays(1).withHourOfDay(23)
                .withMinuteOfHour(59).withSecondOfMinute(59);
        return previousPartitionLastSecond.getMillis() / 1000;
    case PARTITION_DAY:
        previousPartitionLastSecond = dateTime.minusDays(1).withHourOfDay(23).withMinuteOfHour(59)
                .withSecondOfMinute(59);
        return previousPartitionLastSecond.getMillis() / 1000;
    case PARTITION_HOUR:
        previousPartitionLastSecond = dateTime.minusHours(1).withMinuteOfHour(59).withSecondOfMinute(59);
        return previousPartitionLastSecond.getMillis() / 1000;
    case PARTITION_5MIN:
    case PARTITION_15MIN:
    case PARTITION_30MIN:
        int approxMinutesPerChunk = granularity.getApproxMinutesPerChunk();
        int startOfPartition_Min = (dateTime.getMinuteOfHour() / approxMinutesPerChunk) * approxMinutesPerChunk;
        previousPartitionLastSecond = dateTime.withMinuteOfHour(startOfPartition_Min).withSecondOfMinute(0)
                .minusSeconds(1);
        return previousPartitionLastSecond.getMillis() / 1000;
    default:
        throw new UnsupportedOperationException("Invalid Partition type " + granularity);
    }
}

From source file:org.fenixedu.academic.domain.WrittenEvaluation.java

License:Open Source License

protected List<EvaluationEventBean> getAllEvents(String description, Registration registration) {
    List<EvaluationEventBean> result = new ArrayList<EvaluationEventBean>();
    String url = CoreConfiguration.getConfiguration().applicationUrl();

    Set<ExecutionCourse> executionCourses = new HashSet<ExecutionCourse>();
    executionCourses.addAll(this.getAttendingExecutionCoursesFor(registration));

    if (this.getEnrollmentBeginDayDateYearMonthDay() != null) {
        DateTime enrollmentBegin = convertTimes(this.getEnrollmentBeginDayDateYearMonthDay(),
                this.getEnrollmentBeginTimeDateHourMinuteSecond());
        DateTime enrollmentEnd = convertTimes(this.getEnrollmentEndDayDateYearMonthDay(),
                this.getEnrollmentEndTimeDateHourMinuteSecond());

        result.add(new EvaluationEventBean("Inicio das inscries para " + description, enrollmentBegin,
                enrollmentBegin.plusHours(1), false, null, url + "/login", null, executionCourses));

        result.add(// w  w  w.j  a v a2  s .  c om
                new EvaluationEventBean("Fim das inscries para " + description, enrollmentEnd.minusHours(1),
                        enrollmentEnd, false, null, url + "/login", null, executionCourses));
    }

    Set<Space> rooms = new HashSet<>();

    if (registration.getRoomFor(this) != null) {
        rooms.add(registration.getRoomFor(this));
    } else {
        for (WrittenEvaluationSpaceOccupation weSpaceOcupation : this
                .getWrittenEvaluationSpaceOccupationsSet()) {
            rooms.add(weSpaceOcupation.getRoom());
        }
    }

    WrittenEvaluationEnrolment writtenEvaluationEnrolment = getWrittenEvaluationEnrolmentFor(registration);
    Space assignedRoom = null;

    if (writtenEvaluationEnrolment != null) {
        assignedRoom = writtenEvaluationEnrolment.getRoom();
    }

    result.add(new EvaluationEventBean(description, this.getBeginningDateTime(), this.getEndDateTime(), false,
            assignedRoom, rooms, executionCourses.iterator().next().getSiteUrl(), null, executionCourses));

    return result;
}

From source file:org.fenixedu.learning.api.EventsResource.java

License:Open Source License

private static Collection<ScheduleEventBean> projectEvents(Project project, ExecutionCourse executionCourse,
        Interval interval) {/* w  ww. j ava2  s.c o m*/
    DateTime projectStart = project.getProjectBeginDateTime();
    DateTime projectEnd = project.getProjectEndDateTime();

    Set<ScheduleEventBean> events = new HashSet<>();

    if (interval.contains(projectStart)) {
        events.add(new ScheduleEventBean(executionCourse.getPrettyAcronym(),
                project.getEvaluationType().toString(), project.getPresentationName(), projectStart,
                projectStart.plusHours(1), null, executionCourse.getSiteUrl(),
                colorForType(project.getEvaluationType()), null, null));
    }
    if (interval.contains(projectEnd)) {
        events.add(new ScheduleEventBean(executionCourse.getPrettyAcronym(),
                project.getEvaluationType().toString(), project.getPresentationName(), projectEnd.minusHours(1),
                projectEnd, null, executionCourse.getSiteUrl(), colorForType(project.getEvaluationType()), null,
                null));
    }

    return events;
}