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 int[] getTimeFields(long time) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(time);//from ww w .j  av  a2 s. c om
    int[] timeFields = new int[6];
    timeFields[0] = calendar.get(Calendar.YEAR);
    timeFields[1] = calendar.get(Calendar.MONTH);
    timeFields[2] = calendar.get(Calendar.DAY_OF_MONTH);
    timeFields[3] = calendar.get(Calendar.HOUR_OF_DAY);
    timeFields[4] = calendar.get(Calendar.MINUTE);
    timeFields[5] = calendar.get(Calendar.SECOND);
    return timeFields;
}

From source file:Main.java

public static boolean sameDate(@NonNull Date date1, @NonNull Date date2) {
    Calendar cal1 = Calendar.getInstance();
    Calendar cal2 = Calendar.getInstance();
    cal1.setTime(date1);//  ww w.  java  2  s .  com
    cal2.setTime(date2);
    return 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

public static boolean isTodayInCalendar(Calendar calendar) {
    return todayCalendar.get(Calendar.DAY_OF_MONTH) == calendar.get(Calendar.DAY_OF_MONTH)
            && todayCalendar.get(Calendar.MONTH) == calendar.get(Calendar.MONTH)
            && todayCalendar.get(Calendar.YEAR) == calendar.get(Calendar.YEAR);
}

From source file:Main.java

public static Date getFinanacialDate(Date taxdate) {

    Calendar calendar = Calendar.getInstance();
    calendar.setTime(taxdate);//from   w w w  .  j  av  a  2 s .  c o m
    int yr = calendar.get(Calendar.YEAR);
    calendar.set(yr, 00, 01);
    taxdate = calendar.getTime();

    return taxdate;
}

From source file:Main.java

private static final Calendar getUtcDate(Date date, boolean noYear) {
    final Calendar calendar = Calendar.getInstance(UTC_TIMEZONE, Locale.US);
    calendar.setTime(date);//from w ww.j a  v a 2  s .  c  o m
    if (noYear) {
        calendar.set(Calendar.YEAR, 0);
    }
    return calendar;
}

From source file:Main.java

public static Date getEndFinanacialDate(Date taxdate) {

    Calendar calendar = Calendar.getInstance();
    calendar.setTime(taxdate);/*from w  w w  .j av a  2 s  . c o  m*/
    int yr = calendar.get(Calendar.YEAR);
    calendar.set(yr, 11, 31);
    taxdate = calendar.getTime();

    return taxdate;
}

From source file:Main.java

/**
 * Compares only the month, day, and year of the provided calendars
 * @param date1 The calendar to compare against
 * @param date2 The calendar to compare//from  www  . j  av a 2  s  .  c o m
 * @return -1 if the first calendar is earlier, 1 if the second calendar is earlier, 0 otherwise
 */
public static int compareDates(Calendar date1, Calendar date2) {
    if (date1.get(Calendar.YEAR) < date2.get(Calendar.YEAR)) {
        return -1;
    } else if (date1.get(Calendar.YEAR) > date2.get(Calendar.YEAR)) {
        return 1;
    }
    // Years are equal
    if (date1.get(Calendar.MONTH) < date2.get(Calendar.MONTH)) {
        return -1;
    } else if (date1.get(Calendar.MONTH) > date2.get(Calendar.MONTH)) {
        return 1;
    }
    // Years and months are equal
    if (date1.get(Calendar.DAY_OF_MONTH) < date2.get(Calendar.DAY_OF_MONTH)) {
        return -1;
    } else if (date1.get(Calendar.DAY_OF_MONTH) > date2.get(Calendar.DAY_OF_MONTH)) {
        return 1;
    }
    return 0;
}

From source file:Main.java

/**
 * Formats a date/time value as a string.
 * //  w w  w  . ja  v  a2  s .c  om
 * @param date the date to format
 * @return the formatted date
 */
public static String formatDateTime(Calendar date) {
    StringBuilder builder = new StringBuilder(16);
    appendField(builder, date.get(Calendar.YEAR), 4);
    appendField(builder, date.get(Calendar.MONTH) + 1, 2);
    appendField(builder, date.get(Calendar.DAY_OF_MONTH), 2);
    appendField(builder, date.get(Calendar.HOUR_OF_DAY), 2);
    appendField(builder, date.get(Calendar.MINUTE), 2);
    appendField(builder, date.get(Calendar.SECOND), 2);
    return builder.toString();
}

From source file:Main.java

public static String getFormattedCommentCreatedAt(String date) {
    String[] arr = date.split("\\s");
    Calendar calendar = Calendar.getInstance();
    StringBuilder stringBuilder = new StringBuilder();
    String hours = arr[3].split(":")[0];
    String minutes = arr[3].split(":")[1];
    if (Integer.parseInt(arr[5]) != calendar.get(Calendar.YEAR)) {
        stringBuilder.append(arr[1]).append(' ').append(arr[2]).append(',').append(' ').append(arr[5]);
    } else {//from   ww w . j a v a2s .  c o  m
        stringBuilder.append(arr[1]).append(' ').append(arr[2]).append(" at ");
    }
    if (Integer.parseInt(hours) < 12) {
        stringBuilder.append(" at ").append(hours).append(':').append(minutes);
        stringBuilder.append(" AM ");
    } else {
        int hoursDigits = Integer.parseInt(hours);
        hoursDigits = hoursDigits - 12;
        hours = Integer.toString(hoursDigits);
        stringBuilder.append(" at ").append(hours).append(':').append(minutes);
        stringBuilder.append(" PM ");
    }
    return stringBuilder.toString();
}

From source file:Main.java

/**
 * Calculate the Julian Day for a given date using the following formula:
 * JD = 367 * Y - INT(7 * (Y + INT((M + 9)/12))/4) + INT(275 * M / 9)
 *      + D + 1721013.5 + UT/24/*w ww.  ja  v  a 2s . com*/
 *
 * Note that this is only valid for the year range 1900 - 2099.
 */
public static double calculateJulianDay(Date date) {
    Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
    cal.setTime(date);

    double hour = cal.get(Calendar.HOUR_OF_DAY) + cal.get(Calendar.MINUTE) / 60.0f
            + cal.get(Calendar.SECOND) / 3600.0f;

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

    double jd = 367.0 * year - Math.floor(7.0 * (year + Math.floor((month + 9.0) / 12.0)) / 4.0)
            + Math.floor(275.0 * month / 9.0) + day + 1721013.5 + hour / 24.0;
    return jd;
}