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

/**
 * @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.//  ww  w  . j  av a  2 s  .  c  o m
 */
@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 long formatTimeString(String timeString) {
    long time = 0;
    if (timeString == null || timeString.length() < 1) {
        return time;
    }/*from  ww  w  .ja v  a2 s. c  om*/
    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;
}

From source file:Main.java

public static int[] parseDate(String dateString) {
    try {/*from  w  w w.  java  2s  .c o m*/
        DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
        formatter.setLenient(false);
        Date date = (Date) formatter.parse(dateString);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int[] dateInts = new int[3];
        dateInts[0] = cal.get(Calendar.DAY_OF_MONTH);
        dateInts[1] = cal.get(Calendar.MONTH);
        dateInts[2] = cal.get(Calendar.YEAR);

        return dateInts;
    } catch (Exception e) {
        throw new RuntimeException(
                "Could not parse date:'" + dateString + "'. Date format is dd-MM-yyyy (ex: 31-12-2011).", e);
    }
}

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

/**
 * Calculates how many months are in given period. If one of the dates only partially covers the month, it still counts as full month.
 *
 * @param start Start of the period.//ww w  .  j a  v a  2  s . c o m
 * @param end   End of the period.
 * @return Number of days in given period. If {@code end < start}, returns -1.
 */
public static int getMonthCountInPeriod(long start, long end) {
    if (end < start)
        return -1;

    final Calendar cal = Calendar.getInstance();
    final int monthCountInYear = cal.getMaximum(Calendar.MONTH);

    cal.setTimeInMillis(start);
    final int startYear = cal.get(Calendar.YEAR);
    final int startMonth = cal.get(Calendar.MONTH) + 1;

    cal.setTimeInMillis(end);
    final int endYear = cal.get(Calendar.YEAR);
    final int endMonth = cal.get(Calendar.MONTH) + 1;

    int monthsCount;

    if (startYear != endYear) {
        monthsCount = monthCountInYear * Math.max(0, endYear - startYear - 1);
        monthsCount += monthCountInYear - startMonth + 1;
        monthsCount += endMonth;
    } else {
        monthsCount = endMonth - startMonth + 1;
    }

    return monthsCount;
}

From source file:Main.java

public static XMLGregorianCalendar dateToXmlCalendar(Date date) {
    if (date != null) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);/*from w  w  w .  j  av  a 2  s.com*/

        try {
            XMLGregorianCalendar xmlGregorianCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar();
            xmlGregorianCalendar.setDay(calendar.get(Calendar.DAY_OF_MONTH));
            xmlGregorianCalendar.setMonth(calendar.get(Calendar.MONTH) + 1);
            xmlGregorianCalendar.setYear(calendar.get(Calendar.YEAR));
            return xmlGregorianCalendar;
        } catch (DatatypeConfigurationException e) {
            return null;
        }
    }

    return null;
}

From source file:Main.java

public static String format(Date date) {
    SimpleDateFormat myFmt1 = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss EEE");
    String[] days = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
    String[] months = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
    String[] daysFull = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);//from www  .  j  av  a 2  s.c o  m
    int day = calendar.get(Calendar.DAY_OF_WEEK);
    int month = calendar.get(Calendar.MONTH);
    return String
            .valueOf(days[day - 1] + " " + myFmt1.format(date) + " " + months[month] + " " + daysFull[day - 1]);

}

From source file:Main.java

public static XMLGregorianCalendar asXMLGregorianCalendar(java.util.Date date) {
    if (date == null) {
        return null;
    } else {//  w  w  w.  j a v  a 2  s . c om
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.setTime(date);

        XMLGregorianCalendar xmlGregorianCalendar = df.newXMLGregorianCalendar();
        xmlGregorianCalendar.setDay(calendar.get(Calendar.DAY_OF_MONTH));
        xmlGregorianCalendar.setMonth(calendar.get(Calendar.MONTH));
        xmlGregorianCalendar.setYear(calendar.get(Calendar.YEAR));

        return xmlGregorianCalendar;
    }
}

From source file:Main.java

public static Date parseData(String text) {
    Calendar calendar = Calendar.getInstance();
    if (text.contains(":")) {
        String[] time = text.split(":");
        int hours = Integer.parseInt(time[0]);
        calendar.set(Calendar.MINUTE, Integer.parseInt(time[1]));
        calendar.set(Calendar.HOUR_OF_DAY, hours);
        return calendar.getTime();
    } else if (text.contains("/")) {
        String[] date = text.split("/");
        if (date.length == 3) {
            calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(date[0]));
            calendar.set(Calendar.MONTH, Integer.parseInt(date[1]) - 1);
            calendar.set(Calendar.YEAR, Integer.parseInt(date[2]));
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            return calendar.getTime();
        } else if (date.length == 2) {
            calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(date[0]));
            calendar.set(Calendar.MONTH, Integer.parseInt(date[1]) - 1);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            return calendar.getTime();
        }//from   w  w  w  .  j  av a  2 s. co m
    }
    return null;
}

From source file:Main.java

static void setDate(Calendar cal, int month, int date) {
    cal.clear();/*from   w w w . j  ava 2s  . co m*/

    cal.set(Calendar.YEAR, Calendar.getInstance().get(Calendar.YEAR));
    cal.set(Calendar.MONTH, month);
    cal.set(Calendar.DATE, date);
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
}