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 getYearFromDate(String origDate) {
    DateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
    Date date;//from  ww w  .ja  va  2  s  .co m
    int year = 1;
    try {
        date = formatter.parse(origDate);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        year = cal.get(Calendar.YEAR);
    } catch (ParseException e) {
        e.printStackTrace();
    }

    return year;
}

From source file:Main.java

/**
 * calculate day count/*from   w ww  .j  ava 2s .  c o m*/
 * @param startYear start year
 * @param startMonth start month
 * @param startDay start day
 * @param endYear end year
 * @param endMonth end month
 * @param endDay end day
 * @return day count
 */
public static int countDays(int startYear, int startMonth, int startDay, int endYear, int endMonth,
        int endDay) {
    Calendar startC = Calendar.getInstance();
    startC.set(Calendar.YEAR, startYear);
    startC.set(Calendar.MONTH, startMonth - 1);
    startC.set(Calendar.DAY_OF_MONTH, startDay);
    Calendar endC = Calendar.getInstance();
    endC.set(Calendar.YEAR, endYear);
    endC.set(Calendar.MONTH, endMonth - 1);
    endC.set(Calendar.DAY_OF_MONTH, endDay);
    return (int) ((endC.getTimeInMillis() - startC.getTimeInMillis()) / 86400000 + 1);
}

From source file:Main.java

public static Calendar getWallpaperDate(Context context) {
    Calendar wallpaperDate = Calendar.getInstance();

    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
    String dateTimeToString = preferences.getString("wallpaper_date", null);
    if (dateTimeToString == null)
        return wallpaperDate;

    wallpaperDate.set(Calendar.YEAR, Integer.parseInt(dateTimeToString.split("/")[0]));
    wallpaperDate.set(Calendar.MONTH, Integer.parseInt(dateTimeToString.split("/")[1]));
    wallpaperDate.set(Calendar.DATE, Integer.parseInt(dateTimeToString.split("/")[2]));

    return wallpaperDate;
}

From source file:Main.java

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

From source file:Main.java

public static boolean isCardExpired(String expiry, Date referenceDate) {
    boolean expired = false;

    int monthExpiry = Integer.parseInt(expiry.substring(0, 2));
    int yearExpiry = 2000 + Integer.parseInt(expiry.substring(2, 4));

    Calendar c = Calendar.getInstance();
    c.setTime(referenceDate);/* w w  w.j  av  a  2s . com*/

    int monthRef = c.get(Calendar.MONTH) + 1; // months indexed from 0 in java
    int yearRef = c.get(Calendar.YEAR);

    if (yearExpiry < yearRef) {
        expired = true;
    } else if (yearExpiry == yearRef && monthExpiry < monthRef) {
        expired = true;
    }

    return expired;
}

From source file:Main.java

public static String getCurrentTimeStampDD_MM_YYYY() {
    Calendar cal = new GregorianCalendar();
    return makeTimeString(cal.get(Calendar.DAY_OF_MONTH), 2) + "."
            + makeTimeString(cal.get(Calendar.MONTH) + 1, 2) + "." + makeTimeString(cal.get(Calendar.YEAR), 4);
}

From source file:Main.java

/**
 * if two date is the same day, for example ,1999-11-21 00:00:00 and  1999-11-21 23:15:40 are in same day,
 * but 1999-11-21 00:00:00 and 2010-11-21 00:00:00 are not
 *
 * @param ltime/*from   ww  w.j  a v  a  2s  .co  m*/
 * @param rtime
 * @return
 */
public static boolean sameDay(long ltime, long rtime) {
    Calendar l = Calendar.getInstance();
    Calendar r = Calendar.getInstance();
    l.setTimeInMillis(ltime);
    r.setTimeInMillis(rtime);

    return l.get(Calendar.YEAR) == r.get(Calendar.YEAR) && l.get(Calendar.MONTH) == r.get(Calendar.MONTH)
            && l.get(Calendar.DAY_OF_MONTH) == r.get(Calendar.DAY_OF_MONTH);
}

From source file:Main.java

/**
 * @param period : today, this_week, this_month, this_year, none
 * @return a date filter string for making query. e.g. date >= 2013-1-1 for this_year
 * return null if period is none.// w w  w .ja  v  a2  s .  c om
 */
@SuppressWarnings("deprecation")
public static String getDateFilterString(String period) {
    if (period == null)
        return null;
    Calendar calendar = Calendar.getInstance();
    int year = calendar.get(Calendar.YEAR);
    int month = calendar.get(Calendar.MONTH) + 1; // 1 - 12 
    int date = calendar.get(Calendar.DATE); // 1 - 31

    String dateFilterString = null;

    if (period.equalsIgnoreCase("today")) {
        dateFilterString = "date >= " + year + "-" + month + "-" + date;
    } else if (period.contains("week")) {
        int offset = calendar.get(Calendar.DAY_OF_WEEK) - calendar.getFirstDayOfWeek();
        long timeInMillis = calendar.getTimeInMillis() - offset * 24 * 60 * 60 * 1000;
        Date firstDay = new Date(timeInMillis);
        dateFilterString = "date >= " + (firstDay.getYear() + 1900) + "-" + firstDay.getMonth() + "-"
                + firstDay.getDate();
    } else if (period.contains("month")) {
        dateFilterString = "date >= " + year + "-" + month + "-1";
    } else if (period.contains("year")) {
        dateFilterString = "date >= " + year + "-1-1";
    }

    return dateFilterString;
}

From source file:Main.java

public static String date2String(Date date, boolean printTime) {
    Calendar cal = new GregorianCalendar();
    cal.setTime(date);/*from   w ww. jav a  2s . c  o m*/
    if (cal.get(Calendar.YEAR) > 10000) {
        return "---";
    }
    return makeTimeString(cal.get(Calendar.DAY_OF_MONTH), 2) + "."
            + makeTimeString(cal.get(Calendar.MONTH) + 1, 2) + "." + makeTimeString(cal.get(Calendar.YEAR), 4)
            + (printTime
                    ? " " + makeTimeString(cal.get(Calendar.HOUR_OF_DAY), 2) + ":"
                            + makeTimeString(cal.get(Calendar.MINUTE), 2) + ":"
                            + makeTimeString(cal.get(Calendar.SECOND), 2)
                    : "");
}

From source file:Main.java

public static long formatTimeString(String timeString) {
    long time = 0;
    if (timeString == null || timeString.length() < 1) {
        return time;
    }//from www  . j  ava  2 s  . c  o  m
    try {
        String[] array = timeString.split(DASH);
        int year = Integer.valueOf(array[0]);
        int month = Integer.valueOf(array[1]);
        int day = Integer.valueOf(array[2]);

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month);
        calendar.set(Calendar.DAY_OF_MONTH, day);

        time = calendar.getTimeInMillis();

    } catch (Exception e) {
        e.printStackTrace();
    }

    return time;
}