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

private static int getTimeByType(int type) {
    return Calendar.getInstance().get(type);
}

From source file:Main.java

public static boolean isSameYear(Date targetTime, Date compareTime) {
    if (targetTime == null || compareTime == null) {
        return false;
    }//ww  w  .j  ava  2  s . c  om

    Calendar tarCalendar = Calendar.getInstance();
    tarCalendar.setTime(targetTime);
    int tarYear = tarCalendar.get(Calendar.YEAR);

    Calendar compareCalendar = Calendar.getInstance();
    compareCalendar.setTime(compareTime);
    int comYear = compareCalendar.get(Calendar.YEAR);

    return tarYear == comYear;
}

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);// w w w  . j av a 2 s .  c o 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

/**
 * Method to check if a date is tomorrow
 * //  w w  w. j a v a  2  s .com
 * @param theDate
 *            the date to see if it is tomorrow
 * @return true of it is tomorrow
 */
public static boolean isTomorrow(Date theDate) {
    // Get a calendar with the events start time
    GregorianCalendar theCalendar = new GregorianCalendar();
    theCalendar.setTime(theDate);
    // Get a calendar with current system time and change its value so it
    // can be used in comparison with the given date.
    Calendar tmpDate = Calendar.getInstance();
    tmpDate.roll(Calendar.DAY_OF_YEAR, true);

    return tmpDate.get(Calendar.YEAR) == theCalendar.get(Calendar.YEAR)
            && tmpDate.get(Calendar.DAY_OF_YEAR) == theCalendar.get(Calendar.DAY_OF_YEAR);
}

From source file:Main.java

public static int getCurrentMonthDay() {
    // calendar.getActualMaximum(Calendar.DAY_OF_MONTH),
    Calendar a = Calendar.getInstance();
    a.set(Calendar.DATE, 1);//from ww w.  j ava2 s .c  om
    a.roll(Calendar.DATE, -1);
    int maxDate = a.get(Calendar.DATE);
    return maxDate;
}

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 boolean isLeapYear(Date date) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);//from   w ww .j ava 2 s . c  o m
    int year = cal.get(Calendar.YEAR);
    return isLeapYear(year);
}

From source file:Main.java

/**
 * Convert string format date data to whooing date format integer
 * @param   dateStr     Date data formatted string like "05/21"
 * @return   Return whooing style integer date like 20121212 otherwise -1
 * *//*from w  ww . j  a va  2 s. c o  m*/
static public int convertWhooingDate(String dateStr) {
    String convertDate = dateStr.replace("/", "");
    if (convertDate.length() == 3) {
        convertDate = "0" + convertDate;
    }
    Calendar rightNow = Calendar.getInstance();

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

    int today = year * 10000 + month * 100 + day;
    convertDate = year + convertDate;
    int convertDateInt = 0;
    try {
        convertDateInt = Integer.valueOf(convertDate);
        // In case of receiving message 12/31 on 1 Jan
        if ((today / 10000) < (convertDateInt / 10000)) {
            convertDateInt -= 10000;
        }

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

    //guard wrong date
    if (convertDateInt < ((year * 10000) + 101)) {
        convertDateInt = today;
    }
    return convertDateInt;
}

From source file:Main.java

/**
 * Format a time value, including the AM/PM indicator.
 * /*from w w w . j  av a  2  s .c om*/
 * @param time the time to format
 * @return the formatted time
 */
public static String formatTime(Calendar time) {
    // TODO: 24 hour clock support
    int hour = time.get(Calendar.HOUR_OF_DAY);
    int minute = time.get(Calendar.MINUTE);
    String ampm = (hour < 12 ? " AM" : " PM");
    if (hour == 0)
        hour = 12;
    else if (hour > 12)
        hour -= 12;
    if (minute < 10)
        return hour + ":0" + minute + ampm;
    else
        return hour + ":" + minute + ampm;
}

From source file:Main.java

public static String getTimeString(Calendar calendar) {
    if (calendar == null)
        return "";
    String hourString = String.format("%1$02d", calendar.get(Calendar.HOUR_OF_DAY));
    String minuteString = String.format("%1$02d", calendar.get(Calendar.MINUTE));
    StringBuilder dateStringBuilder = new StringBuilder();
    dateStringBuilder.append(hourString).append(":");
    dateStringBuilder.append(minuteString);
    return dateStringBuilder.toString();
}