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:co.com.soinsoftware.altablero.controller.YearController.java

public String getCurrentYearString() {
    Calendar date = Calendar.getInstance();
    return String.valueOf(date.get(Calendar.YEAR));
}

From source file:com.worldline.easycukes.rest.utils.DateHelper.java

/**
 * Used to convert date value in json date format
 *
 * @param value/*from   w w  w  .java2 s .  c o m*/
 * @return
 */
public static String convertDateToJsonFormat(@NonNull final Date value) {
    log.info("setting the date value " + value + " to format json");
    final Calendar calendar = Calendar.getInstance();
    calendar.setTime(value);
    return calendar.get(Calendar.YEAR) + "-" + formatTo2Digit(calendar.get(Calendar.MONTH) + 1) + "-"
            + formatTo2Digit(calendar.get(Calendar.DAY_OF_MONTH)) + "T"
            + formatTo2Digit(calendar.get(Calendar.HOUR_OF_DAY)) + ":"
            + formatTo2Digit(calendar.get(Calendar.MINUTE)) + ":"
            + formatTo2Digit(calendar.get(Calendar.SECOND)) + ":" + calendar.get(Calendar.MILLISECOND) + "Z";
}

From source file:storybook.ui.chart.jfreechart.ChartUtil.java

@SuppressWarnings("unchecked")
public static TreeSet<Date> correctDates(TreeSet<Date> paramTreeSet) {
    TreeSet localTreeSet = new TreeSet();
    Date localDate1 = (Date) paramTreeSet.first();
    Calendar localCalendar1 = Calendar.getInstance();
    localCalendar1.setTime(localDate1);/* w  ww. j a v  a 2  s  . co  m*/
    int i = localCalendar1.get(Calendar.YEAR);
    if (i > 1900) {
        return paramTreeSet;
    }
    for (Date localDate2 : paramTreeSet) {
        Calendar localCalendar2 = Calendar.getInstance();
        localCalendar2.setTime(localDate2);
        int j = localCalendar2.get(Calendar.YEAR);
        localDate2 = DateUtils.addYears(localDate2, 1900 - j);
        localTreeSet.add(localDate2);
    }
    return localTreeSet;
}

From source file:com.bellman.bible.service.readingplan.ReadingPlanInfoDto.java

public void setStartToJan1() {
    Date jan1 = DateUtils.truncate(new Date(), Calendar.YEAR);

    startOn(jan1, true);
}

From source file:CalendarDemo.java

public void format() {

    // Tell the calendar what date/time to format
    calendar.setTime(timeNow);//from   w ww .j  av a2  s.  com

    // print out most of the known fields
    System.out.println("ERA: " + calendar.get(Calendar.ERA));
    System.out.println("YEAR: " + calendar.get(Calendar.YEAR));
    System.out.println("MONTH: " + calendar.get(Calendar.MONTH));
    System.out.println("WEEK_OF_YEAR: " + calendar.get(Calendar.WEEK_OF_YEAR));
    System.out.println("WEEK_OF_MONTH: " + calendar.get(Calendar.WEEK_OF_MONTH));
    System.out.println("DATE: " + calendar.get(Calendar.DATE));
    System.out.println("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH));
    System.out.println("DAY_OF_YEAR: " + calendar.get(Calendar.DAY_OF_YEAR));
    System.out.println("DAY_OF_WEEK: " + calendar.get(Calendar.DAY_OF_WEEK));
    System.out.println("DAY_OF_WEEK_IN_MONTH: " + calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));
    System.out.println("AM_PM: " + calendar.get(Calendar.AM_PM));
    System.out.println("HOUR: " + calendar.get(Calendar.HOUR));
    System.out.println("HOUR_OF_DAY: " + calendar.get(Calendar.HOUR_OF_DAY));
    System.out.println("MINUTE: " + calendar.get(Calendar.MINUTE));
    System.out.println("SECOND: " + calendar.get(Calendar.SECOND));
    System.out.println("MILLISECOND: " + calendar.get(Calendar.MILLISECOND));
    System.out.println("ZONE_OFFSET: " + (calendar.get(Calendar.ZONE_OFFSET) / (60 * 60 * 1000)));
    System.out.println("DST_OFFSET: " + (calendar.get(Calendar.DST_OFFSET) / (60 * 60 * 1000)));
}

From source file:Main.java

/**
 * Given a calendar (possibly containing only a day of the year), returns the earliest possible
 * anniversary of the date that is equal to or after the current point in time if the date
 * does not contain a year, or the date converted to the local time zone (if the date contains
 * a year.//from  ww  w .  ja  v  a 2  s .c  om
 *
 * @param target The date we wish to convert(in the UTC time zone).
 * @return If date does not contain a year (year < 1900), returns the next earliest anniversary
 * that is after the current point in time (in the local time zone). Otherwise, returns the
 * adjusted Date in the local time zone.
 */
public static Date getNextAnnualDate(Calendar target) {
    final Calendar today = Calendar.getInstance();
    today.setTime(new Date());

    // Round the current time to the exact start of today so that when we compare
    // today against the target date, both dates are set to exactly 0000H.
    today.set(Calendar.HOUR_OF_DAY, 0);
    today.set(Calendar.MINUTE, 0);
    today.set(Calendar.SECOND, 0);
    today.set(Calendar.MILLISECOND, 0);

    final boolean isYearSet = isYearSet(target);
    final int targetYear = target.get(Calendar.YEAR);
    final int targetMonth = target.get(Calendar.MONTH);
    final int targetDay = target.get(Calendar.DAY_OF_MONTH);
    final boolean isFeb29 = (targetMonth == Calendar.FEBRUARY && targetDay == 29);
    final GregorianCalendar anniversary = new GregorianCalendar();
    // Convert from the UTC date to the local date. Set the year to today's year if the
    // there is no provided year (targetYear < 1900)
    anniversary.set(!isYearSet ? today.get(Calendar.YEAR) : targetYear, targetMonth, targetDay);
    // If the anniversary's date is before the start of today and there is no year set,
    // increment the year by 1 so that the returned date is always equal to or greater than
    // today. If the day is a leap year, keep going until we get the next leap year anniversary
    // Otherwise if there is already a year set, simply return the exact date.
    if (!isYearSet) {
        int anniversaryYear = today.get(Calendar.YEAR);
        if (anniversary.before(today) || (isFeb29 && !anniversary.isLeapYear(anniversaryYear))) {
            // If the target date is not Feb 29, then set the anniversary to the next year.
            // Otherwise, keep going until we find the next leap year (this is not guaranteed
            // to be in 4 years time).
            do {
                anniversaryYear += 1;
            } while (isFeb29 && !anniversary.isLeapYear(anniversaryYear));
            anniversary.set(anniversaryYear, targetMonth, targetDay);
        }
    }
    return anniversary.getTime();
}

From source file:com.sammyun.util.DateUtil.java

/**
 * _?yyyy//from  www. java 2 s.c  o m
 * 
 * @return int
 */
public static int getNextYear() {
    return new GregorianCalendar().get(Calendar.YEAR) + 1;
}

From source file:Dates.java

/**
 * Returns the year (4 digits) of date. <o> 
 * /*from  www . j  a va 2s .co  m*/
 * If date is null returns 0.
 */
public static int getYear(Date date) {
    if (date == null)
        return 0;
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    return cal.get(Calendar.YEAR);
}

From source file:Main.java

/**
 * @param date/* ww  w . j a  v a2s.c  om*/
 * @return
 */
public static String encodeTime(Date date) {
    StringBuilder result = new StringBuilder("T_");
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    int month = cal.get(Calendar.MONTH) + 1;
    int year = cal.get(Calendar.YEAR);
    int day = cal.get(Calendar.DAY_OF_MONTH);
    int hour = cal.get(Calendar.HOUR_OF_DAY);
    int minute = cal.get(Calendar.MINUTE);
    int second = cal.get(Calendar.SECOND);
    //yyyy-MM-DD'T'kk:mm:ss
    result.append(year).append("-").append((month < 10) ? "0" : "").append(month).append("-")
            .append((day < 10) ? "0" : "").append(day).append("T").append((hour < 10) ? "0" : "").append(hour)
            .append(":").append((minute < 10) ? "0" : "").append(minute).append(":")
            .append((second < 10) ? "0" : "").append(second);
    return result.toString();
}

From source file:ezbake.services.provenance.graph.Utils.java

public static ezbake.base.thrift.DateTime convertDate2DateTime(final java.util.Date theDate) {
    final Calendar cal = new GregorianCalendar();
    cal.setTime(theDate);//  w  ww .j a va2s  . co  m

    // get calendar parts
    final int year = cal.get(Calendar.YEAR);
    final int month = cal.get(Calendar.MONTH) + 1;
    final int day = cal.get(Calendar.DAY_OF_MONTH);
    final int hour = cal.get(Calendar.HOUR_OF_DAY);
    final int minute = cal.get(Calendar.MINUTE);
    final int second = cal.get(Calendar.SECOND);
    int offsetMinutes = (cal.getTimeZone().getOffset(cal.getTimeInMillis())) / (1000 * 60);

    // set thrift DateTime propertiesd
    final ezbake.base.thrift.DateTime dt = new ezbake.base.thrift.DateTime();
    // Date
    final ezbake.base.thrift.Date date = new ezbake.base.thrift.Date();
    date.setMonth((short) month).setDay((short) day).setYear((short) year);
    dt.setDate(date);

    // Time with TimeZone
    final ezbake.base.thrift.Time t = new ezbake.base.thrift.Time();
    boolean afterUtc = offsetMinutes > 0;
    offsetMinutes = Math.abs(offsetMinutes);
    final ezbake.base.thrift.TimeZone tz = new ezbake.base.thrift.TimeZone((short) (offsetMinutes / 60),
            (short) (offsetMinutes % 60), afterUtc);
    t.setHour((short) hour).setMinute((short) minute).setSecond((short) second).setTz(tz);
    dt.setTime(t);

    return dt;
}