Example usage for org.joda.time DateTime getMinuteOfHour

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

Introduction

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

Prototype

public int getMinuteOfHour() 

Source Link

Document

Get the minute of hour field value.

Usage

From source file:aplicacion.control.util.Fechas.java

public static LocalTime getLocalFromTime(Time time) {
    if (time == null) {
        return null;
    } else {//from w ww.j  a  va 2 s.  c om
        DateTime dateTime = new DateTime(time.getTime());
        return LocalTime.of(dateTime.getHourOfDay(), dateTime.getMinuteOfHour());
    }
}

From source file:app.rappla.calendar.Date.java

License:Open Source License

/**
 * Constructor//from   w  w  w .  ja v  a2s .  c  om
 * 
 * @param icalStr
 *            One or more lines of iCalendar that specifies a date
 * @param parseMode
 *            PARSE_STRICT or PARSE_LOOSE
 */
public Date(String icalStr) throws ParseException, BogusDataException {
    super(icalStr);

    year = month = day = 0;
    hour = minute = second = 0;

    for (int i = 0; i < attributeList.size(); i++) {
        Attribute a = attributeAt(i);
        String aname = a.name.toUpperCase(Locale.ENGLISH);
        String aval = a.value.toUpperCase(Locale.ENGLISH);
        // TODO: not sure if any attributes are allowed here...
        // Look for VALUE=DATE or VALUE=DATE-TIME
        // DATE means untimed for the event
        if (aname.equals("VALUE")) {
            if (aval.equals("DATE")) {
                dateOnly = true;
            } else if (aval.equals("DATE-TIME")) {
                dateOnly = false;
            }
        } else if (aname.equals("TZID")) {
            tzid = a.value;
        } else {
            // TODO: anything else allowed here?
        }
    }

    String inDate = value;

    if (inDate.length() < 8) {
        // Invalid format
        throw new ParseException("Invalid date format '" + inDate + "'", inDate);
    }

    // Make sure all parts of the year are numeric.
    for (int i = 0; i < 8; i++) {
        char ch = inDate.charAt(i);
        if (ch < '0' || ch > '9') {
            throw new ParseException("Invalid date format '" + inDate + "'", inDate);
        }
    }
    year = Integer.parseInt(inDate.substring(0, 4));
    month = Integer.parseInt(inDate.substring(4, 6));
    day = Integer.parseInt(inDate.substring(6, 8));
    if (day < 1 || day > 31 || month < 1 || month > 12)
        throw new BogusDataException("Invalid date '" + inDate + "'", inDate);
    // Make sure day of month is valid for specified month
    if (year % 4 == 0) {
        // leap year
        if (day > leapMonthDays[month - 1]) {
            throw new BogusDataException("Invalid day of month '" + inDate + "'", inDate);
        }
    } else {
        if (day > monthDays[month - 1]) {
            throw new BogusDataException("Invalid day of month '" + inDate + "'", inDate);
        }
    }
    // TODO: parse time, handle localtime, handle timezone
    if (inDate.length() > 8) {
        // TODO make sure dateOnly == false
        if (inDate.charAt(8) == 'T') {
            try {
                hour = Integer.parseInt(inDate.substring(9, 11));
                minute = Integer.parseInt(inDate.substring(11, 13));
                second = Integer.parseInt(inDate.substring(13, 15));
                if (hour > 23 || minute > 59 || second > 59) {
                    throw new BogusDataException("Invalid time in date string '" + inDate + "'", inDate);
                }
                if (inDate.length() > 15) {
                    isUTC = inDate.charAt(15) == 'Z';
                }
            } catch (NumberFormatException nef) {
                throw new BogusDataException("Invalid time in date string '" + inDate + "' - " + nef, inDate);
            }
        } else {
            // Invalid format
            throw new ParseException("Invalid date format '" + inDate + "'", inDate);
        }
    } else {
        // Just date, no time
        dateOnly = true;
    }

    if (isUTC && !dateOnly) {
        // Use Joda Time to convert UTC to localtime
        DateTime utcDateTime = new DateTime(DateTimeZone.UTC);
        utcDateTime = utcDateTime.withDate(year, month, day).withTime(hour, minute, second, 0);
        DateTime localDateTime = utcDateTime.withZone(DateTimeZone.getDefault());
        year = localDateTime.getYear();
        month = localDateTime.getMonthOfYear();
        day = localDateTime.getDayOfMonth();
        hour = localDateTime.getHourOfDay();
        minute = localDateTime.getMinuteOfHour();
        second = localDateTime.getSecondOfMinute();
    } else if (tzid != null) {
        DateTimeZone tz = DateTimeZone.forID(tzid);
        if (tz != null) {
            // Convert to localtime
            DateTime utcDateTime = new DateTime(tz);
            utcDateTime = utcDateTime.withDate(year, month, day).withTime(hour, minute, second, 0);
            DateTime localDateTime = utcDateTime.withZone(DateTimeZone.getDefault());
            year = localDateTime.getYear();
            month = localDateTime.getMonthOfYear();
            day = localDateTime.getDayOfMonth();
            hour = localDateTime.getHourOfDay();
            minute = localDateTime.getMinuteOfHour();
            second = localDateTime.getSecondOfMinute();
            // Since we have converted to localtime, remove the TZID
            // attribute
            this.removeNamedAttribute("TZID");
        }
    }
    isUTC = false;

    // Add attribute that says date-only or date with time
    if (dateOnly)
        addAttribute("VALUE", "DATE");
    else
        addAttribute("VALUE", "DATE-TIME");

}

From source file:ca.uwaterloo.cs.cs349.mikrocalendar.ui.datetimepicker.DateTimePickerDialog.java

License:Open Source License

/**
 * Creates a new {@link DateTimePickerDialog} with a specified
 * {@link DateTime}./*from  w w w.j  ava  2 s. co m*/
 * 
 * @param dateTime
 *            The {@link DateTime}.
 */
public DateTimePickerDialog(DateTime dateTime) {
    super((Frame) null, true);
    setTitle("Date Time Picker");

    this.dateTime = dateTime;

    // Array of selectable months.
    String[] months = new String[] { null, "January", "February", "March", "April", "May", "June", "July",
            "August", "September", "October", "November", "December" };

    // Array of selectable days.
    Integer days[] = new Integer[32];
    days[0] = null;
    for (int i = 1; i < days.length; i++) {
        days[i] = new Integer(i);
    }

    // Array of selectable hours.
    Integer hours[] = new Integer[24];
    for (int i = 0; i < hours.length; i++) {
        hours[i] = new Integer(i);
    }

    // Array of selectable minutes.
    Integer minutes[] = new Integer[60];
    for (int i = 0; i < minutes.length; i++) {
        minutes[i] = new Integer(i);
    }

    // Date time fields.
    monthComboBox = new JComboBox(months);
    dayComboBox = new JComboBox(days);
    yearTextField = new JTextField();
    yearTextField.setPreferredSize(new Dimension(40, (int) yearTextField.getPreferredSize().getHeight()));
    hourComboBox = new JComboBox(hours);
    minuteComboBox = new JComboBox(minutes);

    if (dateTime == null) {
        monthComboBox.setSelectedIndex(0);
        dayComboBox.setSelectedIndex(0);
        yearTextField.setText(null);
        hourComboBox.setSelectedIndex(0);
        minuteComboBox.setSelectedIndex(0);
    } else {
        monthComboBox.setSelectedIndex(dateTime.getMonthOfYear());
        dayComboBox.setSelectedIndex(dateTime.getDayOfMonth());
        yearTextField.setText(String.valueOf(dateTime.getYear()));
        hourComboBox.setSelectedIndex(dateTime.getHourOfDay());
        minuteComboBox.setSelectedIndex(dateTime.getMinuteOfHour());
    }

    // Labels to indicate what each field is for.
    JLabel monthLabel = new JLabel("Month");
    JLabel dayLabel = new JLabel("Day");
    JLabel yearLabel = new JLabel("Year");
    JLabel hourLabel = new JLabel("Hour");
    JLabel minuteLabel = new JLabel("Minute");

    // Main panel.
    final JPanel dateTimePanel = new JPanel(new SpringLayout());
    dateTimePanel.add(monthLabel);
    dateTimePanel.add(dayLabel);
    dateTimePanel.add(yearLabel);
    dateTimePanel.add(hourLabel);
    dateTimePanel.add(minuteLabel);
    dateTimePanel.add(monthComboBox);
    dateTimePanel.add(dayComboBox);
    dateTimePanel.add(yearTextField);
    dateTimePanel.add(hourComboBox);
    dateTimePanel.add(minuteComboBox);

    final ActionListener validateListener = new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            validateFields();
        }
    };

    // Add listeners to the date time fields which enable or disable
    // the OK button.
    monthComboBox.addActionListener(validateListener);
    dayComboBox.addActionListener(validateListener);
    yearTextField.addActionListener(validateListener);
    hourComboBox.addActionListener(validateListener);
    dayComboBox.addActionListener(validateListener);

    // Reposition components for a better look.
    SpringUtilities.makeCompactGrid(dateTimePanel, 2, 5, 10, 10, 10, 10);

    // Create buttons.
    cancelButton = new JButton("Cancel");
    okButton = new JButton("OK");
    JButton resetButton = new JButton(new AbstractAction("Reset") {

        @Override
        public void actionPerformed(ActionEvent e) {
            resetFields();
        }
    });

    // Add buttons to panel.
    final JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.LINE_AXIS));
    buttonPanel.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
    buttonPanel.add(Box.createHorizontalGlue());
    buttonPanel.add(resetButton);
    buttonPanel.add(Box.createRigidArea(new Dimension(10, 0)));
    buttonPanel.add(cancelButton);
    buttonPanel.add(Box.createRigidArea(new Dimension(10, 0)));
    buttonPanel.add(okButton);

    getContentPane().add(dateTimePanel, BorderLayout.CENTER);
    getContentPane().add(buttonPanel, BorderLayout.PAGE_END);

    // Set fields to the date time that was passed into the constructor.
    resetFields();
}

From source file:calculadora.CalculadoraDeHoras.java

License:Apache License

public Tempo quantidadeDeHoras(DateTime dataInicial, DateTime dataFinal, Janela janela) {

    long horasTrabalhadas = 0;

    int minutosDaHoraInicial = 0;
    int minutosDaHoraFinal = 0;

    if (forDiferente(dataInicial, dataFinal)) {
        minutosDaHoraInicial = tratarMinutosIniciais(dataInicial, janela);
        minutosDaHoraFinal = tratarMinutosFinais(dataFinal, janela);
    } else {//  w  w  w.j a v  a 2  s . c o  m
        minutosDaHoraInicial = dataFinal.getMinuteOfHour();
        minutosDaHoraFinal = dataInicial.getMinuteOfHour() * -1;
    }

    dataInicial = dataInicial.plusMinutes(minutosDaHoraInicial);

    while (forDiferente(dataInicial, dataFinal) && dataInicial.isBefore(dataFinal)) {

        if (janela.estaDentro(dataInicial)) {
            horasTrabalhadas++;
        }

        dataInicial = dataInicial.plusHours(1);

    }

    return criarTempo(horasTrabalhadas, minutosDaHoraInicial, minutosDaHoraFinal);
}

From source file:calculadora.CalculadoraDeHoras.java

License:Apache License

private int tratarMinutosFinais(DateTime finall, Janela janela) {
    int minutosFinais = finall.getMinuteOfHour();
    if (!janela.estaDentro(finall)) {
        minutosFinais = 0;// ww  w.  j a  v  a  2 s. c  om
    }
    return minutosFinais;
}

From source file:calculadora.CalculadoraDeHoras.java

License:Apache License

private int tratarMinutosIniciais(DateTime inicial, Janela janela) {
    int minutosIniciais = inicial.getMinuteOfHour();
    if (!janela.estaDentro(inicial)) {
        minutosIniciais = 0;/*from  w w  w. ja v  a2 s.c om*/
    } else {
        if (minutosIniciais > 0)
            minutosIniciais = 60 - minutosIniciais;
    }
    return minutosIniciais;
}

From source file:cd.education.data.collector.android.widgets.DateTimeWidget.java

License:Apache License

private void setAnswer() {

    if (mPrompt.getAnswerValue() != null) {

        DateTime ldt = new DateTime(((Date) ((DateTimeData) mPrompt.getAnswerValue()).getValue()).getTime());
        mDatePicker.init(ldt.getYear(), ldt.getMonthOfYear() - 1, ldt.getDayOfMonth(), mDateListener);
        mTimePicker.setCurrentHour(ldt.getHourOfDay());
        mTimePicker.setCurrentMinute(ldt.getMinuteOfHour());

    } else {/*www  . j a v a  2s.c  om*/
        // create time widget with current time as of right now
        clearAnswer();
    }
}

From source file:cd.education.data.collector.android.widgets.DateTimeWidget.java

License:Apache License

/**
 * Resets date to today.//  w  ww  .  j  ava 2  s  .  c  o m
 */
@Override
public void clearAnswer() {
    DateTime ldt = new DateTime();
    mDatePicker.init(ldt.getYear(), ldt.getMonthOfYear() - 1, ldt.getDayOfMonth(), mDateListener);
    mTimePicker.setCurrentHour(ldt.getHourOfDay());
    mTimePicker.setCurrentMinute(ldt.getMinuteOfHour());
}

From source file:cd.education.data.collector.android.widgets.TimeWidget.java

License:Apache License

public TimeWidget(Context context, final FormEntryPrompt prompt) {
    super(context, prompt);

    mTimePicker = new TimePicker(getContext());
    mTimePicker.setId(QuestionWidget.newUniqueId());
    mTimePicker.setFocusable(!prompt.isReadOnly());
    mTimePicker.setEnabled(!prompt.isReadOnly());

    String clockType = android.provider.Settings.System.getString(context.getContentResolver(),
            android.provider.Settings.System.TIME_12_24);
    if (clockType == null || clockType.equalsIgnoreCase("24")) {
        mTimePicker.setIs24HourView(true);
    }//from   w  ww . j av a  2  s .  c  om

    // If there's an answer, use it.
    if (prompt.getAnswerValue() != null) {

        // create a new date time from date object using default time zone
        DateTime ldt = new DateTime(((Date) ((TimeData) prompt.getAnswerValue()).getValue()).getTime());
        System.out.println("retrieving:" + ldt);

        mTimePicker.setCurrentHour(ldt.getHourOfDay());
        mTimePicker.setCurrentMinute(ldt.getMinuteOfHour());

    } else {
        // create time widget with current time as of right now
        clearAnswer();
    }

    mTimePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
        @Override
        public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
            Collect.getInstance().getActivityLogger().logInstanceAction(TimeWidget.this, "onTimeChanged",
                    String.format("%1$02d:%2$02d", hourOfDay, minute), mPrompt.getIndex());
        }
    });

    setGravity(Gravity.LEFT);
    addView(mTimePicker);

}

From source file:cd.education.data.collector.android.widgets.TimeWidget.java

License:Apache License

/**
 * Resets time to today.//  www . j a  v a2 s.c  om
 */
@Override
public void clearAnswer() {
    DateTime ldt = new DateTime();
    mTimePicker.setCurrentHour(ldt.getHourOfDay());
    mTimePicker.setCurrentMinute(ldt.getMinuteOfHour());
}