Java Calendar Minute getMinutes(Calendar cal1, Calendar cal2)

Here you can find the source of getMinutes(Calendar cal1, Calendar cal2)

Description

get Minutes

License

Apache License

Declaration

public static int getMinutes(Calendar cal1, Calendar cal2) 

Method Source Code

//package com.java2s;
/*//from  ww w.ja v a  2s  .com
 * Copyright 2002-2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.Calendar;

import java.util.GregorianCalendar;

public class Main {
    public static final int SECONDS_60 = 60;
    public static final int MILLI_SECONDS_1000 = 1000;

    public static int getMinutes(Calendar cal1, Calendar cal2) {
        long utc1 = cal1.getTimeInMillis();
        long utc2 = cal2.getTimeInMillis();

        long result = (utc2 - utc1) / (SECONDS_60 * MILLI_SECONDS_1000);

        return (int) result;
    }

    public static int getMinutes(String date1, String date2) {
        Calendar cal1 = convertStringToCalender(date1);
        Calendar cal2 = convertStringToCalender(date2);

        return getMinutes(cal1, cal2);
    }

    public static Calendar convertStringToCalender(String str) {
        if ((str == null) || (str.length() < 14))
            return null;

        String year = str.substring(0, 4);
        String month = str.substring(4, 6);
        String day = str.substring(6, 8);
        String hour = str.substring(8, 10);
        String minute = str.substring(10, 12);
        String second = str.substring(12, 14);

        return (new GregorianCalendar(Integer.valueOf(year), Integer.valueOf(month) - 1, Integer.valueOf(day),
                Integer.valueOf(hour), Integer.valueOf(minute), Integer.valueOf(second)));
    }
}

Related

  1. calendarTodayOf(int hour, int minute, int second, int millis)
  2. clearHoursMinutesSecondsMillis(Calendar calendar)
  3. getFirstMinuteOfDay(Calendar dt)
  4. getMinute(Calendar cal)
  5. getMinutes(Calendar cal)
  6. getMinutes(String expression, Calendar time)
  7. getMinutesInterval(Calendar start, Calendar end)
  8. getOffsetInMinutes(Calendar cal)
  9. isNow(Calendar cal, Locale locale, boolean minuteCheck)