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

/**
 * Decode a 16-bit encoded DOS date/time into a java date/time.
 * //from  w w  w  . j av a 2 s. c  o  m
 * @param dosDate
 * @param dosTime
 * @return long
 */
public static long decodeDateTime(int dosDate, int dosTime) {
    final Calendar cal = Calendar.getInstance();

    cal.set(Calendar.MILLISECOND, 0);
    cal.set(Calendar.SECOND, (dosTime & 0x1f) * 2);
    cal.set(Calendar.MINUTE, (dosTime >> 5) & 0x3f);
    cal.set(Calendar.HOUR_OF_DAY, dosTime >> 11);

    cal.set(Calendar.DATE, dosDate & 0x1f);
    cal.set(Calendar.MONTH, ((dosDate >> 5) & 0x0f) - 1);
    cal.set(Calendar.YEAR, 1980 + (dosDate >> 9));

    return cal.getTimeInMillis();
}

From source file:Main.java

/**
 * Returns the month and release date of the movie
 *
 * @param releaseDate release date in yyyy-MM-dd format
 * @return month and year/*from w  w  w.  j av  a 2 s .  co m*/
 */
@NonNull
public static String getDisplayReleaseDate(String releaseDate) {
    if (TextUtils.isEmpty(releaseDate)) {
        return "";
    }
    try {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(DATE_FORMAT.parse(releaseDate));
        return calendar.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.ENGLISH) + " "
                + calendar.get(Calendar.YEAR);
    } catch (ParseException e) {
        return "";
    }
}

From source file:Main.java

/**
 * Using the expiry date, generate the application end date in the correct format.
 *
 * @param expiryMonth Month of expiry./*from w  w  w .jav  a  2 s  . c  o  m*/
 * @param expiryYear  Year of expiry.
 * @return The application end date in the correct format.
 */
private static String generateExpiryDate(String expiryMonth, String expiryYear) {
    // Just combine the year and month
    String applicationEndDate = expiryYear + expiryMonth;

    // Now need to work out the last day of the month
    // NB: Calendar's months range from 0-11 rather than 1-12
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.YEAR, Integer.parseInt(expiryYear));
    calendar.set(Calendar.MONTH, Integer.parseInt(expiryMonth) - 1);
    applicationEndDate += calendar.getActualMaximum(Calendar.DATE);

    return applicationEndDate;
}

From source file:DateUtils.java

/**
 * <p>Checks if two calendars represent the same day ignoring time.</p>
 * @param cal1  the first calendar, not altered, not null
 * @param cal2  the second calendar, not altered, not null
 * @return true if they represent the same day
 * @throws IllegalArgumentException if either calendar is <code>null</code>
 *///  ww w.  j  a  v  a 2  s .  com
public static boolean isSameDay(Calendar cal1, Calendar cal2) {
    if (cal1 == null || cal2 == null) {
        throw new IllegalArgumentException("The dates must not be null");
    }
    return (cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA)
            && cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
            && cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR));
}

From source file:Main.java

/**
 *
 * This method is a utility method to create a new java.sql.Date in one line.
 *
 * @param year//from  w w w.j  av  a  2s.c o m
 * @param month
 * @param day
 *
 * @return a populated java.sql.Date with the year, month, and day specified, and no values for hour, minute, second,
 *         millisecond
 *
 */
public static java.sql.Date newDate(Integer year, Integer month, Integer day) {

    // test for null arguments
    if (year == null) {
        throw new IllegalArgumentException("Argument 'year' passed in was null.");
    }
    if (month == null) {
        throw new IllegalArgumentException("Argument 'month' passed in was null.");
    }
    if (day == null) {
        throw new IllegalArgumentException("Argument 'day' passed in was null.");
    }

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.YEAR, year);
    calendar.set(Calendar.MONTH, month);
    calendar.set(Calendar.DAY_OF_MONTH, day);
    calendar.clear(Calendar.HOUR_OF_DAY);
    calendar.clear(Calendar.MINUTE);
    calendar.clear(Calendar.SECOND);
    calendar.clear(Calendar.MILLISECOND);

    return new java.sql.Date(calendar.getTimeInMillis());
}

From source file:DateParser.java

private static Calendar getCalendar(String isodate) {
    // YYYY-MM-DDThh:mm:ss.sTZD
    StringTokenizer st = new StringTokenizer(isodate, "-T:.+Z", true);

    Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
    calendar.clear();/*from  w  w  w . j  av  a2  s .co  m*/
    try {
        // Year
        if (st.hasMoreTokens()) {
            int year = Integer.parseInt(st.nextToken());
            calendar.set(Calendar.YEAR, year);
        } else {
            return calendar;
        }
        // Month
        if (check(st, "-") && (st.hasMoreTokens())) {
            int month = Integer.parseInt(st.nextToken()) - 1;
            calendar.set(Calendar.MONTH, month);
        } else {
            return calendar;
        }
        // Day
        if (check(st, "-") && (st.hasMoreTokens())) {
            int day = Integer.parseInt(st.nextToken());
            calendar.set(Calendar.DAY_OF_MONTH, day);
        } else {
            return calendar;
        }
        // Hour
        if (check(st, "T") && (st.hasMoreTokens())) {
            int hour = Integer.parseInt(st.nextToken());
            calendar.set(Calendar.HOUR_OF_DAY, hour);
        } else {
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);
            return calendar;
        }
        // Minutes
        if (check(st, ":") && (st.hasMoreTokens())) {
            int minutes = Integer.parseInt(st.nextToken());
            calendar.set(Calendar.MINUTE, minutes);
        } else {
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);
            return calendar;
        }

        //
        // Not mandatory now
        //

        // Secondes
        if (!st.hasMoreTokens()) {
            return calendar;
        }
        String tok = st.nextToken();
        if (tok.equals(":")) { // secondes
            if (st.hasMoreTokens()) {
                int secondes = Integer.parseInt(st.nextToken());
                calendar.set(Calendar.SECOND, secondes);
                if (!st.hasMoreTokens()) {
                    return calendar;
                }
                // frac sec
                tok = st.nextToken();
                if (tok.equals(".")) {
                    // bug fixed, thx to Martin Bottcher
                    String nt = st.nextToken();
                    while (nt.length() < 3) {
                        nt += "0";
                    }
                    nt = nt.substring(0, 3); // Cut trailing chars..
                    int millisec = Integer.parseInt(nt);
                    // int millisec = Integer.parseInt(st.nextToken()) * 10;
                    calendar.set(Calendar.MILLISECOND, millisec);
                    if (!st.hasMoreTokens()) {
                        return calendar;
                    }
                    tok = st.nextToken();
                } else {
                    calendar.set(Calendar.MILLISECOND, 0);
                }
            } else {
                throw new RuntimeException("No secondes specified");
            }
        } else {
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);
        }
        // Timezone
        if (!tok.equals("Z")) { // UTC
            if (!(tok.equals("+") || tok.equals("-"))) {
                throw new RuntimeException("only Z, + or - allowed");
            }
            boolean plus = tok.equals("+");
            if (!st.hasMoreTokens()) {
                throw new RuntimeException("Missing hour field");
            }
            int tzhour = Integer.parseInt(st.nextToken());
            int tzmin = 0;
            if (check(st, ":") && (st.hasMoreTokens())) {
                tzmin = Integer.parseInt(st.nextToken());
            } else {
                throw new RuntimeException("Missing minute field");
            }
            if (plus) {
                calendar.add(Calendar.HOUR, -tzhour);
                calendar.add(Calendar.MINUTE, -tzmin);
            } else {
                calendar.add(Calendar.HOUR, tzhour);
                calendar.add(Calendar.MINUTE, tzmin);
            }
        }
    } catch (NumberFormatException ex) {
        throw new RuntimeException("[" + ex.getMessage() + "] is not an integer");
    }
    return calendar;
}

From source file:com.project.framework.util.DateUtils.java

/**
 * ????//  www  . j  a va 2 s. c  o  m
 * 
 * @param start
 *            
 * @param end
 *            
 * @return start<end0start=endstart>end
 */
public static int compare(Date start, Date end) {
    Calendar c1 = Calendar.getInstance();
    c1.setTime(start);
    Calendar c2 = Calendar.getInstance();
    c2.setTime(end);
    int year1 = c1.get(Calendar.YEAR);
    int year2 = c2.get(Calendar.YEAR);
    if (year1 != year2) {
        return year1 - year2;
    }
    int month1 = c1.get(Calendar.MONTH);
    int month2 = c2.get(Calendar.MONTH);
    if (month1 != month2) {
        return month1 - month2;
    }
    int day1 = c1.get(Calendar.DAY_OF_MONTH);
    int day2 = c2.get(Calendar.DAY_OF_MONTH);
    return day1 - day2;
}

From source file:com.westlinkin.android_feedback_helper.utils.MailUntils.java

private static String getFeedbackNumber() {
    Calendar calendar = Calendar.getInstance();
    return "" + calendar.get(Calendar.YEAR)
            + Utils.formatCalendarIntValue((calendar.get(Calendar.MONTH) + 1), 2)
            + Utils.formatCalendarIntValue(calendar.get(Calendar.DAY_OF_MONTH), 2) + "-"
            + Utils.formatCalendarIntValue(calendar.get(Calendar.HOUR_OF_DAY), 2)
            + Utils.formatCalendarIntValue(calendar.get(Calendar.MINUTE), 2)
            + Utils.formatCalendarIntValue(calendar.get(Calendar.SECOND), 2) + "-"
            + Utils.formatCalendarIntValue(calendar.get(Calendar.MILLISECOND), 3);
}

From source file:Main.java

/**
 * Try to parse input with SimpleDateFormat
 *
 * @param input input.//  w  w w. jav  a 2s. c  o m
 * @param format      SimpleDateFormat
 * @param setYear1700 When true the age will be not displayed in brackets
 * @param locale locale.
 * @return Date object if successful, otherwise null
 */
private static Date parseStringWithSimpleDateFormat(String input, String format, boolean setYear1700,
        Locale locale) {
    SimpleDateFormat dateFormat = new SimpleDateFormat(format, locale);
    dateFormat.setTimeZone(TimeZone.getDefault());

    try {
        Date parsedDate = dateFormat.parse(input);

        /*
         * Because no year is defined in address book, set year to 1700
         *
         * When year < 1800, the age will be not displayed in brackets
         */
        if (setYear1700) {
            Calendar cal = Calendar.getInstance();
            cal.setTime(parsedDate);
            cal.set(Calendar.YEAR, 1700);
        }

        return parsedDate;
    } catch (ParseException ignored) {
        return null;
    }
}

From source file:com.jennifer.ui.util.TimeUtil.java

public static Date add(Date d, String type, int interval) {
    Calendar c = Calendar.getInstance();
    c.setTime(d);// w ww  . j a  va  2  s . c  om

    if (Time.YEARS.equals(type)) {
        c.add(Calendar.YEAR, interval);
    } else if (Time.MONTHS.equals(type)) {
        c.add(Calendar.MONTH, interval);
    } else if (Time.DAYS.equals(type)) {
        c.add(Calendar.DATE, interval);
    } else if (Time.HOURS.equals(type)) {
        c.add(Calendar.HOUR_OF_DAY, interval);
    } else if (Time.MINUTES.equals(type)) {
        c.add(Calendar.MINUTE, interval);
    } else if (Time.SECONDS.equals(type)) {
        c.add(Calendar.SECOND, interval);
    } else if (Time.MILLISECONDS.equals(type)) {
        c.add(Calendar.MILLISECOND, interval);
    } else if (Time.WEEKS.equals(type)) {
        c.add(Calendar.DATE, interval * 7);
    }

    return c.getTime();
}