Example usage for java.util Calendar MONTH

List of usage examples for java.util Calendar MONTH

Introduction

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

Prototype

int MONTH

To view the source code for java.util Calendar MONTH.

Click Source Link

Document

Field number for get and set indicating the month.

Usage

From source file:Main.java

/**
 * /*  w w  w.  j av a  2 s .  c  o  m*/
 * @param calendar
 * @return date with prefixes
 */
public static String styleDate(Calendar calendar) {
    String style = "";
    int date = calendar.get(Calendar.DAY_OF_MONTH);
    int month = calendar.get(Calendar.MONTH);
    if (stDates.contains(date)) {
        style = calendar.get(Calendar.DAY_OF_MONTH) + "<sup>st</sup> " + getMonth(month);
    } else if (ndDates.contains(date)) {
        style = calendar.get(Calendar.DAY_OF_MONTH) + "<sup>nd</sup> " + getMonth(month);
    } else if (rdDates.contains(date)) {
        style = calendar.get(Calendar.DAY_OF_MONTH) + "<sup>rd</sup> " + getMonth(month);
    } else {
        style = calendar.get(Calendar.DAY_OF_MONTH) + "<sup>th</sup> " + getMonth(month);
    }
    return style;
}

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
 * *///w  w w  .j a va2s  . co  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:com.googlecode.commons.swing.util.DateUtils2.java

public static Date getEndOfMonth(Date month) {
    if (month == null) {
        return null;
    }//from  w ww .  j a  v a 2  s.  c om
    Date end = (Date) month.clone();
    end = DateUtils.truncate(end, Calendar.MONTH);
    end = DateUtils.addMonths(end, 1);
    end = DateUtils.addMilliseconds(end, -1);
    return end;
}

From source file:Main.java

/**
 * Get a list of initial and end calendar of months in the range received
 * @param cStart Calendar date to start// www .  j a  v a2s . c o  m
 * @param cEnd Calendar date to end
 * @return List<Pair<Calendar, Calendar>> list of calendars (initial and end)
 */
public static List<Pair<Calendar, Calendar>> getRangeInMonths(Calendar cStart, Calendar cEnd) {

    //generate the list
    List<Pair<Calendar, Calendar>> calendars = new ArrayList<>();

    //from the first calendar start adding a month until the actual calendar is after the end
    Calendar cActual = generateCalendar(cStart);
    cActual.set(Calendar.DAY_OF_MONTH, 1);
    Calendar c0;
    Calendar cF;

    while (cActual.compareTo(cEnd) < 0) {

        //calendar start
        if (calendars.size() == 0) {
            c0 = generateCalendar(cStart);
        } else {
            c0 = generateCalendar(cActual);
        }

        //increment a month
        cActual.add(Calendar.MONTH, 1);

        //calendar end
        if (cActual.after(cEnd)) {
            cF = generateCalendar(cEnd);
        } else {
            cF = generateCalendar(cActual);

            //remove 1 day to set the last day of the month
            cF.add(Calendar.DAY_OF_YEAR, -1);
        }

        //add the pair to the list
        calendars.add(new Pair<Calendar, Calendar>(c0, cF));
    }

    //return the list
    return calendars;
}

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   w  ww.  j  a va  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

protected static String toString(GregorianCalendar g1) {
    StringBuilder buffer = new StringBuilder();

    buffer.append(/*  w ww  . j a v  a2 s .c o m*/
            g1.get(Calendar.YEAR) + "/" + (g1.get(Calendar.MONTH) + 1) + "/" + g1.get(Calendar.DAY_OF_MONTH));
    buffer.append("  ");
    buffer.append(g1.get(Calendar.HOUR_OF_DAY) + ":" + g1.get(Calendar.MINUTE) + ":" + g1.get(Calendar.SECOND)
            + "." + g1.get(Calendar.MILLISECOND));

    return buffer.toString();
}

From source file:Main.java

/**
 * Gets the default ARO trace folder name in the HH:MM:SS:DD:MM:YY format.
 * // ww w . ja va2  s  .  com
 * @return The default ARO trace folder name.
 */
public static String getDefaultTraceFolderName() {
    final Date systemDate = new Date();
    final Calendar now = Calendar.getInstance();
    final int currenthours = systemDate.getHours();
    final int currentminutes = systemDate.getMinutes();
    final int currentseconds = systemDate.getSeconds();
    final int currentdate = now.get(Calendar.DATE); // java calendar

    int currentmonth = now.get(Calendar.MONTH); // As Jan is defined as 0 in
    currentmonth = currentmonth + 1;
    if (currentmonth >= 13) // As Jan is defined as 0 in java calendar
        currentmonth = 1;
    String currentMonth = Integer.toString(currentmonth);
    String currentDate = Integer.toString(currentdate);
    String currentHours = Integer.toString(currenthours);
    String currentMinutes = Integer.toString(currentminutes);
    String currentSeconds = Integer.toString(currentseconds - 1);

    if (currentmonth < 10) {
        currentMonth = "";
        currentMonth = "0" + currentmonth;
    }
    if (currentdate < 10) {
        currentDate = "";
        currentDate = "0" + currentdate;
    }

    if (currenthours < 10) {
        currentHours = "";
        currentHours = "0" + currenthours;
    }
    if (currentminutes < 10) {
        currentMinutes = "";
        currentMinutes = "0" + currentminutes;
    }
    if (currentseconds < 10) {
        currentSeconds = "";
        currentSeconds = "0" + currentseconds;
    }
    final String folderName = now.get(Calendar.YEAR) + "-" + currentMonth + "-" + currentDate + "-"
            + currentHours + "-" + currentMinutes + "-" + currentSeconds;

    return folderName;
}

From source file:Main.java

public static boolean isInPast(int day, int month, int year) {
    Calendar c = Calendar.getInstance();

    c.set(Calendar.YEAR, year);//from   w w w  .  ja  va 2 s. co  m
    c.set(Calendar.MONTH, month - 1);
    c.set(Calendar.DAY_OF_MONTH, day);

    Date pastDate = c.getTime();

    return (pastDate.getTime() < new Date().getTime());
}

From source file:net.seratch.taskun.util.CalendarUtil.java

 public static Integer getMonth(Calendar calendar) {
   Integer month = calendar.get(Calendar.MONTH) + 1;
   return month;
}

From source file:Main.java

@NonNull
static Date convertDate(int date, int time) {
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.YEAR, 1997);
    calendar.set(Calendar.MONTH, Calendar.JANUARY);
    calendar.set(Calendar.DAY_OF_MONTH, 1);
    calendar.set(Calendar.HOUR_OF_DAY, time / 60);
    calendar.set(Calendar.MINUTE, time % 60);

    calendar.add(Calendar.DATE, date);

    return calendar.getTime();
}