Example usage for java.util Calendar get

List of usage examples for java.util Calendar get

Introduction

In this page you can find the example usage for java.util Calendar get.

Prototype

public int get(int field) 

Source Link

Document

Returns the value of the given calendar field.

Usage

From source file:Main.java

public static int calculateDayDiffOfSameYear(Date targetTime, Date compareTime) {
    if (targetTime == null || compareTime == null) {
        return 0;
    }/*  www  .j  a  v a  2  s .co m*/

    Calendar tarCalendar = Calendar.getInstance();
    tarCalendar.setTime(targetTime);
    int tarDayOfYear = tarCalendar.get(Calendar.DAY_OF_YEAR);

    Calendar compareCalendar = Calendar.getInstance();
    compareCalendar.setTime(compareTime);
    int comDayOfYear = compareCalendar.get(Calendar.DAY_OF_YEAR);

    return tarDayOfYear - comDayOfYear;
}

From source file:Main.java

static int daysBetween(Calendar day1, Calendar day2) {
    /**// w w w  . j a  v a  2  s  .c o m
     * Saved some effort using the solution described here,
     * http://stackoverflow.com/a/28865648/1587370
     */
    Calendar dayOne = (Calendar) day1.clone(), dayTwo = (Calendar) day2.clone();

    if (dayOne.get(Calendar.YEAR) == dayTwo.get(Calendar.YEAR)) {
        return Math.abs(dayOne.get(Calendar.DAY_OF_YEAR) - dayTwo.get(Calendar.DAY_OF_YEAR));
    } else {
        if (dayTwo.get(Calendar.YEAR) > dayOne.get(Calendar.YEAR)) {
            //swap them
            Calendar temp = dayOne;
            dayOne = dayTwo;
            dayTwo = temp;
        }
        int extraDays = 0;

        int dayOneOriginalYearDays = dayOne.get(Calendar.DAY_OF_YEAR);

        while (dayOne.get(Calendar.YEAR) > dayTwo.get(Calendar.YEAR)) {
            dayOne.add(Calendar.YEAR, -1);
            // getActualMaximum() important for leap years
            extraDays += dayOne.getActualMaximum(Calendar.DAY_OF_YEAR);
        }

        return extraDays - dayTwo.get(Calendar.DAY_OF_YEAR) + dayOneOriginalYearDays;
    }
}

From source file:Main.java

public static int getWeekOfYear(Date date) {
    Calendar c = Calendar.getInstance();
    c.setFirstDayOfWeek(Calendar.MONDAY);
    c.setTime(date);//  w w  w . j  a va2 s .  co m
    int week = c.get(Calendar.WEEK_OF_YEAR) - 1;
    week = week == 0 ? 52 : week;
    return week > 0 ? week : 1;
}

From source file:Main.java

public static int getDayNum(int index) {
    java.util.Calendar calendar = new java.util.GregorianCalendar();

    calendar.add(Calendar.DATE, index);//
    int dWeek = calendar.get(Calendar.DAY_OF_WEEK);
    int realDay = (dWeek - 1) == 0 ? 7 : (dWeek - 1);

    return realDay;
}

From source file:Main.java

public static String getTimeString(long millitm) {
    Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
    cal.setTimeInMillis(millitm);//from w  w  w  .  ja v a  2  s  .  co  m
    return (setZeroPad(cal.get(Calendar.HOUR)) + ":" + setZeroPad(cal.get(Calendar.MINUTE)) + ":"
            + setZeroPad(cal.get(Calendar.SECOND)));
}

From source file:eu.trentorise.smartcampus.mobility.processor.alerts.DelayChecker.java

private static String buildDate() {
    Calendar cal = Calendar.getInstance();
    return cal.get(Calendar.DAY_OF_MONTH) + "-" + cal.get(Calendar.MONTH) + "-" + cal.get(Calendar.YEAR);
}

From source file:Main.java

private static long betweenTime(int what, String t1, String t2, String... patterns) {
    if (patterns.length == 0) {
        throw new RuntimeException("must give me a pattern");
    }/*from www.j  av a 2  s .  c  om*/
    Date d1 = getDate(t1, patterns[0]);
    Date d2 = getDate(t2, patterns.length > 1 ? patterns[1] : patterns[0]);
    if (d1 != null && d2 != null) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(d1);
        int i1 = calendar.get(what);
        calendar.setTime(d2);
        int i2 = calendar.get(what);
        return i1 - i2;
    }
    return Long.MIN_VALUE;
}

From source file:Main.java

/**
 * Returns the day of the year (IE 365 = Dec 31st)
 * @param date This is the date to use to calculate the day of the year
 * @return Day - Day of the year based on date.
 *///from  w w  w  .  jav  a  2s.c o  m
public static Integer getCurrentDay(Date date) {
    Calendar cal = Calendar.getInstance();
    if (date != null)
        cal.setTime(date);
    return cal.get(Calendar.DAY_OF_YEAR);
}