Example usage for java.util Calendar DAY_OF_WEEK

List of usage examples for java.util Calendar DAY_OF_WEEK

Introduction

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

Prototype

int DAY_OF_WEEK

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

Click Source Link

Document

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

Usage

From source file:Main.java

public static int[] getSchoolCalDate(int year, int month, int day, int week, int dayOfWeek) {
    Calendar cal = Calendar.getInstance();
    cal.set(year, month - 1, day);//  ww w  .j  a  v  a2  s . c om
    Log.d(TAG, "Date: " + cal.toString());
    Log.d(TAG, "year: " + year);
    Log.d(TAG, "month: " + month);
    Log.d(TAG, "day: " + day);
    cal.add(Calendar.DATE, -offset(cal.get(Calendar.DAY_OF_WEEK)));
    int count = (week - 1) * 7 + dayOfWeek - 1;
    Log.d(TAG, "count: " + count);
    cal.add(Calendar.DATE, count);
    int result[] = { cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + 1, cal.get(Calendar.DAY_OF_MONTH) };

    return result;
}

From source file:Main.java

public static int getDayOfWeek(Date date) {
    Calendar c = Calendar.getInstance();
    c.setTime(date);//w w w .j  a  v a 2 s  . co  m
    return c.get(Calendar.DAY_OF_WEEK);
}

From source file:Main.java

public static Calendar getRandomDate(int baseYear) {
    synchronized (rand) {
        Calendar today = Calendar.getInstance();
        int year = rand.nextInt(today.get(Calendar.YEAR) - baseYear + 1) + baseYear;
        int month = year == today.get(Calendar.YEAR) ? rand.nextInt(today.get(Calendar.MONTH) - 1) + 1
                : rand.nextInt(12) + 1;/* w ww  .j av a2s .com*/
        int day = year == today.get(Calendar.YEAR) ? rand.nextInt(today.get(Calendar.DAY_OF_WEEK)) + 1
                : rand.nextInt(29) + 1;

        return new GregorianCalendar(year, month, day);
    }
}

From source file:Main.java

private static String getDayOfWeek(String format, int calendarField) {
    String strDate = null;//from  w ww.jav a 2 s . c om
    try {
        Calendar c = new GregorianCalendar();
        SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format, Locale.CHINA);
        int week = c.get(Calendar.DAY_OF_WEEK);
        if (week == calendarField) {
            strDate = mSimpleDateFormat.format(c.getTime());
        } else {
            int offectDay = calendarField - week;
            if (calendarField == Calendar.SUNDAY) {
                offectDay = 7 - Math.abs(offectDay);
            }
            c.add(Calendar.DATE, offectDay);
            strDate = mSimpleDateFormat.format(c.getTime());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return strDate;
}

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  w w w  .  j  a va2s.  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 int getWeekIndex(Date date) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);/* w  w w . j  av  a 2  s.  c  om*/
    return cal.get(Calendar.DAY_OF_WEEK);
}

From source file:Main.java

/**
 * Get the displayable date from week and year
 * @param week week of year//  w  ww .jav  a2s  .co  m
 * @param year year
 * @return Date string for displaying
 */
public static String getDate(int week, int year) {
    SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy");
    Calendar cal = Calendar.getInstance();
    //        Log.i(TAG, sdf.format(cal.getTime()));
    cal.set(Calendar.YEAR, year);
    //        Log.i(TAG, sdf.format(cal.getTime()));
    cal.set(Calendar.WEEK_OF_YEAR, week);
    //        Log.i(TAG, sdf.format(cal.getTime()));
    cal.set(Calendar.DAY_OF_WEEK, 1);
    //        Log.i(TAG, sdf.format(cal.getTime()));
    String start = sdf.format(cal.getTime());
    cal.set(Calendar.DAY_OF_WEEK, 7);
    //        Log.i(TAG, sdf.format(cal.getTime()));
    String end = sdf.format(cal.getTime());
    return start + " to " + end;
}

From source file:Main.java

public static Calendar getCalendarForTime(int repeatMode, int dayOfWeek, int hour, int minute, int second) {
    Calendar calendarNow = Calendar.getInstance();
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, hour);
    calendar.set(Calendar.MINUTE, minute);
    calendar.set(Calendar.SECOND, second);
    calendar.set(Calendar.MILLISECOND, 0);
    if (repeatMode == 2) {
        int dayOfWeekAndroid = 0; // 7 stands for sunday for interface, but for android, sunday stands for 1.
        dayOfWeekAndroid = dayOfWeek % 7 + 1;
        calendar.set(Calendar.DAY_OF_WEEK, dayOfWeekAndroid);
    }//from   w ww .  j  av  a2s  .  c o m

    // make sure the desire alarm time is in future.
    int tryCount = 0;
    int tryCountMax = 62;
    while (calendar.getTimeInMillis() < calendarNow.getTimeInMillis() && tryCount < tryCountMax) {
        if (repeatMode == 1) {
            calendar.add(Calendar.DAY_OF_YEAR, 1);
        } else if (repeatMode == 2) {
            calendar.add(Calendar.DAY_OF_YEAR, 7);
        }
        tryCount++;
    }
    Log.v("cpeng", "getCalendearForTime target info: " + calendar.toString());
    return calendar;
}

From source file:Main.java

public static boolean isWeekendDayInCalendar(Calendar calendar) {
    return calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY
            || calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY;
}

From source file:Main.java

public static int getDayOfWeek() {
    Calendar calendar = Calendar.getInstance();
    int day = calendar.get(Calendar.DAY_OF_WEEK);
    if (day == 1) {
        day = 7;/*from   w  w w  .j  a v a2 s  .c om*/
    } else {
        day = day - 1;
    }
    return day;
}