Example usage for java.util Calendar DAY_OF_MONTH

List of usage examples for java.util Calendar DAY_OF_MONTH

Introduction

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

Prototype

int DAY_OF_MONTH

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

Click Source Link

Document

Field number for get and set indicating the day of the month.

Usage

From source file:Main.java

/**
 * /*from  w ww.j ava2 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

public static long getPeriodStart(int periodType, long date) {
    final Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(date);//from  w w w .j  a v  a 2s  . c o m

    switch (periodType) {
    case TYPE_DAY: {
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        break;
    }

    case TYPE_WEEK: {
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        final int currentDayOfWeek = (cal.get(Calendar.DAY_OF_WEEK) + 7 - cal.getFirstDayOfWeek()) % 7;
        cal.add(Calendar.DAY_OF_YEAR, -currentDayOfWeek);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        break;
    }

    case TYPE_MONTH: {
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        break;
    }

    case TYPE_YEAR: {
        cal.set(Calendar.MONTH, 0);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        break;
    }
    }
    return cal.getTimeInMillis();
}

From source file:Main.java

/**
 * Returns date before given days/*from   w  ww. j a v  a 2 s.  com*/
 *
 * @param days days to before
 * @return string date string before days
 */
public static String getDateBefore(int days) {
    Date today = new Date();
    Calendar cal = new GregorianCalendar();
    cal.setTime(today);
    cal.add(Calendar.DAY_OF_MONTH, days * -1);
    Date date = cal.getTime();
    SimpleDateFormat gmtFormat = new SimpleDateFormat();
    gmtFormat.applyPattern("yyyy-MM-dd 00:00:00");
    TimeZone gmtTime = TimeZone.getTimeZone("GMT");
    gmtFormat.setTimeZone(gmtTime);
    return gmtFormat.format(date);
}

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   www  .j  a  v  a  2s.  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

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

    buffer.append(/*from   w ww  .j  a  v a2s  .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

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

    c.set(Calendar.YEAR, year);/*from w w  w  .  ja  v a  2  s .c  o 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:Main.java

public static void addCalendarDay(Calendar cal, int days) {
    cal.add(Calendar.DAY_OF_MONTH, days);
}

From source file:com.alibaba.utils.date.CommonDateUtils.java

/**
 * <pre>//from  w w w.  j ava2 s  .c  om
 * 
 * :
 * 2013.09.202
 * 2013.09.18, 2013.09.19, 2013.09.20
 * </pre>
 * @param currentDate
 * @param days
 * @return
 */
public static List<Date> beforeCurrentDate(Date currentDate, int days) {
    List<Date> dateList = new ArrayList<Date>();

    // 0. 
    if (currentDate == null) {
        return null;
    }
    if (days <= 0) {
        dateList.add(DateUtils.truncate(currentDate, Calendar.DAY_OF_MONTH));
        return dateList;
    }

    // 1. 
    Date startDate = DateUtils.addDays(currentDate, 0 - days);
    for (int i = 0; i < days; ++i) {
        Date date = DateUtils.addDays(startDate, i);
        // 
        dateList.add(DateUtils.truncate(date, Calendar.DAY_OF_MONTH));
    }
    // 2. 
    dateList.add(DateUtils.truncate(currentDate, Calendar.DAY_OF_MONTH));

    return dateList;
}

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();
}

From source file:Main.java

public static int[] getTime(long time, String timeZone) {
    Calendar c = Calendar.getInstance();
    if (timeZone != null && timeZone.length() != 0) {
        TimeZone tz = TimeZone.getTimeZone(timeZone);
        c = Calendar.getInstance(tz);
    }/*w  ww . j  a v a  2s .co m*/
    c.setTimeInMillis(time);
    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 new int[] { y, m, d, hour, minute, second };
}