Example usage for org.joda.time DateTime getHourOfDay

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

Introduction

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

Prototype

public int getHourOfDay() 

Source Link

Document

Get the hour of day field value.

Usage

From source file:com.francelabs.datafari.alerts.AlertsManager.java

License:Apache License

/**
 * Calculate the difference in minutes between the current date time and the
 * provided scheduled one, according to the frequency
 *
 * @param frequency//w ww.j  a  v a 2s.c  o  m
 *          the frequency of the scheduled date time
 * @param scheduledDate
 *          the initial scheduled date time that the user has typed in the
 *          admin UI
 * @return the difference in minutes between the current date time and the
 *         next scheduled one
 */
private long calculateDelays(final String frequency, final DateTime scheduledDate) {
    long diff = 0L;
    final Calendar cal = Calendar.getInstance();
    cal.setTime(new Date());
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    final DateTime currentDateTime = new DateTime(cal.getTime());
    DateTime scheduledDateTimeUpdate;

    switch (frequency.toLowerCase()) {
    case "hourly":
        // Create what would be the current scheduled date
        cal.setTime(new Date());
        cal.set(Calendar.MINUTE, scheduledDate.getMinuteOfHour());
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        scheduledDateTimeUpdate = new DateTime(cal.getTime());

        // Compare the current date with the current scheduled one, if the
        // current date is earlier than the scheduled one, simply calculate
        // the difference in minutes, otherwise create the next scheduled
        // date and calculate the difference
        if (!currentDateTime.isBefore(scheduledDateTimeUpdate)) {
            cal.add(Calendar.HOUR_OF_DAY, 1);
            scheduledDateTimeUpdate = new DateTime(cal.getTime());
        }
        diff = Minutes.minutesBetween(currentDateTime, scheduledDateTimeUpdate).getMinutes();
        break;

    case "daily":
        // Create what would be the current scheduled date
        cal.setTime(new Date());
        cal.set(Calendar.HOUR_OF_DAY, scheduledDate.getHourOfDay());
        cal.set(Calendar.MINUTE, scheduledDate.getMinuteOfHour());
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        scheduledDateTimeUpdate = new DateTime(cal.getTime());

        // Compare the current date with the current scheduled one, if the
        // current date is earlier than the scheduled one, simply calculate
        // the difference in minutes, otherwise create the next scheduled
        // date and calculate the difference
        if (!currentDateTime.isBefore(scheduledDateTimeUpdate)) {
            cal.add(Calendar.DAY_OF_YEAR, 1);
            scheduledDateTimeUpdate = new DateTime(cal.getTime());
        }
        diff = Minutes.minutesBetween(currentDateTime, scheduledDateTimeUpdate).getMinutes();
        break;

    case "weekly":
        // Create what would be the current scheduled date
        cal.setTime(new Date());
        cal.set(Calendar.DAY_OF_WEEK, scheduledDate.getDayOfWeek() + 1); // +1
        // =
        // diff
        // between
        // Joda
        // and
        // Calendar
        cal.set(Calendar.HOUR_OF_DAY, scheduledDate.getHourOfDay());
        cal.set(Calendar.MINUTE, scheduledDate.getMinuteOfHour());
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        scheduledDateTimeUpdate = new DateTime(cal.getTime());

        // Compare the current date with the current scheduled one, if the
        // current date is earlier than the scheduled one, simply calculate
        // the difference in minutes, otherwise create the next scheduled
        // date and calculate the difference
        if (!currentDateTime.isBefore(scheduledDateTimeUpdate)) {
            cal.add(Calendar.WEEK_OF_YEAR, 1);
            scheduledDateTimeUpdate = new DateTime(cal.getTime());
        }
        diff = Minutes.minutesBetween(currentDateTime, scheduledDateTimeUpdate).getMinutes();
        break;

    default:
        break;
    }

    return diff;
}

From source file:com.francelabs.datafari.servlets.admin.alertsAdmin.java

License:Apache License

private String getNextEvent(final String frequency, final String initialDate) {
    final DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy/HH:mm");
    final DateTime scheduledDate = new DateTime(formatter.parseDateTime(initialDate));
    final Calendar cal = Calendar.getInstance();
    cal.setTime(new Date());
    cal.set(Calendar.SECOND, 0);/*from  w w w.  j av a  2 s  . c  o m*/
    cal.set(Calendar.MILLISECOND, 0);
    final DateTime currentDateTime = new DateTime(cal.getTime());
    DateTime scheduledDateTimeUpdate = new DateTime(cal.getTime());

    switch (frequency.toLowerCase()) {
    case "hourly":
        // Create what would be the current scheduled date
        cal.setTime(new Date());
        cal.set(Calendar.MINUTE, scheduledDate.getMinuteOfHour());
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        scheduledDateTimeUpdate = new DateTime(cal.getTime());

        // Compare the current date with the current scheduled one, if the
        // current date is later than the scheduled one then create the next
        // scheduled date
        if (!currentDateTime.isBefore(scheduledDateTimeUpdate)) {
            cal.add(Calendar.HOUR_OF_DAY, 1);
            scheduledDateTimeUpdate = new DateTime(cal.getTime());
        }
        break;

    case "daily":
        // Create what would be the current scheduled date
        cal.setTime(new Date());
        cal.set(Calendar.HOUR_OF_DAY, scheduledDate.getHourOfDay());
        cal.set(Calendar.MINUTE, scheduledDate.getMinuteOfHour());
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        scheduledDateTimeUpdate = new DateTime(cal.getTime());

        // Compare the current date with the current scheduled one, if the
        // current date is later than the scheduled one then create the next
        // scheduled date
        if (!currentDateTime.isBefore(scheduledDateTimeUpdate)) {
            cal.add(Calendar.DAY_OF_YEAR, 1);
            scheduledDateTimeUpdate = new DateTime(cal.getTime());
        }
        break;

    case "weekly":
        // Create what would be the current scheduled date
        cal.setTime(new Date());
        cal.set(Calendar.DAY_OF_WEEK, scheduledDate.getDayOfWeek() + 1); // +1
        // =
        // diff
        // between
        // Joda
        // and
        // Calendar
        cal.set(Calendar.HOUR_OF_DAY, scheduledDate.getHourOfDay());
        cal.set(Calendar.MINUTE, scheduledDate.getMinuteOfHour());
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        scheduledDateTimeUpdate = new DateTime(cal.getTime());

        // Compare the current date with the current scheduled one, if the
        // current date is later than the scheduled one then create the next
        // scheduled date
        if (!currentDateTime.isBefore(scheduledDateTimeUpdate)) {
            cal.add(Calendar.WEEK_OF_YEAR, 1);
            scheduledDateTimeUpdate = new DateTime(cal.getTime());
        }
        break;

    default:
        break;
    }
    return scheduledDateTimeUpdate.toString(formatter);
}

From source file:com.github.autermann.matlab.value.MatlabDateTime.java

License:Open Source License

public double[] toArray() {
    DateTime utc = time.toDateTime(DateTimeZone.UTC);
    return new double[] { (double) utc.getYear(), (double) utc.getMonthOfYear(), (double) utc.getDayOfMonth(),
            (double) utc.getHourOfDay(), (double) utc.getMinuteOfHour(),
            (double) utc.getSecondOfMinute() + (double) utc.getMillisOfSecond() / 1000, };

}

From source file:com.github.dbourdette.otto.source.TimeFrame.java

License:Apache License

private DateTime twelveHours(DateTime dateTime) {
    dateTime = dateTime.withMillisOfSecond(0);
    dateTime = dateTime.withSecondOfMinute(0);
    dateTime = dateTime.withMinuteOfHour(0);

    if (dateTime.getHourOfDay() < 12) {
        return dateTime.withHourOfDay(0);
    } else {/*from   w  w  w .  java2s  .c  om*/
        return dateTime.withHourOfDay(12);
    }
}

From source file:com.github.markserrano.jsonquery.jpa.util.DateTimeUtil.java

License:Apache License

public static DateTime normalize(DateTime dateTime) {
    if (dateTime.toString().matches(".+(\\+)[0-9][0-9]:[0-9][0-9]")) {
        dateTime = dateTime.minusHours(dateTime.getHourOfDay());

    } else if (dateTime.toString().matches(".+(\\-)[0-9][0-9]:[0-9][0-9]")) {
        dateTime = dateTime.plusHours(24 - dateTime.getHourOfDay());
    }//from w  w  w .jav a 2 s .c om
    return dateTime;
}

From source file:com.github.markserrano.jsonquery.jpa.util.DateTimeUtil.java

License:Apache License

public static Map<String, DateTime> getYesterdaySpan() {
    DateTime now = new DateTime();

    DateTime from = now.minusDays(1).minusHours(now.getHourOfDay()).minusMinutes(now.getMinuteOfHour())
            .minusSeconds(now.getSecondOfMinute()).minusMillis(now.getMillisOfSecond());

    DateTime to = from.plusHours(23).plusMinutes(59).plusSeconds(59);

    Map<String, DateTime> map = new HashMap<String, DateTime>();
    map.put("from", from);
    map.put("to", to);

    return map;//from  w w  w .jav  a 2  s.c o  m
}

From source file:com.google.android.apps.paco.ExperimentScheduleActivity.java

License:Open Source License

private void showEsmScheduleConfiguration() {
    setContentView(R.layout.esm_schedule);
    TextView title = (TextView) findViewById(R.id.experimentNameSchedule);
    title.setText(experiment.getExperimentDAO().getTitle());

    startHourField = (Button) findViewById(R.id.startHourTimePickerLabel);
    startHourField.setText(new DateMidnight().toDateTime()
            .withMillisOfDay(schedule.getEsmStartHour().intValue()).toString(TIME_FORMAT_STRING));

    endHourField = (Button) findViewById(R.id.endHourTimePickerLabel);
    endHourField.setText(new DateMidnight().toDateTime().withMillisOfDay(schedule.getEsmEndHour().intValue())
            .toString(TIME_FORMAT_STRING));

    // TODO (bobevans): get rid of this duplication

    startHourField.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(ExperimentScheduleActivity.this);
            unsetTimesViewParent();/*w  w  w.ja v a2  s . c  o  m*/
            dialogBuilder.setView(timesScheduleLayout);
            final AlertDialog dialog = dialogBuilder.setTitle(R.string.start_time_title).create();

            Long offset = schedule.getEsmStartHour();
            DateTime startHour = new DateMidnight().toDateTime().withMillisOfDay(offset.intValue());
            timePicker.setCurrentHour(startHour.getHourOfDay());
            timePicker.setCurrentMinute(startHour.getMinuteOfHour());

            dialog.setButton(Dialog.BUTTON_POSITIVE, getString(R.string.save_button),
                    new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog, int which) {
                            schedule.setEsmStartHour(getHourOffsetFromPicker());
                            startHourField.setText(getTextFromPicker(schedule.getEsmStartHour().intValue()));
                        }

                    });
            dialog.show();
        }
    });

    //

    endHourField.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            final AlertDialog.Builder endHourDialogBuilder = new AlertDialog.Builder(
                    ExperimentScheduleActivity.this);
            unsetTimesViewParent();
            endHourDialogBuilder.setView(timesScheduleLayout);
            final AlertDialog endHourDialog = endHourDialogBuilder.setTitle(R.string.end_time_title).create();

            Long offset = schedule.getEsmEndHour();
            DateTime endHour = new DateMidnight().toDateTime().withMillisOfDay(offset.intValue());
            timePicker.setCurrentHour(endHour.getHourOfDay());
            timePicker.setCurrentMinute(endHour.getMinuteOfHour());

            endHourDialog.setButton(Dialog.BUTTON_POSITIVE, getString(R.string.save_button),
                    new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog, int which) {
                            schedule.setEsmEndHour(getHourOffsetFromPicker());
                            endHourField.setText(getTextFromPicker(schedule.getEsmEndHour().intValue()));
                        }

                    });
            endHourDialog.show();
        }
    });

}

From source file:com.google.android.apps.paco.SignalSchedule.java

License:Open Source License

public String getHourOffsetAsTimeString(Long esmEndHour2) {
    DateTime endHour = new DateMidnight().toDateTime().plus(esmEndHour2);
    String endHourString = endHour.getHourOfDay() + ":" + pad(endHour.getMinuteOfHour());
    return endHourString;
}

From source file:com.google.cloud.dataflow.sdk.util.TimeUtil.java

License:Apache License

/**
 * Converts a {@link ReadableInstant} into a Dateflow API time value.
 *///from ww  w . ja  v  a  2  s  .  co  m
public static String toCloudTime(ReadableInstant instant) {
    // Note that since Joda objects use millisecond resolution, we always
    // produce either no fractional seconds or fractional seconds with
    // millisecond resolution.

    // Translate the ReadableInstant to a DateTime with ISOChronology.
    DateTime time = new DateTime(instant);

    int millis = time.getMillisOfSecond();
    if (millis == 0) {
        return String.format("%04d-%02d-%02dT%02d:%02d:%02dZ", time.getYear(), time.getMonthOfYear(),
                time.getDayOfMonth(), time.getHourOfDay(), time.getMinuteOfHour(), time.getSecondOfMinute());
    } else {
        return String.format("%04d-%02d-%02dT%02d:%02d:%02d.%03dZ", time.getYear(), time.getMonthOfYear(),
                time.getDayOfMonth(), time.getHourOfDay(), time.getMinuteOfHour(), time.getSecondOfMinute(),
                millis);
    }
}

From source file:com.google.jenkins.plugins.cloudbackup.backup.BackupProcedure.java

License:Open Source License

private static String calculateBackupName(DateTime backupTime) {
    return String.format("backup-%d%02d%02d%02d%02d%02d", backupTime.getYear(), backupTime.getMonthOfYear(),
            backupTime.getDayOfMonth(), backupTime.getHourOfDay(), backupTime.getMinuteOfHour(),
            backupTime.getSecondOfMinute());
}