Example usage for java.util Calendar get

List of usage examples for java.util Calendar get

Introduction

In this page you can find the example usage for java.util Calendar get.

Prototype

public int get(int field) 

Source Link

Document

Returns the value of the given calendar field.

Usage

From source file:Main.java

/**
 * @return Return current year (IE 2015, 2016)
 *///from   w  w w . ja v a  2s  . com
public static int getCurrentYear() {
    Calendar cal = Calendar.getInstance();
    int year = cal.get(Calendar.YEAR);
    return year;
}

From source file:Main.java

public static String getCurrentDateString() {
    Calendar date = Calendar.getInstance();
    int day = date.get(Calendar.DATE);
    int month = (date.get(Calendar.MONTH)) + 1;
    int year = date.get(Calendar.YEAR);
    String dateString = month + "-" + day + "-" + year;
    return dateString;
}

From source file:Main.java

public static Calendar getLastFriday(Calendar cal, int offset) {
    int dayofweek;
    cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) + offset);
    cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
    dayofweek = cal.get(Calendar.DAY_OF_WEEK);
    if (dayofweek < Calendar.FRIDAY)
        cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH) - 7 + Calendar.FRIDAY - dayofweek);
    else//ww  w. j a va  2s.co m
        cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH) + Calendar.FRIDAY - dayofweek);

    return cal;
}

From source file:Main.java

public static String getAbbrevDayOfWeekString(Calendar date) {
    return getAbbrevDayOfWeekString(date.get(Calendar.DAY_OF_WEEK));
}

From source file:Main.java

public static String getFirstDayOfLastMonth() {
    Calendar c = Calendar.getInstance();
    int month = c.get(Calendar.MONTH);
    c.set(Calendar.MONTH, month - 1);
    c.set(Calendar.DAY_OF_MONTH, c.getActualMinimum(Calendar.DAY_OF_MONTH));
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    return df.format(c.getTime());
}

From source file:Main.java

public static int getCurrentMouth() {
    Calendar cal = Calendar.getInstance();
    int year = cal.get(Calendar.YEAR);
    int mouth = cal.get(Calendar.MONTH);
    int day = cal.get(Calendar.DATE);
    int currentIndex = (year - 1970) * 12 + mouth;
    return currentIndex;
}

From source file:Main.java

public static int getAnioVigenciaPosition(String vigenciaAnio) {
    Calendar c = Calendar.getInstance();
    int anio = c.get(Calendar.YEAR);

    String anioInicial = String.valueOf(anio).substring(2, 4);
    int inicial = Integer.parseInt(anioInicial);
    int fin = Integer.parseInt(vigenciaAnio);

    return fin - inicial;
}

From source file:Main.java

public static int getDayOfTheWeek() {
    Calendar calendar = Calendar.getInstance();
    int day = calendar.get(Calendar.DAY_OF_WEEK);

    return day;//from   w  ww .  j a va 2 s .  co  m
}

From source file:Main.java

/**
 * @return Return current year (IE 2015, 2016)
 *///www . j a  v  a2s.  c o  m
public static int getCurrentMonth() {
    Calendar cal = Calendar.getInstance();
    int year = cal.get(Calendar.MONTH);
    year += 1; //January returns 0, December returns 11. Add one to return
    return year;
}

From source file:Main.java

public static String formatMediumDate(Context context, Calendar date) {
    return getAbbrevDayOfWeekString(date.get(Calendar.DAY_OF_WEEK)) + ", "
            + DateUtils.formatDateTime(context, date.getTimeInMillis(), DateUtils.FORMAT_NUMERIC_DATE);
}