Example usage for org.joda.time DateTime withHourOfDay

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

Introduction

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

Prototype

public DateTime withHourOfDay(int hour) 

Source Link

Document

Returns a copy of this datetime with the hour of day field updated.

Usage

From source file:org.everit.jira.timetracker.plugin.util.DateTimeConverterUtil.java

License:Apache License

/**
 * Concat DateTime date and Date time to a DateTime date based on the originalDate param.
 *
 * @param originalDate// w  ww  . j  ava 2 s .co m
 *          The date.
 * @param time
 *          The time.
 * @return The concated date time.
 */
public static DateTime stringToDateAndTime(final DateTime originalDate, final Date time) {
    DateTime date;
    try {
        date = originalDate.withHourOfDay(time.getHours());
        date = date.withMinuteOfHour(time.getMinutes());
    } catch (IllegalArgumentException e) {
        throw new WorklogException(WorklogComponent.PropertiesKey.DATE_PARSE, originalDate + " " + time);
    }
    return date;
}

From source file:org.jruby.CompatVersion.java

License:LGPL

protected static RubyTime s_mload(IRubyObject recv, RubyTime time, IRubyObject from) {
        Ruby runtime = recv.getRuntime();

        DateTime dt = new DateTime(DateTimeZone.UTC);

        byte[] fromAsBytes = null;
        fromAsBytes = from.convertToString().getBytes();
        if (fromAsBytes.length != 8) {
            throw runtime.newTypeError("marshaled time format differ");
        }/*from w  w w  . j av a 2  s . c  o  m*/
        int p = 0;
        int s = 0;
        for (int i = 0; i < 4; i++) {
            p |= ((int) fromAsBytes[i] & 0xFF) << (8 * i);
        }
        for (int i = 4; i < 8; i++) {
            s |= ((int) fromAsBytes[i] & 0xFF) << (8 * (i - 4));
        }
        if ((p & (1 << 31)) == 0) {
            dt = dt.withMillis(p * 1000L + s);
        } else {
            p &= ~(1 << 31);
            dt = dt.withYear(((p >>> 14) & 0xFFFF) + 1900);
            dt = dt.withMonthOfYear(((p >>> 10) & 0xF) + 1);
            dt = dt.withDayOfMonth(((p >>> 5) & 0x1F));
            dt = dt.withHourOfDay((p & 0x1F));
            dt = dt.withMinuteOfHour(((s >>> 26) & 0x3F));
            dt = dt.withSecondOfMinute(((s >>> 20) & 0x3F));
            // marsaling dumps usec, not msec
            dt = dt.withMillisOfSecond((s & 0xFFFFF) / 1000);
            dt = dt.withZone(getLocalTimeZone(runtime));
            time.setUSec((s & 0xFFFFF) % 1000);
        }
        time.setDateTime(dt);
        return time;
    }

From source file:org.jruby.RubyTime.java

License:LGPL

protected static RubyTime s_mload(IRubyObject recv, RubyTime time, IRubyObject from) {
    Ruby runtime = recv.getRuntime();/* w  w w . j  ava2 s  .c  o  m*/

    DateTime dt = new DateTime(DateTimeZone.UTC);

    byte[] fromAsBytes;
    fromAsBytes = from.convertToString().getBytes();
    if (fromAsBytes.length != 8) {
        throw runtime.newTypeError("marshaled time format differ");
    }
    int p = 0;
    int s = 0;
    for (int i = 0; i < 4; i++) {
        p |= ((int) fromAsBytes[i] & 0xFF) << (8 * i);
    }
    for (int i = 4; i < 8; i++) {
        s |= ((int) fromAsBytes[i] & 0xFF) << (8 * (i - 4));
    }
    boolean utc = false;
    if ((p & (1 << 31)) == 0) {
        dt = dt.withMillis(p * 1000L);
        time.setUSec((s & 0xFFFFF) % 1000);
    } else {
        p &= ~(1 << 31);
        utc = ((p >>> 30 & 0x1) == 0x1);
        dt = dt.withYear(((p >>> 14) & 0xFFFF) + 1900);
        dt = dt.withMonthOfYear(((p >>> 10) & 0xF) + 1);
        dt = dt.withDayOfMonth(((p >>> 5) & 0x1F));
        dt = dt.withHourOfDay((p & 0x1F));
        dt = dt.withMinuteOfHour(((s >>> 26) & 0x3F));
        dt = dt.withSecondOfMinute(((s >>> 20) & 0x3F));
        // marsaling dumps usec, not msec
        dt = dt.withMillisOfSecond((s & 0xFFFFF) / 1000);
        time.setUSec((s & 0xFFFFF) % 1000);
    }
    time.setDateTime(dt);
    if (!utc)
        time.localtime();

    from.getInstanceVariables().copyInstanceVariablesInto(time);

    // pull out nanos, offset, zone
    IRubyObject nano_num = (IRubyObject) from.getInternalVariables().getInternalVariable("nano_num");
    IRubyObject nano_den = (IRubyObject) from.getInternalVariables().getInternalVariable("nano_den");
    IRubyObject offsetVar = (IRubyObject) from.getInternalVariables().getInternalVariable("offset");
    IRubyObject zoneVar = (IRubyObject) from.getInternalVariables().getInternalVariable("zone");

    if (nano_num != null && nano_den != null) {
        long nanos = nano_num.convertToInteger().getLongValue() / nano_den.convertToInteger().getLongValue();
        time.nsec += nanos;
    }

    int offset = 0;
    if (offsetVar != null && offsetVar.respondsTo("to_int")) {
        IRubyObject oldExc = runtime.getGlobalVariables().get("$!"); // Save $!
        try {
            offset = offsetVar.convertToInteger().getIntValue() * 1000;
        } catch (RaiseException typeError) {
            runtime.getGlobalVariables().set("$!", oldExc); // Restore $!
        }
    }

    String zone = "";
    if (zoneVar != null && zoneVar.respondsTo("to_str")) {
        IRubyObject oldExc = runtime.getGlobalVariables().get("$!"); // Save $!
        try {
            zone = zoneVar.convertToString().toString();
        } catch (RaiseException typeError) {
            runtime.getGlobalVariables().set("$!", oldExc); // Restore $!
        }
    }

    time.dt = dt.withZone(getTimeZoneWithOffset(runtime, zone, offset));
    return time;
}

From source file:org.jvyamlb.SafeConstructorImpl.java

public static Object constructYamlTimestamp(final Constructor ctor, final Node node) {
    Matcher match = YMD_REGEXP.matcher(node.getValue().toString());
    if (match.matches()) {
        final String year_s = match.group(1);
        final String month_s = match.group(2);
        final String day_s = match.group(3);
        DateTime dt = new DateTime(0, 1, 1, 0, 0, 0, 0);
        if (year_s != null) {
            dt = dt.withYear(Integer.parseInt(year_s));
        }//  www .  j ava  2  s .  c  o  m
        if (month_s != null) {
            dt = dt.withMonthOfYear(Integer.parseInt(month_s));
        }
        if (day_s != null) {
            dt = dt.withDayOfMonth(Integer.parseInt(day_s));
        }
        return new Object[] { dt };
    }
    match = TIMESTAMP_REGEXP.matcher(node.getValue().toString());
    if (!match.matches()) {
        return new Object[] { ctor.constructPrivateType(node) };
    }
    final String year_s = match.group(1);
    final String month_s = match.group(2);
    final String day_s = match.group(3);
    final String hour_s = match.group(4);
    final String min_s = match.group(5);
    final String sec_s = match.group(6);
    final String fract_s = match.group(7);
    final String utc = match.group(8);
    final String timezoneh_s = match.group(9);
    final String timezonem_s = match.group(10);

    int usec = 0;
    if (fract_s != null) {
        usec = Integer.parseInt(fract_s);
        if (usec != 0) {
            while (10 * usec < 1000) {
                usec *= 10;
            }
        }
    }
    DateTime dt = new DateTime(0, 1, 1, 0, 0, 0, 0);
    if ("Z".equalsIgnoreCase(utc)) {
        dt = dt.withZone(DateTimeZone.forID("Etc/GMT"));
    } else {
        if (timezoneh_s != null || timezonem_s != null) {
            int zone = 0;
            int sign = +1;
            if (timezoneh_s != null) {
                if (timezoneh_s.startsWith("-")) {
                    sign = -1;
                }
                zone += Integer.parseInt(timezoneh_s.substring(1)) * 3600000;
            }
            if (timezonem_s != null) {
                zone += Integer.parseInt(timezonem_s) * 60000;
            }
            dt = dt.withZone(DateTimeZone.forOffsetMillis(sign * zone));
        }
    }
    if (year_s != null) {
        dt = dt.withYear(Integer.parseInt(year_s));
    }
    if (month_s != null) {
        dt = dt.withMonthOfYear(Integer.parseInt(month_s));
    }
    if (day_s != null) {
        dt = dt.withDayOfMonth(Integer.parseInt(day_s));
    }
    if (hour_s != null) {
        dt = dt.withHourOfDay(Integer.parseInt(hour_s));
    }
    if (min_s != null) {
        dt = dt.withMinuteOfHour(Integer.parseInt(min_s));
    }
    if (sec_s != null) {
        dt = dt.withSecondOfMinute(Integer.parseInt(sec_s));
    }
    dt = dt.withMillisOfSecond(usec / 1000);

    return new Object[] { dt, new Integer(usec % 1000) };
}

From source file:org.kairosdb.core.aggregator.RangeAggregator.java

License:Apache License

/**
 For YEARS, MONTHS, WEEKS, DAYS:/*w w w  .  ja  va2 s.  c o  m*/
 Computes the timestamp of the first millisecond of the day
 of the timestamp.
 For HOURS,
 Computes the timestamp of the first millisecond of the hour
 of the timestamp.
 For MINUTES,
 Computes the timestamp of the first millisecond of the minute
 of the timestamp.
 For SECONDS,
 Computes the timestamp of the first millisecond of the second
 of the timestamp.
 For MILLISECONDS,
 returns the timestamp
        
 @param timestamp
 @return
 */
private long alignRangeBoundary(long timestamp) {
    DateTime dt = new DateTime(timestamp, m_timeZone);
    TimeUnit tu = m_sampling.getUnit();
    switch (tu) {
    case YEARS:
        dt = dt.withDayOfYear(1).withMillisOfDay(0);
        break;
    case MONTHS:
        dt = dt.withDayOfMonth(1).withMillisOfDay(0);
        break;
    case WEEKS:
        dt = dt.withDayOfWeek(1).withMillisOfDay(0);
        break;
    case DAYS:
    case HOURS:
    case MINUTES:
    case SECONDS:
        dt = dt.withHourOfDay(0);
        dt = dt.withMinuteOfHour(0);
        dt = dt.withSecondOfMinute(0);
    default:
        dt = dt.withMillisOfSecond(0);
        break;
    }
    return dt.getMillis();
}

From source file:org.kalypso.ui.rrm.internal.timeseries.operations.RepairTimestampsOperation.java

License:Open Source License

private Date doRepair(final Date date) {
    final DateTime d1 = new DateTime(date.getTime(), m_timestamp.getChronology());
    final DateTime d2 = d1.withHourOfDay(m_timestamp.getHourOfDay());

    final long time = d2.toDate().getTime();
    date.setTime(time);/* w w w . java2 s .c o  m*/

    return date;
}

From source file:org.killbill.billing.plugin.analytics.reports.scheduler.JobsScheduler.java

License:Apache License

@VisibleForTesting
DateTime computeNextRun(final AnalyticsReportJob report) {

    final DateTime now = clock.getUTCNow();

    if (Frequency.HOURLY.equals(report.getRefreshFrequency())) {
        // 5' past the hour (fixed to avoid drifts)
        return now.plusHours(1).withMinuteOfHour(5).withSecondOfMinute(0).withMillisOfSecond(0);
    } else if (Frequency.DAILY.equals(report.getRefreshFrequency())) {
        // 6am GMT by default
        final Integer hourOfTheDayGMT = MoreObjects.firstNonNull(report.getRefreshHourOfDayGmt(), 6);
        final DateTime boundaryTime = now.withHourOfDay(hourOfTheDayGMT).withMinuteOfHour(0)
                .withSecondOfMinute(0).withMillisOfSecond(0);
        return now.compareTo(boundaryTime) >= 0 ? boundaryTime.plusDays(1) : boundaryTime;
    } else {/*from  w  ww .j  av  a2  s .c  o m*/
        // Run now
        return now;
    }
}

From source file:org.kuali.kpme.tklm.time.rules.graceperiod.service.GracePeriodServiceImpl.java

License:Educational Community License

public DateTime processGracePeriodRule(DateTime actualDateTime, LocalDate asOfDate) {
    DateTime gracePeriodDateTime = actualDateTime;

    GracePeriodRule gracePeriodRule = getGracePeriodRule(asOfDate);
    if (gracePeriodRule != null) {
        //go ahead and round off seconds
        gracePeriodDateTime = gracePeriodDateTime.withSecondOfMinute(0);

        BigDecimal minuteIncrement = gracePeriodRule.getHourFactor();
        BigDecimal minutes = new BigDecimal(gracePeriodDateTime.getMinuteOfHour());
        int bottomIntervalFactor = minutes.divide(minuteIncrement, 0, BigDecimal.ROUND_FLOOR).intValue();
        BigDecimal bottomInterval = new BigDecimal(bottomIntervalFactor).multiply(minuteIncrement);
        BigDecimal topInterval = new BigDecimal(bottomIntervalFactor + 1).multiply(minuteIncrement);
        if (bottomInterval.subtract(minutes).abs().intValue() < topInterval.subtract(minutes).abs()
                .intValue()) {//from w  w  w.  j  a va  2  s .  c  o  m
            gracePeriodDateTime = gracePeriodDateTime
                    .withMinuteOfHour(bottomInterval.setScale(0, BigDecimal.ROUND_HALF_UP).intValue());
        } else {
            if (topInterval.equals(new BigDecimal(60))) {
                gracePeriodDateTime = gracePeriodDateTime.withHourOfDay(gracePeriodDateTime.getHourOfDay() + 1)
                        .withMinuteOfHour(0);
            } else {
                gracePeriodDateTime = gracePeriodDateTime
                        .withMinuteOfHour(topInterval.setScale(0, BigDecimal.ROUND_HALF_UP).intValue());
            }
        }
    }
    return gracePeriodDateTime;
}

From source file:org.neotree.ui.view.DateTimeFieldView.java

License:Open Source License

@Override
public void onTimeSet(RadialPickerLayout view, int hourOfDay, int minute, int second) {
    DateTime value = getValue();//  w  ww.jav  a  2 s.c om
    DateTime baseDate = (value != null) ? value : DateTime.now();
    value = baseDate.withHourOfDay(hourOfDay).withMinuteOfHour(minute).withSecondOfMinute(0)
            .withMillisOfSecond(0);
    setValue(value);
}

From source file:org.opencastproject.index.service.catalog.adapter.DublinCoreMetadataUtil.java

License:Educational Community License

/**
 * Gets the current hour, minute and second from a dublin core period if available.
 *
 * @param period//from w  w w  .j av a2 s  . c o  m
 *          The current period from dublin core.
 * @return A new DateTime with the current hour, minute and second.
 */
static DateTime getCurrentStartTime(Opt<DCMIPeriod> period) {
    DateTime currentStartTime = new DateTime();
    currentStartTime = currentStartTime.withZone(DateTimeZone.UTC);
    currentStartTime = currentStartTime.withHourOfDay(0);
    currentStartTime = currentStartTime.withMinuteOfHour(0);
    currentStartTime = currentStartTime.withSecondOfMinute(0);

    if (period.isSome() && period.get().hasStart()) {
        DateTime fromDC = new DateTime(period.get().getStart().getTime());
        fromDC = fromDC.withZone(DateTimeZone.UTC);
        currentStartTime = currentStartTime.withZone(DateTimeZone.UTC);
        currentStartTime = currentStartTime.withHourOfDay(fromDC.getHourOfDay());
        currentStartTime = currentStartTime.withMinuteOfHour(fromDC.getMinuteOfHour());
        currentStartTime = currentStartTime.withSecondOfMinute(fromDC.getSecondOfMinute());
    }
    return currentStartTime;
}