向Calendar对象添加分钟

时间:2015-06-19 22:18:56

标签: java calendar

我目前正在尝试为日历对象添加一些时间。输入来自时间选择器,输出变为两个字符串。

    DateFormat dateFormat = new android.text.format.DateFormat();

    Calendar startTimeCalendar = Calendar.getInstance();

    //These two string will be like this: "08:15AM"
    int prevStartHour = Integer.parseInt(startTime.getText().subSequence(0, 2).toString());
    int prevStartMinute = Integer.parseInt(startTime.getText().subSequence(3, 5).toString());
    int prevEndHour = Integer.parseInt(endTime.getText().subSequence(0, 2).toString());
    int prevEndMinute = Integer.parseInt(endTime.getText().subSequence(3, 5).toString());

    //Get the difference in hours between two strings
    int hourChange = Math.abs(prevEndHour - prevStartHour);
    //Get the difference in minutes between two strings
    int minuteChange = Math.abs(prevEndMinute - prevStartMinute);

    //Set the date on the first label
    startTime.setText(dateFormat.format("hh:mmAA", startTimeCalendar).toString());
    //Add the difference in hours to the calendar
    startTimeCalendar.add(Calendar.HOUR_OF_DAY, hourChange);
    //Add the difference in minutes to the calendar
    startTimeCalendar.add(Calendar.MINUTE, minuteChange);
    //Set the date on the second label
    endTime.setText(dateFormat.format("hh:mmAA", startTimeCalendar).toString());

现在这是奇怪的部分...时间加上很好,花花公子,而分钟只是决定停止工作中途。而不是添加预期的时间(15,30或45分钟),它每次增加30分钟。

1 个答案:

答案 0 :(得分:0)

hh:mmAA更改为hh:mmaalink)。