Java Calendar Compare min(Calendar c1, Calendar c2)

Here you can find the source of min(Calendar c1, Calendar c2)

Description

min

License

Open Source License

Declaration

public static Calendar min(Calendar c1, Calendar c2) 

Method Source Code

//package com.java2s;

import java.util.Calendar;

import static java.util.Calendar.*;

public class Main {
    public static Calendar min(Calendar c1, Calendar c2) {
        Calendar result = Calendar.getInstance();
        result.setTimeInMillis(c2.getTimeInMillis());
        if (yearsDiff(c1, c2) > 1.0) {
            result.set(WEEK_OF_YEAR, 0);
            result.set(MONTH, 0);//  w w w . j  a  v a2s .c o  m
            result.set(DAY_OF_MONTH, 0);
            result.set(HOUR_OF_DAY, 0);
            result.set(MINUTE, 0);
            result.set(SECOND, 0);
            result.set(MILLISECOND, 0);
        } else if (weeksDiff(c1, c2) > 1.0) {
            result.set(HOUR_OF_DAY, 0);
            result.set(MINUTE, 0);
            result.set(SECOND, 0);
            result.set(MILLISECOND, 0);
        } else if (daysDiff(c1, c2) > 1.0) {
            result.set(HOUR_OF_DAY, 0);
            result.set(MINUTE, 0);
            result.set(SECOND, 0);
            result.set(MILLISECOND, 0);
            return result;
        } else if (hoursDiff(c1, c2) > 1.0) {
            result.set(MINUTE, 0);
            result.set(SECOND, 0);
            result.set(MILLISECOND, 0);
        } else if (minutesDiff(c1, c2) > 1.0) {
            result.set(SECOND, 0);
            result.set(MILLISECOND, 0);
        } else if (secondsDiff(c1, c2) > 1.0) {
            result.set(MILLISECOND, 0);
            result.add(SECOND, 1);
        }
        return result;
    }

    public static double yearsDiff(Calendar c1, Calendar c2) {
        return weeksDiff(c1, c2) / 52.0;
    }

    public static double weeksDiff(Calendar c1, Calendar c2) {
        return daysDiff(c1, c2) / 7.0;
    }

    public static double daysDiff(Calendar c1, Calendar c2) {
        return hoursDiff(c1, c2) / 24.0;
    }

    public static double hoursDiff(Calendar c1, Calendar c2) {
        return minutesDiff(c1, c2) / 60.0;
    }

    public static double minutesDiff(Calendar c1, Calendar c2) {
        return secondsDiff(c1, c2) / 60.0;
    }

    public static double secondsDiff(Calendar c1, Calendar c2) {
        return millisDiff(c1, c2) / 1000.0;
    }

    public static long millisDiff(Calendar c1, Calendar c2) {
        long time1 = c1.getTimeInMillis();
        long time2 = c2.getTimeInMillis();
        return Math.abs(time1 - time2);
    }
}

Related

  1. isSameInstant(Calendar cal1, Calendar cal2)
  2. isSameInstant(Calendar cal1, Calendar cal2)
  3. isSameInstant(Calendar cal1, Calendar cal2)
  4. isSameInstant(final Calendar cal1, final Calendar cal2)
  5. max(Calendar cal1, Calendar cal2)
  6. min(final Calendar... calendars)
  7. minCalendars(List calendars)
  8. minimum(Calendar... calendars)