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

/**
 * Check if date1 is after date2 or not//from   w w  w.  j a va2 s .c o m
 * @return true if date1 after date 2, otherwise false.
 */
public static boolean isBefore(Date date1, Date date2) {
    Calendar cal1 = Calendar.getInstance();
    cal1.setTime(date1);

    Calendar cal2 = Calendar.getInstance();
    cal2.setTime(date2);

    if (cal1.get(Calendar.YEAR) < cal2.get(Calendar.YEAR)) {
        return true;
    } else if (cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
            && cal1.get(Calendar.MONTH) < cal2.get(Calendar.MONTH)) {
        return true;
    } else if (cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
            && cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH)
            && cal1.get(Calendar.DAY_OF_MONTH) < cal2.get(Calendar.DAY_OF_MONTH)) {
        return true;
    }

    return false;
}

From source file:Main.java

public static String getCurrent() {

    Calendar calender = Calendar.getInstance();

    String minute = "";
    if (calender.get(Calendar.MINUTE) < 10) {
        minute = 0 + "" + calender.get(Calendar.MINUTE);
    } else {//from   w  w  w.  ja v  a  2s .  co m
        minute = "" + calender.get(Calendar.MINUTE);
    }

    String created = calender.get(Calendar.YEAR) % 100 + "/" + (calender.get(Calendar.MONTH) + 1) + "/"
            + calender.get(Calendar.DAY_OF_MONTH) + " " + calender.get(Calendar.HOUR_OF_DAY) + ":" + minute
            + "";
    return created;
}

From source file:Main.java

public static Date getDateTimeFrom(int hour, int minute) {
    Calendar calendar = Calendar.getInstance();

    calendar.set(Calendar.DAY_OF_MONTH, 0);
    calendar.set(Calendar.MONTH, 0);
    calendar.set(Calendar.YEAR, 0);
    calendar.set(Calendar.MINUTE, minute);
    calendar.set(Calendar.HOUR_OF_DAY, hour);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);

    Date ret = calendar.getTime();
    return ret;/*from   w  w w .j a  v  a 2s.  co m*/
}

From source file:Main.java

public static long CompareTimeDelta(String arrivalTime) {

    Calendar calendarToday = Calendar.getInstance();
    Calendar calendarTrainArrival = Calendar.getInstance();

    long millisecondsCurrent = calendarToday.getTimeInMillis();
    long millisecondsTrainArrival;

    int nowYear = calendarToday.get(Calendar.YEAR);
    int nowMonth = calendarToday.get(Calendar.MONTH);
    int nowDay = calendarToday.get(Calendar.DATE);

    int evalhr = 0;
    int evalmin = 0;
    int evalsec = 0;

    String delims = "[:]";
    String[] timesplit = arrivalTime.split(delims);

    evalhr = Integer.parseInt(timesplit[0]); //HR
    evalmin = Integer.parseInt(timesplit[1]); //MIN
    evalsec = Integer.parseInt(timesplit[2]); //SEC - VRE does not provide a schedule down to seconds, so ignore.

    calendarTrainArrival.set(nowYear, nowMonth, nowDay, evalhr, evalmin);
    millisecondsTrainArrival = calendarTrainArrival.getTimeInMillis();

    return millisecondsCurrent - millisecondsTrainArrival;
}

From source file:Main.java

public static String getCurrentTimeStampYYYY_MM_DD_HH_MM_SS() {
    Calendar cal = new GregorianCalendar();
    return makeTimeString(cal.get(Calendar.YEAR), 4) + "/" + makeTimeString(cal.get(Calendar.MONTH) + 1, 2)
            + "/" + makeTimeString(cal.get(Calendar.DAY_OF_MONTH), 2) + " "
            + makeTimeString(cal.get(Calendar.HOUR_OF_DAY), 2) + ":"
            + makeTimeString(cal.get(Calendar.MINUTE), 2) + ":" + makeTimeString(cal.get(Calendar.SECOND), 2);
}

From source file:Util.java

public static Date getSunday(Date today) {
    Calendar cal = Calendar.getInstance();

    cal.setTime(today);/*from  w w  w.  j  a  v  a  2 s  . co m*/

    int dow = cal.get(Calendar.DAY_OF_WEEK);

    while (dow != Calendar.SUNDAY) {
        int date = cal.get(Calendar.DATE);

        int month = cal.get(Calendar.MONTH);

        int year = cal.get(Calendar.YEAR);

        if (date == getMonthLastDate(month, year)) {

            if (month == Calendar.DECEMBER) {
                month = Calendar.JANUARY;

                cal.set(Calendar.YEAR, year + 1);
            } else {
                month++;
            }

            cal.set(Calendar.MONTH, month);

            date = 1;
        } else {
            date++;
        }

        cal.set(Calendar.DATE, date);

        dow = cal.get(Calendar.DAY_OF_WEEK);
    }

    return cal.getTime();
}

From source file:Main.java

public static String getToday() {
    Calendar calendar = Calendar.getInstance();
    int month = calendar.get(Calendar.MONTH) + 1;
    return calendar.get(Calendar.YEAR) + "-" + ((month < 10) ? "0" + month : "" + month) + "-"
            + calendar.get(Calendar.DAY_OF_MONTH);
}

From source file:Main.java

private static String formatPeriodDate(int daysPeriod) {
    Calendar now = Calendar.getInstance();
    Calendar period = Calendar.getInstance();
    period.add(Calendar.DAY_OF_MONTH, daysPeriod);
    SimpleDateFormat sdf = new SimpleDateFormat("MMMM", Locale.US);

    if (now.get(Calendar.YEAR) == period.get(Calendar.YEAR)) {
        if (now.get(Calendar.MONTH) == period.get(Calendar.MONTH)) {
            return period.get(Calendar.DAY_OF_MONTH) + "-" + now.get(Calendar.DAY_OF_MONTH) + " "
                    + sdf.format(now.getTime()) + ", " + now.get(Calendar.YEAR);
        }/*from w w  w  .j  av  a 2s  .  c om*/
        return period.get(Calendar.DAY_OF_MONTH) + " " + sdf.format(period.getTime()) + " - "
                + now.get(Calendar.DAY_OF_MONTH) + " " + sdf.format(now.getTime()) + ", "
                + now.get(Calendar.YEAR);
    }
    return period.get(Calendar.DAY_OF_MONTH) + " " + sdf.format(period.getTime()) + " "
            + period.get(Calendar.YEAR) + " - " + now.get(Calendar.DAY_OF_MONTH) + " "
            + sdf.format(now.getTime()) + " " + now.get(Calendar.YEAR);
}

From source file:Main.java

public static String getCurrentDateString(Calendar calendar) {
    if (calendar == null)
        return "";
    return calendar.get(Calendar.YEAR) + "-" + String.format("%02d", (calendar.get(Calendar.MONTH) + 1)) + "-"
            + String.format("%02d", calendar.get(Calendar.DATE));
}

From source file:Main.java

public static int getAge(String year, String month, String day) throws NumberFormatException {
    int yearInt = Integer.parseInt(year);
    int monthInt = Integer.parseInt(month);
    int dayInt = Integer.parseInt(day);
    monthInt--;//from   w  ww.j a  va 2  s.c o m
    Calendar birthCal = Calendar.getInstance();
    birthCal.set(yearInt, monthInt, dayInt);
    Calendar todayCal = Calendar.getInstance();

    int diff = todayCal.get(Calendar.YEAR) - birthCal.get(Calendar.YEAR);
    if (birthCal.get(Calendar.MONTH) > todayCal.get(Calendar.MONTH)
            || (birthCal.get(Calendar.MONTH) == todayCal.get(Calendar.MONTH)
                    && birthCal.get(Calendar.DATE) > todayCal.get(Calendar.DATE))) {
        diff--;
    }
    return diff;
}