本文整理汇总了Java中java.util.GregorianCalendar.setFirstDayOfWeek方法的典型用法代码示例。如果您正苦于以下问题:Java GregorianCalendar.setFirstDayOfWeek方法的具体用法?Java GregorianCalendar.setFirstDayOfWeek怎么用?Java GregorianCalendar.setFirstDayOfWeek使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.GregorianCalendar
的用法示例。
在下文中一共展示了GregorianCalendar.setFirstDayOfWeek方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getWeekStartDate
import java.util.GregorianCalendar; //导入方法依赖的package包/类
/**
* 当前星期的第一天
* @param date
* @return
*/
public static Date getWeekStartDate(Date date){
GregorianCalendar c = new GregorianCalendar(Locale.CHINA);
c.setTime(date);
c.setFirstDayOfWeek(RANGE_WEEK_MONDAY);
c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
c.set(Calendar.HOUR_OF_DAY, 00);
c.set(Calendar.MINUTE, 00);
c.set(Calendar.SECOND, 00);
return c.getTime();
}
示例2: getWeekEndDate
import java.util.GregorianCalendar; //导入方法依赖的package包/类
/**
* 当前星期的最后一天
* @param date
* @return
*/
public static Date getWeekEndDate(Date date){
GregorianCalendar c = new GregorianCalendar(Locale.CHINA);
c.setTime(date);
c.setFirstDayOfWeek(RANGE_WEEK_MONDAY);
c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
c.set(Calendar.HOUR_OF_DAY, 23);
c.set(Calendar.MINUTE, 59);
c.set(Calendar.SECOND, 59);
return c.getTime();
}
示例3: Test4166109
import java.util.GregorianCalendar; //导入方法依赖的package包/类
/**
* GregorianCalendar.getActualMaximum() does not account for first day of week.
*/
public void Test4166109() {
/* Test month:
*
* March 1998
* Su Mo Tu We Th Fr Sa
* 1 2 3 4 5 6 7
* 8 9 10 11 12 13 14
* 15 16 17 18 19 20 21
* 22 23 24 25 26 27 28
* 29 30 31
*/
boolean passed = true;
int field = WEEK_OF_MONTH;
GregorianCalendar calendar = new GregorianCalendar(Locale.US);
calendar.set(1998, MARCH, 1);
calendar.setMinimalDaysInFirstWeek(1);
logln("Date: " + calendar.getTime());
int firstInMonth = calendar.get(DAY_OF_MONTH);
for (int firstInWeek = SUNDAY; firstInWeek <= SATURDAY; firstInWeek++) {
calendar.setFirstDayOfWeek(firstInWeek);
int returned = calendar.getActualMaximum(field);
int expected = (31 + ((firstInMonth - firstInWeek + 7) % 7) + 6) / 7;
logln("First day of week = " + firstInWeek
+ " getActualMaximum(WEEK_OF_MONTH) = " + returned
+ " expected = " + expected
+ ((returned == expected) ? " ok" : " FAIL"));
if (returned != expected) {
passed = false;
}
}
if (!passed) {
errln("Test failed");
}
}
示例4: Test4197699
import java.util.GregorianCalendar; //导入方法依赖的package包/类
/**
* Week of year is wrong at the start and end of the year.
*/
public void Test4197699() {
GregorianCalendar cal = new GregorianCalendar();
cal.setFirstDayOfWeek(MONDAY);
cal.setMinimalDaysInFirstWeek(4);
DateFormat fmt = new SimpleDateFormat("E dd MMM yyyy 'DOY='D 'WOY='w");
fmt.setCalendar(cal);
int[] DATA = {
2000, JANUARY, 1, 52,
2001, DECEMBER, 31, 1};
for (int i = 0; i < DATA.length;) {
cal.set(DATA[i++], DATA[i++], DATA[i++]);
int expWOY = DATA[i++];
int actWOY = cal.get(WEEK_OF_YEAR);
if (expWOY == actWOY) {
logln("Ok: " + fmt.format(cal.getTime()));
} else {
errln("FAIL: " + fmt.format(cal.getTime())
+ ", expected WOY=" + expWOY);
cal.add(DATE, -8);
for (int j = 0; j < 14; ++j) {
cal.add(DATE, 1);
logln(fmt.format(cal.getTime()));
}
}
}
}
示例5: Test4546637
import java.util.GregorianCalendar; //导入方法依赖的package包/类
/**
* 4546637: Incorrect WEEK_OF_MONTH after changing First Day Of Week
*/
public void Test4546637() {
GregorianCalendar day = new GregorianCalendar(2001, NOVEMBER, 04);
day.setMinimalDaysInFirstWeek(1);
int wom = day.get(WEEK_OF_MONTH);
day.setFirstDayOfWeek(MONDAY);
if (day.get(WEEK_OF_MONTH) != 1) {
errln("Fail: 2001/11/4 must be the first week of the month.");
}
}
示例6: Test4623997
import java.util.GregorianCalendar; //导入方法依赖的package包/类
/**
* 4623997: GregorianCalendar returns bad WEEK_OF_YEAR
*/
public void Test4623997() {
GregorianCalendar cal = new GregorianCalendar(2000, JANUARY, 1);
int dow = cal.get(DAY_OF_WEEK);
cal.setFirstDayOfWeek(MONDAY);
cal.setMinimalDaysInFirstWeek(4);
if (cal.get(WEEK_OF_YEAR) != 52) {
errln("Fail: 2000/1/1 must be the 52nd week of the year.");
}
}
示例7: completeDate
import java.util.GregorianCalendar; //导入方法依赖的package包/类
private String completeDate(GregorianCalendar gc, boolean bLongInfo)
{
GregorianCalendar calendar = (GregorianCalendar)gc.clone();
String csDate = "00000000";
String csDayOfWeek = "0";
String csDaysInMonth = "00";
String csDayOfYear = "000";
String csDaysToEndYear = "000";
String csDaysInYear = "000";
String csWeekOfYear = "00";
String csDayEaster = "000";
String csFlagHoliday = " ";
String csLeapYear = "0";
calendar.setFirstDayOfWeek(GregorianCalendar.MONDAY);
calendar.setMinimalDaysInFirstWeek(4);
Format formatter = new SimpleDateFormat("yyyyMMdd");
csDate = formatter.format(calendar.getTime());
int dayOfWeek = calendar.get(GregorianCalendar.DAY_OF_WEEK);
if (dayOfWeek == 1)
{
csDayOfWeek = "7";
}
else
{
csDayOfWeek = String.valueOf(dayOfWeek - 1) ;
}
csDaysInMonth = String.valueOf(calendar.getActualMaximum(GregorianCalendar.DAY_OF_MONTH));
csWeekOfYear = StringUtil.leftPad(String.valueOf(calendar.get(GregorianCalendar.WEEK_OF_YEAR)), 2, '0');
int dayOfYear = calendar.get(GregorianCalendar.DAY_OF_YEAR);
calendar.set(GregorianCalendar.MONTH, 11);
calendar.set(GregorianCalendar.DAY_OF_MONTH, 31);
int daysInYear = calendar.get(GregorianCalendar.DAY_OF_YEAR);
csDayOfYear = StringUtil.leftPad(String.valueOf(dayOfYear), 3, '0');
csDaysToEndYear = StringUtil.leftPad(String.valueOf(daysInYear - dayOfYear), 3, '0');
csDaysInYear = StringUtil.leftPad(String.valueOf(daysInYear), 3, '0');
GregorianCalendar easterCalendar = findDateEaster(calendar.get(GregorianCalendar.YEAR));
easterCalendar.set(GregorianCalendar.HOUR, 0);
csDayEaster = StringUtil.leftPad(String.valueOf(easterCalendar.get(GregorianCalendar.DAY_OF_YEAR)), 3, '0');
//int diffEasterDays = elapsedTime(easterCalendar, calendar, GregorianCalendar.DATE);
long diffMillis = gc.getTimeInMillis() - easterCalendar.getTimeInMillis();
long diffEasterDays = diffMillis/(24*60*60*1000);
if (csDate.substring(4).equals("0101") ||
csDate.substring(4).equals("0102") ||
csDate.substring(4).equals("0801") ||
csDate.substring(4).equals("1225") ||
csDate.substring(4).equals("1226") ||
csDayEaster.equals(csDayOfYear) ||
diffEasterDays == 1 || // lundi de P�ques
diffEasterDays == 39 || // Ascension
diffEasterDays == 50) // Pentec�te
{
csFlagHoliday = "F";
}
if (calendar.isLeapYear(calendar.get(GregorianCalendar.YEAR)))
{
csLeapYear = "9";
}
if (bLongInfo)
{
return csDate + csDayOfWeek + csDaysInMonth + csDayOfYear + csDaysToEndYear + csDaysInYear + csWeekOfYear + csDayEaster + csFlagHoliday;
}
else
{
String csTarif = csDayOfWeek;
if (csFlagHoliday.equals("F"))
csTarif = "7";
return csDate.substring(2) + csDayOfYear + csLeapYear + csTarif + csDayOfWeek + csWeekOfYear;
}
}
示例8: test_DayOfWeek_IsoChronology_vsCalendar
import java.util.GregorianCalendar; //导入方法依赖的package包/类
@Test(dataProvider = "RangeVersusCalendar")
public void test_DayOfWeek_IsoChronology_vsCalendar(LocalDate isoStartDate, LocalDate isoEndDate) {
GregorianCalendar cal = new GregorianCalendar();
assertEquals(cal.getCalendarType(), "gregory", "Unexpected calendar type");
LocalDate isoDate = IsoChronology.INSTANCE.date(isoStartDate);
for (DayOfWeek firstDayOfWeek : DayOfWeek.values()) {
for (int minDays = 1; minDays <= 7; minDays++) {
WeekFields weekDef = WeekFields.of(firstDayOfWeek, minDays);
cal.setFirstDayOfWeek(Math.floorMod(firstDayOfWeek.getValue(), 7) + 1);
cal.setMinimalDaysInFirstWeek(minDays);
cal.setTimeZone(TimeZone.getTimeZone("GMT+00"));
cal.set(Calendar.YEAR, isoDate.get(YEAR));
cal.set(Calendar.MONTH, isoDate.get(MONTH_OF_YEAR) - 1);
cal.set(Calendar.DAY_OF_MONTH, isoDate.get(DAY_OF_MONTH));
// For every date in the range
while (isoDate.isBefore(isoEndDate)) {
assertEquals(isoDate.get(DAY_OF_MONTH), cal.get(Calendar.DAY_OF_MONTH), "Day mismatch in " + isoDate + "; cal: " + cal);
assertEquals(isoDate.get(MONTH_OF_YEAR), cal.get(Calendar.MONTH) + 1, "Month mismatch in " + isoDate);
assertEquals(isoDate.get(YEAR_OF_ERA), cal.get(Calendar.YEAR), "Year mismatch in " + isoDate);
int jdow = Math.floorMod(cal.get(Calendar.DAY_OF_WEEK) - 2, 7) + 1;
int dow = isoDate.get(weekDef.dayOfWeek());
assertEquals(jdow, dow, "Calendar DayOfWeek does not match ISO DayOfWeek");
int jweekOfMonth = cal.get(Calendar.WEEK_OF_MONTH);
int isoWeekOfMonth = isoDate.get(weekDef.weekOfMonth());
assertEquals(jweekOfMonth, isoWeekOfMonth, "Calendar WeekOfMonth does not match ISO WeekOfMonth");
int jweekOfYear = cal.get(Calendar.WEEK_OF_YEAR);
int weekOfYear = isoDate.get(weekDef.weekOfWeekBasedYear());
assertEquals(jweekOfYear, weekOfYear, "GregorianCalendar WeekOfYear does not match WeekOfWeekBasedYear");
int jWeekYear = cal.getWeekYear();
int weekBasedYear = isoDate.get(weekDef.weekBasedYear());
assertEquals(jWeekYear, weekBasedYear, "GregorianCalendar getWeekYear does not match YearOfWeekBasedYear");
int jweeksInWeekyear = cal.getWeeksInWeekYear();
int weeksInWeekBasedYear = (int)isoDate.range(weekDef.weekOfWeekBasedYear()).getMaximum();
assertEquals(jweeksInWeekyear, weeksInWeekBasedYear, "length of weekBasedYear");
isoDate = isoDate.plus(1, ChronoUnit.DAYS);
cal.add(Calendar.DAY_OF_MONTH, 1);
}
}
}
}