Example usage for java.util Calendar YEAR

List of usage examples for java.util Calendar YEAR

Introduction

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

Prototype

int YEAR

To view the source code for java.util Calendar YEAR.

Click Source Link

Document

Field number for get and set indicating the year.

Usage

From source file:Main.java

/**
 * Takes a date value as used in CPLC Date fields (represented by 2 bytes)
 * /*from   w  ww . ja  v a2 s  . c  o m*/
 * @param paramByte1
 * @param paramByte2
 * @throws IllegalArgumentException
 * @return
 */
public static Date calculateCplcDate(byte[] dateBytes) throws IllegalArgumentException {
    if (dateBytes == null || dateBytes.length != 2) {
        throw new IllegalArgumentException("Error! CLCP Date values consist always of exactly 2 bytes");
    }
    // current time
    Calendar now = Calendar.getInstance();

    int year = now.get(Calendar.YEAR);
    int startYearOfCurrentDecade = year - (year % 10);

    int days = 100 * (dateBytes[0] & 0xF) + 10 * (0xF & dateBytes[1] >>> 4) + (dateBytes[1] & 0xF);

    if (days > 366) {
        throw new IllegalArgumentException("Invalid date (or are we parsing it wrong??)");
    }

    Calendar calculatedDate = Calendar.getInstance();
    calculatedDate.clear();
    calculatedDate.set(Calendar.YEAR, startYearOfCurrentDecade + (0xF & dateBytes[0] >>> 4));
    calculatedDate.set(Calendar.DAY_OF_YEAR, days);
    while (calculatedDate.after(now)) {
        calculatedDate.add(Calendar.YEAR, -10);
    }
    return calculatedDate.getTime();
}

From source file:Main.java

/**
 * get month between minTime and maxTime, including head and tail
 *
 * @param minTime//from   ww  w .  java  2 s.  co m
 * @param maxTime
 * @return
 */
public static int getMonthNum(long minTime, long maxTime) {
    Calendar min = Calendar.getInstance();
    min.setTimeInMillis(minTime);
    Calendar max = Calendar.getInstance();
    max.setTimeInMillis(maxTime);
    if (max.before(min)) {
        throw new IllegalArgumentException("max date is before min date");
    }
    int minMonth = min.get(Calendar.MONTH) + 1;
    int maxMonth = max.get(Calendar.MONTH) + 1;
    return (max.get(Calendar.YEAR) - min.get(Calendar.YEAR)) * 12 + maxMonth - minMonth + 1;
}

From source file:Main.java

public static String getAge(String fec_nac) {

    DateFormat df = new SimpleDateFormat("yyyy-mm-dd");
    Calendar dob = Calendar.getInstance();

    Date d = null;//from w w  w.ja v a2s .  co m
    try {
        d = df.parse(fec_nac);

    } catch (Exception e) {
    }

    Calendar today = Calendar.getInstance();
    dob.setTime(d);

    int age = today.get(Calendar.YEAR) - dob.get(Calendar.YEAR);

    if (today.get(Calendar.DAY_OF_YEAR) < dob.get(Calendar.DAY_OF_YEAR)) {
        age--;
    }
    return String.valueOf(age);
}

From source file:Main.java

/**
 * Get the age of the user. Takes in their birthday and calculates it according to today's date
 * @param birthday Date Object// ww  w.j  ava 2 s  .c om
 * @return Returns an int of their age (IE 20, 55, 18). If the date is in the future, it will
 *         return -1 instead.
 */
public static int getAge(Date birthday) {

    Calendar now = Calendar.getInstance();
    Calendar dob = Calendar.getInstance();
    dob.setTime(birthday);

    //First check for in the future:
    if (dob.after(now)) {
        return -1;
    }

    int year1 = now.get(Calendar.YEAR);
    int year2 = dob.get(Calendar.YEAR);

    int age = year1 - year2;

    int month1 = now.get(Calendar.MONTH);
    int month2 = dob.get(Calendar.MONTH);

    if (month2 > month1) {
        age--;

    } else if (month1 == month2) {
        int day1 = now.get(Calendar.DAY_OF_MONTH);
        int day2 = dob.get(Calendar.DAY_OF_MONTH);
        if (day2 > day1) {
            age--;
        }
    }

    return age;
}

From source file:Main.java

/**
 * Method to check if a date is this week. Note that it checks if it is this
 * week and not 7 days ahead!//from w  ww  . ja  v a2  s  . c om
 * 
 * @param theDate
 *            the date to see if it is this week
 * @return true if it is this week
 */
public static boolean isThisWeek(Date theDate) {
    // Get a calendar with the events start time
    GregorianCalendar theCalendar = new GregorianCalendar();
    theCalendar.setTime(theDate);
    // Get a calendar with current system time
    Calendar tmpDate = Calendar.getInstance();

    return tmpDate.get(Calendar.YEAR) == theCalendar.get(Calendar.YEAR)
            && tmpDate.get(Calendar.WEEK_OF_YEAR) == theCalendar.get(Calendar.WEEK_OF_YEAR);
}

From source file:Main.java

/** Helper method - creates a Phoenix date <I>String</I> from a Date object.
   @param dteValue <I>Date</I> object to be converted.
   @return A <I>String</I> that follows the format "YYYY-MM-DDTHH:NN:SS.00000".
 *///from  ww w .  j  a va  2s.  c o  m
public static String getXMLStringFromDate(Date dteValue) {
    if (null == dteValue)
        return null;

    Calendar pCalendar = Calendar.getInstance();

    pCalendar.setTime(dteValue);

    // Return the String value.
    // Special treatment for the month because the current implementation
    // values January as 0. Code below should work if that ever changes.
    return pCalendar.get(Calendar.YEAR) + "-" + padNumber(pCalendar.get(Calendar.MONTH) + 1 - Calendar.JANUARY)
            + "-" + padNumber(pCalendar.get(Calendar.DAY_OF_MONTH)) + "T"
            + padNumber(pCalendar.get(Calendar.HOUR_OF_DAY)) + ":" + padNumber(pCalendar.get(Calendar.MINUTE))
            + ":" + padNumber(pCalendar.get(Calendar.SECOND));
}

From source file:Main.java

/**
 * Calculates how many months are in given period. If one of the dates only partially covers the month, it still counts as full month.
 *
 * @param start Start of the period.//from ww w . j a  v  a  2 s .co  m
 * @param end   End of the period.
 * @return Number of days in given period. If {@code end < start}, returns -1.
 */
public static int getMonthCountInPeriod(long start, long end) {
    if (end < start)
        return -1;

    final Calendar cal = Calendar.getInstance();
    final int monthCountInYear = cal.getMaximum(Calendar.MONTH);

    cal.setTimeInMillis(start);
    final int startYear = cal.get(Calendar.YEAR);
    final int startMonth = cal.get(Calendar.MONTH) + 1;

    cal.setTimeInMillis(end);
    final int endYear = cal.get(Calendar.YEAR);
    final int endMonth = cal.get(Calendar.MONTH) + 1;

    int monthsCount;

    if (startYear != endYear) {
        monthsCount = monthCountInYear * Math.max(0, endYear - startYear - 1);
        monthsCount += monthCountInYear - startMonth + 1;
        monthsCount += endMonth;
    } else {
        monthsCount = endMonth - startMonth + 1;
    }

    return monthsCount;
}

From source file:Main.java

public static XMLGregorianCalendar dateToXmlCalendar(Date date) {
    if (date != null) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);//from w  w w  . j a  v  a  2s.  co m

        try {
            XMLGregorianCalendar xmlGregorianCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar();
            xmlGregorianCalendar.setDay(calendar.get(Calendar.DAY_OF_MONTH));
            xmlGregorianCalendar.setMonth(calendar.get(Calendar.MONTH) + 1);
            xmlGregorianCalendar.setYear(calendar.get(Calendar.YEAR));
            return xmlGregorianCalendar;
        } catch (DatatypeConfigurationException e) {
            return null;
        }
    }

    return null;
}

From source file:Main.java

public static XMLGregorianCalendar asXMLGregorianCalendar(java.util.Date date) {
    if (date == null) {
        return null;
    } else {//  w  ww  . j av a  2s .  c o m
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.setTime(date);

        XMLGregorianCalendar xmlGregorianCalendar = df.newXMLGregorianCalendar();
        xmlGregorianCalendar.setDay(calendar.get(Calendar.DAY_OF_MONTH));
        xmlGregorianCalendar.setMonth(calendar.get(Calendar.MONTH));
        xmlGregorianCalendar.setYear(calendar.get(Calendar.YEAR));

        return xmlGregorianCalendar;
    }
}

From source file:Main.java

public static Date parseData(String text) {
    Calendar calendar = Calendar.getInstance();
    if (text.contains(":")) {
        String[] time = text.split(":");
        int hours = Integer.parseInt(time[0]);
        calendar.set(Calendar.MINUTE, Integer.parseInt(time[1]));
        calendar.set(Calendar.HOUR_OF_DAY, hours);
        return calendar.getTime();
    } else if (text.contains("/")) {
        String[] date = text.split("/");
        if (date.length == 3) {
            calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(date[0]));
            calendar.set(Calendar.MONTH, Integer.parseInt(date[1]) - 1);
            calendar.set(Calendar.YEAR, Integer.parseInt(date[2]));
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            return calendar.getTime();
        } else if (date.length == 2) {
            calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(date[0]));
            calendar.set(Calendar.MONTH, Integer.parseInt(date[1]) - 1);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            return calendar.getTime();
        }//www .  ja va  2  s  .c o  m
    }
    return null;
}