下面列出了java.util.Calendar#SUNDAY 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public Object compute(Object[] args, ExecutionContext ec) {
for (Object arg : args) {
if (ExecUtils.isNull(arg)) {
return null;
}
}
java.sql.Timestamp timestamp = DataType.TimestampType.convertFrom(args[0]);
Calendar cal = Calendar.getInstance();
cal.setTime(timestamp);
int week = cal.get(Calendar.DAY_OF_WEEK);
if (week == Calendar.SUNDAY) {
return 6;
} else {
return week - 2;
}
}
public static int getTodayWeekdayNormalized() {
final Calendar calendar = Calendar.getInstance();
final int weekday = calendar.get(Calendar.DAY_OF_WEEK);
switch (weekday) {
case Calendar.SUNDAY:
return 0;
case Calendar.MONDAY:
return 1;
case Calendar.TUESDAY:
return 2;
case Calendar.WEDNESDAY:
return 3;
case Calendar.THURSDAY:
return 4;
case Calendar.FRIDAY:
return 5;
case Calendar.SATURDAY:
return 6;
default:
return 0;
}
}
public String getDays(String[] dayNames, String[] shortDyNames) {
int nrDays = 0;
int dayCode = 0;
for (Meeting meeting : getMeetings()) {
int dc = 0;
switch (meeting.getDayOfWeek()) {
case Calendar.MONDAY : dc = Constants.DAY_CODES[Constants.DAY_MON]; break;
case Calendar.TUESDAY : dc = Constants.DAY_CODES[Constants.DAY_TUE]; break;
case Calendar.WEDNESDAY : dc = Constants.DAY_CODES[Constants.DAY_WED]; break;
case Calendar.THURSDAY : dc = Constants.DAY_CODES[Constants.DAY_THU]; break;
case Calendar.FRIDAY : dc = Constants.DAY_CODES[Constants.DAY_FRI]; break;
case Calendar.SATURDAY : dc = Constants.DAY_CODES[Constants.DAY_SAT]; break;
case Calendar.SUNDAY : dc = Constants.DAY_CODES[Constants.DAY_SUN]; break;
}
if ((dayCode & dc)==0) nrDays++;
dayCode |= dc;
}
String ret = "";
for (int i=0;i<Constants.DAY_CODES.length;i++) {
if ((dayCode & Constants.DAY_CODES[i])!=0)
ret += (nrDays==1?dayNames:shortDyNames)[i];
}
return ret;
}
/**
* 获取星期几
*
* @param date
* @param format
* @return 周日 周一 周二 周三 周四 周五 周六
* @throws ParseException
*/
public static String getWeekDay(String date, String format) throws ParseException {
Calendar c = gc(date, format);
if (c != null) {
int weekDay = c.get(Calendar.DAY_OF_WEEK);
switch (weekDay) {
case Calendar.SUNDAY:
return "周日";
case Calendar.MONDAY:
return "周一";
case Calendar.TUESDAY:
return "周二";
case Calendar.WEDNESDAY:
return "周三";
case Calendar.THURSDAY:
return "周四";
case Calendar.FRIDAY:
return "周五";
case Calendar.SATURDAY:
return "周六";
}
}
return "";
}
protected String getCalendarAbbrFor(int day) {
switch (day) {
case Calendar.MONDAY: return "Mo";
case Calendar.TUESDAY: return "Tu";
case Calendar.WEDNESDAY: return "We";
case Calendar.THURSDAY: return "Th";
case Calendar.FRIDAY: return "Fr";
default:
case Calendar.SUNDAY: return "Su";
}
}
@Test
public void alarm_RingsAt_BackwardQueueingRecurringDays() {
Calendar cal = new GregorianCalendar();
int D_C = cal.get(Calendar.DAY_OF_WEEK);
for (int h = 0; h < 24; h++) {
for (int m = 0; m < 60; m++) {
Alarm a = Alarm.builder().hour(h).minutes(m).build();
for (int D = Calendar.SATURDAY; D >= Calendar.SUNDAY; D--) {
out.println("Testing (h, m, d) = ("+h+", "+m+", "+ (D-1) +")");
int hC = cal.get(HOUR_OF_DAY); // Current hour
int mC = cal.get(MINUTE); // Current minute
a.setRecurring(D - 1, true);
int days = 0;
if (h > hC || (h == hC && m > mC)) {
if (D < D_C) {
days = Calendar.SATURDAY - D_C + D;
} else if (D == D_C) {
days = 0; // upcoming on the same day
} else {
days = D - D_C;
}
} else if (h <= hC) {
if (D < D_C) {
days = Calendar.SATURDAY - D_C + D - 1;
} else if (D == D_C) {
days = 0;
} else {
days = D - D_C - 1;
}
}
calculateRingTimeAndTest(h, m, days, cal, a.ringsAt());
}
}
}
}
/**
* Get the day of the week
*
* @param useLocale return locale specific day of week
* e.g. <code>SUNDAY = 1, MONDAY = 2, ...</code> in the U.S.,
* <code>MONDAY = 1, TUESDAY = 2, ...</code> in France.
* @return the day of the week.
*/
public int getDay_Of_Week( boolean useLocale )
{
int dayofweek = m_calendar.get(Calendar.DAY_OF_WEEK);
if ( useLocale )
{
if ( dayofweek >= m_calendar.getFirstDayOfWeek() )
dayofweek = dayofweek - (m_calendar.getFirstDayOfWeek()-Calendar.SUNDAY);
else
dayofweek = dayofweek + Calendar.SATURDAY - (m_calendar.getFirstDayOfWeek()-Calendar.SUNDAY);
}
return dayofweek;
}
/**
* 判断是否周末
* @param recordDate
* @return
*/
public boolean isWeekend(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime( date );
if(cal.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY||cal.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY){
return true;
}
return false;
}
/**
* 根据日期,返回其星期数,周一为1,周日为7
* @param date
* @return
*/
public static int getWeekDay(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int w = calendar.get(Calendar.DAY_OF_WEEK);
int ret;
if (w == Calendar.SUNDAY)
ret = 7;
else
ret = w - 1;
return ret;
}
@SuppressWarnings("unused")
public void setFirstDayOfWeek(int startOfWeek) {
if (startOfWeek < Calendar.SUNDAY || startOfWeek > Calendar.SATURDAY) {
throw new IllegalArgumentException("Value must be between Calendar.SUNDAY and " +
"Calendar.SATURDAY");
}
mWeekStart = startOfWeek;
if (mDayPickerView != null) {
mDayPickerView.onChange();
}
}
private static int toWeekDay(int calWeekDay) {
if (calWeekDay == Calendar.SUNDAY) {
return 7;
} else {
return calWeekDay - 1;
}
}
private static int toWeekDay(int calWeekDay) {
if (calWeekDay == Calendar.SUNDAY) {
return 7;
} else {
return calWeekDay - 1;
}
}
private static int toWeekDay(int calWeekDay) {
if (calWeekDay == Calendar.SUNDAY) {
return 7;
} else {
return calWeekDay - 1;
}
}
private static int toWeekDay(int calWeekDay) {
if (calWeekDay == Calendar.SUNDAY) {
return 7;
} else {
return calWeekDay - 1;
}
}
/**
* Gets the text for the specified chrono, field, locale and style
* for the purpose of formatting.
* <p>
* The text associated with the value is returned.
* The null return value should be used if there is no applicable text, or
* if the text would be a numeric representation of the value.
*
* @param chrono the Chronology to get text for, not null
* @param field the field to get text for, not null
* @param value the field value to get text for, not null
* @param style the style to get text for, not null
* @param locale the locale to get text for, not null
* @return the text for the field value, null if no text found
*/
public String getText(Chronology chrono, TemporalField field, long value,
TextStyle style, Locale locale) {
if (chrono == IsoChronology.INSTANCE
|| !(field instanceof ChronoField)) {
return getText(field, value, style, locale);
}
int fieldIndex;
int fieldValue;
if (field == ERA) {
fieldIndex = Calendar.ERA;
if (chrono == JapaneseChronology.INSTANCE) {
if (value == -999) {
fieldValue = 0;
} else {
fieldValue = (int) value + 2;
}
} else {
fieldValue = (int) value;
}
} else if (field == MONTH_OF_YEAR) {
fieldIndex = Calendar.MONTH;
fieldValue = (int) value - 1;
} else if (field == DAY_OF_WEEK) {
fieldIndex = Calendar.DAY_OF_WEEK;
fieldValue = (int) value + 1;
if (fieldValue > 7) {
fieldValue = Calendar.SUNDAY;
}
} else if (field == AMPM_OF_DAY) {
fieldIndex = Calendar.AM_PM;
fieldValue = (int) value;
} else {
return null;
}
return CalendarDataUtility.retrieveJavaTimeFieldValueName(
chrono.getCalendarType(), fieldIndex, fieldValue, style.toCalendarStyle(), locale);
}
public void testDstNewYork2014_2ndSundayMarch_1stSundayNovember_StandardTime() {
TimeZone timeZone = new SimpleTimeZone(NEW_YORK_RAW_OFFSET, "EST",
Calendar.MARCH, 2, Calendar.SUNDAY, 7200000, SimpleTimeZone.STANDARD_TIME,
Calendar.NOVEMBER, 1, Calendar.SUNDAY, 3600000, SimpleTimeZone.STANDARD_TIME,
3600000);
checkDstNewYork2014(timeZone);
}
public static long getWeekMonday0AM(long time) {
Calendar calendar = getCalendar();
calendar.setTimeInMillis(time);
if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
calendar.add(Calendar.DAY_OF_WEEK, -1);
}
calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTimeInMillis();
}
private static byte dayOfWeekToRawBytes(Calendar cal) {
int calValue = cal.get(Calendar.DAY_OF_WEEK);
switch (calValue) {
case Calendar.SUNDAY:
return 7;
default:
return (byte) (calValue - 1);
}
}
/**
* Gets the text for the specified chrono, field, locale and style
* for the purpose of formatting.
* <p>
* The text associated with the value is returned.
* The null return value should be used if there is no applicable text, or
* if the text would be a numeric representation of the value.
*
* @param chrono the Chronology to get text for, not null
* @param field the field to get text for, not null
* @param value the field value to get text for, not null
* @param style the style to get text for, not null
* @param locale the locale to get text for, not null
* @return the text for the field value, null if no text found
*/
public String getText(Chronology chrono, TemporalField field, long value,
TextStyle style, Locale locale) {
if (chrono == IsoChronology.INSTANCE
|| !(field instanceof ChronoField)) {
return getText(field, value, style, locale);
}
int fieldIndex;
int fieldValue;
if (field == ERA) {
fieldIndex = Calendar.ERA;
if (chrono == JapaneseChronology.INSTANCE) {
if (value == -999) {
fieldValue = 0;
} else {
fieldValue = (int) value + 2;
}
} else {
fieldValue = (int) value;
}
} else if (field == MONTH_OF_YEAR) {
fieldIndex = Calendar.MONTH;
fieldValue = (int) value - 1;
} else if (field == DAY_OF_WEEK) {
fieldIndex = Calendar.DAY_OF_WEEK;
fieldValue = (int) value + 1;
if (fieldValue > 7) {
fieldValue = Calendar.SUNDAY;
}
} else if (field == AMPM_OF_DAY) {
fieldIndex = Calendar.AM_PM;
fieldValue = (int) value;
} else {
return null;
}
return CalendarDataUtility.retrieveJavaTimeFieldValueName(
chrono.getCalendarType(), fieldIndex, fieldValue, style.toCalendarStyle(), locale);
}
public String getSunday() {
return daysOfWeek[Calendar.SUNDAY];
}