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

public static boolean isLeapYear(Date date) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);/* www .j av  a  2 s .  com*/
    int year = cal.get(Calendar.YEAR);
    return isLeapYear(year);
}

From source file:Main.java

/**
 * Returns the year (index 0), month (index 1) and day (index 2) of the given timestamp.
 * @param timestamp the timestamp/*from ww w  .  ja v  a  2  s.  co  m*/
 * @return the year (index 0), month (index 1) and day (index 2) of the given timestamp
 */
public static int[] getDayMonthYear(long timestamp) {
    GregorianCalendar gregorianCalendar = new GregorianCalendar();
    gregorianCalendar.setTimeInMillis(timestamp);

    int day = gregorianCalendar.get(Calendar.DAY_OF_MONTH);
    int month = gregorianCalendar.get(Calendar.MONTH) + 1;
    int year = gregorianCalendar.get(Calendar.YEAR);

    return new int[] { year, month, day };
}

From source file:Main.java

static void setDate(Calendar cal, int month, int date, boolean endOfDay) {
    cal.clear();//from ww w .j a  va2 s .c om
    cal.set(Calendar.YEAR, Calendar.getInstance().get(Calendar.YEAR));
    cal.set(Calendar.MONTH, month);
    cal.set(Calendar.DATE, date);
    if (endOfDay) {
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MILLISECOND, 999);
    } else {
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
    }
}

From source file:Main.java

public static int[] parseDate(String dateString) {
    try {//from  w w  w .  j  a  v a 2s.c  o  m
        DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
        formatter.setLenient(false);
        Date date = (Date) formatter.parse(dateString);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int[] dateInts = new int[3];
        dateInts[0] = cal.get(Calendar.DAY_OF_MONTH);
        dateInts[1] = cal.get(Calendar.MONTH);
        dateInts[2] = cal.get(Calendar.YEAR);

        return dateInts;
    } catch (Exception e) {
        throw new RuntimeException(
                "Could not parse date:'" + dateString + "'. Date format is dd-MM-yyyy (ex: 31-12-2011).", e);
    }
}

From source file:Main.java

public static String formatDateDiff(Calendar fromDate, Calendar toDate) {
    boolean future = false;
    if (toDate.equals(fromDate)) {
        return ("now");
    }/*  w  w  w.j  av  a2 s. c  om*/
    if (toDate.after(fromDate)) {
        future = true;
    }
    StringBuilder sb = new StringBuilder();
    int[] types = new int[] { Calendar.YEAR, Calendar.MONTH, Calendar.DAY_OF_MONTH, Calendar.HOUR_OF_DAY,
            Calendar.MINUTE, Calendar.SECOND };
    String[] names = new String[] { ("year"), ("years"), ("month"), ("months"), ("day"), ("days"), ("hour"),
            ("hours"), ("minute"), ("minutes"), ("second"), ("seconds") };
    int accuracy = 0;
    for (int i = 0; i < types.length; i++) {
        if (accuracy > 2) {
            break;
        }
        int diff = dateDiff(types[i], fromDate, toDate, future);
        if (diff > 0) {
            accuracy++;
            sb.append(" ").append(diff).append(" ").append(names[i * 2 + (diff > 1 ? 1 : 0)]);
        }
    }
    if (sb.length() == 0) {
        return "now";
    }
    return sb.toString().trim();
}

From source file:Main.java

public static String createJcFileName() {
    Calendar c = Calendar.getInstance();
    c.setTimeInMillis(System.currentTimeMillis());

    int y = c.get(Calendar.YEAR);
    int m = c.get(Calendar.MONTH);
    int d = c.get(Calendar.DAY_OF_MONTH);
    int hour = c.get(Calendar.HOUR_OF_DAY);
    int minute = c.get(Calendar.MINUTE);
    int second = c.get(Calendar.SECOND);
    return y + "-" + m + "-" + d + "-" + hour + "-" + minute + "-" + second;
}

From source file:Main.java

public static int monthsBetweenDates(String start, String end) throws ParseException {
    SimpleDateFormat format = new SimpleDateFormat("MMMM, yyyy", Locale.getDefault());
    Date startDate = format.parse(start);
    Date endDate = format.parse(end);

    Calendar startCalendar = new GregorianCalendar();
    startCalendar.setTime(startDate);//from  www.  j  a  v  a 2  s . co  m
    Calendar endCalendar = new GregorianCalendar();
    endCalendar.setTime(endDate);

    int diffYear = endCalendar.get(Calendar.YEAR) - startCalendar.get(Calendar.YEAR);
    return diffYear * 12 + endCalendar.get(Calendar.MONTH) - startCalendar.get(Calendar.MONTH);
}

From source file:Main.java

public static boolean isSameDay(Date date1, Date date2) {
    Calendar cal1 = Calendar.getInstance();
    Calendar cal2 = Calendar.getInstance();
    cal1.setTime(date1);/* ww  w  .java 2s .  c o m*/
    cal2.setTime(date2);
    boolean sameDay = cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
            && cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR);

    return sameDay;
}

From source file:Main.java

public static int[] getSchoolCalDate(int year, int month, int day, int week, int dayOfWeek) {
    Calendar cal = Calendar.getInstance();
    cal.set(year, month - 1, day);//from  w w  w . ja v a 2  s . c  om
    Log.d(TAG, "Date: " + cal.toString());
    Log.d(TAG, "year: " + year);
    Log.d(TAG, "month: " + month);
    Log.d(TAG, "day: " + day);
    cal.add(Calendar.DATE, -offset(cal.get(Calendar.DAY_OF_WEEK)));
    int count = (week - 1) * 7 + dayOfWeek - 1;
    Log.d(TAG, "count: " + count);
    cal.add(Calendar.DATE, count);
    int result[] = { cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + 1, cal.get(Calendar.DAY_OF_MONTH) };

    return result;
}

From source file:Main.java

public static String getMsgDate(long msg_time) {
    Calendar cl = Calendar.getInstance();
    cl.setTimeInMillis(msg_time); //here your time in miliseconds
    String date = "";

    String[] monthNames = { "January", "February", "March", "April", "May", "June", "July", "August",
            "September", "October", "November", "December" };

    SimpleDateFormat month_date = new SimpleDateFormat("MMM");
    String month_name = monthNames[cl.get(Calendar.MONTH)];

    int day = cl.get(Calendar.DAY_OF_MONTH);
    int year = cl.get(Calendar.YEAR);

    int current_year = Calendar.getInstance().get(Calendar.YEAR);
    if (current_year == year) {
        date = month_name + " " + day;
    } else {/*from  w w  w. ja v a  2s. c  o  m*/
        date = month_name + " " + day + ", " + year;
    }

    return date;
}