本文整理匯總了Java中java.util.Calendar.setMinimalDaysInFirstWeek方法的典型用法代碼示例。如果您正苦於以下問題:Java Calendar.setMinimalDaysInFirstWeek方法的具體用法?Java Calendar.setMinimalDaysInFirstWeek怎麽用?Java Calendar.setMinimalDaysInFirstWeek使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.util.Calendar
的用法示例。
在下文中一共展示了Calendar.setMinimalDaysInFirstWeek方法的11個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: format
import java.util.Calendar; //導入方法依賴的package包/類
@Override
public String format(DateTime timestamp) {
Calendar c = Calendar.getInstance(timestamp.getZone().toTimeZone(), LOCALE);
c.setFirstDayOfWeek(Calendar.MONDAY);
c.setMinimalDaysInFirstWeek(4);
c.setTimeInMillis(timestamp.getMillis());
int week = c.get(Calendar.WEEK_OF_YEAR);
int weekYear = c.getWeekYear();
int year = c.get(Calendar.YEAR);
if (weekYear < year) {
week = 0;
} else if (weekYear > year) {
week = c.getWeeksInWeekYear();
}
return zeroPadded(2, String.valueOf(week));
}
示例2: getWeekDayOfYear
import java.util.Calendar; //導入方法依賴的package包/類
/**
* 一周的日期
* @param date 時間
* @return 結果
*/
public static List<Date> getWeekDayOfYear(Date date) {
Calendar calendar = getCalendarFromDate(date);
calendar.setFirstDayOfWeek(Calendar.MONDAY);
calendar.setMinimalDaysInFirstWeek(DAYS_OF_A_WEEK);
int week = calendar.get(Calendar.WEEK_OF_YEAR);
int year = calendar.get(Calendar.YEAR);
List<Date> result = new ArrayList<Date>();
result.add(getDateOfYearWeek(year, week, Calendar.MONDAY));
result.add(getDateOfYearWeek(year, week, Calendar.TUESDAY));
result.add(getDateOfYearWeek(year, week, Calendar.WEDNESDAY));
result.add(getDateOfYearWeek(year, week, Calendar.THURSDAY));
result.add(getDateOfYearWeek(year, week, Calendar.FRIDAY));
result.add(getDateOfYearWeek(year, week, Calendar.SATURDAY));
result.add(getDateOfYearWeek(year, week, Calendar.SUNDAY));
return result;
}
示例3: getWeekOfYear
import java.util.Calendar; //導入方法依賴的package包/類
/**
* getWeekOfYear
* 計算當前時間屬於一年的第幾周,並計算出相對於基準時間 offsetDate的周數<br/>
* 如果 基準時間 offsetDate 為 null 則僅返回當前時間屬於一年中的第幾周<br/>
* 一周從 星期一開始計算 <br/>
* 遵循 ISO8601 week algorithm, 即 一年的第一周至少有4天及以上
* @param date
* @param offsetDate
* @return
* @return int 返回類型
* @author wangwei
* @date 2016年6月24日 下午2:39:13
* @version [1.0, 2016年6月24日]
* @since version 1.0
*/
public static int getWeekOfYear(Date date, Date offsetDate)
{
Calendar calendar1 = Calendar.getInstance();
calendar1.setFirstDayOfWeek(Calendar.MONDAY);
calendar1.setMinimalDaysInFirstWeek(4);
calendar1.setTime(date);
int weekOfYear = calendar1.get(Calendar.WEEK_OF_YEAR);
if(null == offsetDate){
return weekOfYear;
}
Calendar calendar2 = Calendar.getInstance();
calendar2.setTime(offsetDate);
calendar2.setFirstDayOfWeek(Calendar.MONDAY);
calendar2.setMinimalDaysInFirstWeek(4);
int weekOfYear2 = calendar2.get(Calendar.WEEK_OF_YEAR);
int tmp = weekOfYear - weekOfYear2 + 1;
return tmp < 0 ? tmp+52 : tmp; // 跨年處理
}
示例4: getWeekInMills
import java.util.Calendar; //導入方法依賴的package包/類
/**
* Get weeks
*
* @param mills mills
* @return weeks
*/
public static int getWeekInMills(long mills) {
Calendar cal = Calendar.getInstance();//This sentence must be set, otherwise, the United States believes that the first day is Sunday, and our country believes that Monday, the calculation of the current date is the first few weeks there will be errors.
cal.setFirstDayOfWeek(Calendar.MONDAY); // The first day of the week set for Monday.
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);// Starting every Monday.
cal.setMinimalDaysInFirstWeek(7); // Set for at least 7 days a week.
cal.setTime(new Date());
int weeks1 = cal.get(Calendar.WEEK_OF_YEAR);
return weeks1;
}
示例5: getWeekOfYear
import java.util.Calendar; //導入方法依賴的package包/類
/**
* 取得當前日期是多少周
*
* @param date
* @return
*/
public static int getWeekOfYear(Date date)
{
Calendar c = new GregorianCalendar();
c.setFirstDayOfWeek(Calendar.MONDAY);
c.setMinimalDaysInFirstWeek(7);
c.setTime(date);
return c.get(Calendar.WEEK_OF_YEAR);
}
示例6: getDateOfYearWeek
import java.util.Calendar; //導入方法依賴的package包/類
/**
* 獲取一年中某周,星期幾的日期
*
* @param yearNum 年
* @param weekNum 那一周
* @param dayOfWeek 周幾
* @return 日期
*/
private static Date getDateOfYearWeek(int yearNum, int weekNum,
int dayOfWeek) {
Calendar cal = Calendar.getInstance();
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.set(Calendar.DAY_OF_WEEK, dayOfWeek);
cal.setMinimalDaysInFirstWeek(DAYS_OF_A_WEEK);
cal.set(Calendar.YEAR, yearNum);
cal.set(Calendar.WEEK_OF_YEAR, weekNum);
/*
* cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0);
* cal.set(Calendar.SECOND, 0);
*/
return cal.getTime();
}
示例7: getCurrentWeekDay
import java.util.Calendar; //導入方法依賴的package包/類
/**
* 本周指定星期幾的日期
*
* @param date 指定的日期
* @param day 那一天
* @return 結果
*/
public static String getCurrentWeekDay(Date date, int day) {
Calendar calendar = getCalendarFromDate(date);
calendar.setFirstDayOfWeek(Calendar.MONDAY);
calendar.setMinimalDaysInFirstWeek(DAYS_OF_A_WEEK);
int week = calendar.get(Calendar.WEEK_OF_YEAR);
int year = calendar.get(Calendar.YEAR);
return formatDate(getDateOfYearWeek(year, week, day));
}
示例8: testWeek12005
import java.util.Calendar; //導入方法依賴的package包/類
/**
* The first week in 2005 should span the range:
*
* TimeZone | Start Millis | End Millis | Start Date | End Date
* -----------------+---------------+---------------+-------------+------------
* Europe/London | 1104710400000 | 1105315199999 | 3-Jan-2005 | 9-Jan-2005
* Europe/Paris | 1104706800000 | 1105311599999 | 3-Jan-2005 | 2-Jan-2005
* America/New_York | 1104037200000 | 1104641999999 | 26-Dec-2004 | 1-Jan-2005
*
* In London and Paris, Monday is the first day of the week, while in the
* US it is Sunday.
*
* Previously, we were using these values, but see Java Bug ID 4960215:
*
* TimeZone | Start Millis | End Millis | Start Date | End Date
* -----------------+---------------+---------------+-------------+------------
* Europe/London | 1104105600000 | 1104710399999 | 27-Dec-2004 | 2-Jan-2005
* Europe/Paris | 1104102000000 | 1104706799999 | 27-Dec-2004 | 2-Jan-2005
* America/New_York | 1104037200000 | 1104641999999 | 26-Dec-2004 | 1-Jan-2005
*/
public void testWeek12005() {
Week w1 = new Week(1, 2005);
Calendar c1 = Calendar.getInstance(
TimeZone.getTimeZone("Europe/London"), Locale.UK);
c1.setMinimalDaysInFirstWeek(4); // see Java Bug ID 4960215
assertEquals(1104710400000L, w1.getFirstMillisecond(c1));
assertEquals(1105315199999L, w1.getLastMillisecond(c1));
Calendar c2 = Calendar.getInstance(
TimeZone.getTimeZone("Europe/Paris"), Locale.FRANCE);
c2.setMinimalDaysInFirstWeek(4); // see Java Bug ID 4960215
assertEquals(1104706800000L, w1.getFirstMillisecond(c2));
assertEquals(1105311599999L, w1.getLastMillisecond(c2));
Calendar c3 = Calendar.getInstance(
TimeZone.getTimeZone("America/New_York"), Locale.US);
assertEquals(1104037200000L, w1.getFirstMillisecond(c3));
assertEquals(1104641999999L, w1.getLastMillisecond(c3));
}
示例9: TestActualMinMax
import java.util.Calendar; //導入方法依賴的package包/類
public void TestActualMinMax() {
Calendar cal = new GregorianCalendar(1967, MARCH, 10);
cal.setFirstDayOfWeek(SUNDAY);
cal.setMinimalDaysInFirstWeek(3);
if (cal.getActualMinimum(DAY_OF_MONTH) != 1) {
errln("Actual minimum date for 3/10/1967 should have been 1; got "
+ cal.getActualMinimum(DAY_OF_MONTH));
}
if (cal.getActualMaximum(DAY_OF_MONTH) != 31) {
errln("Actual maximum date for 3/10/1967 should have been 31; got "
+ cal.getActualMaximum(DAY_OF_MONTH));
}
cal.set(MONTH, FEBRUARY);
if (cal.getActualMaximum(DAY_OF_MONTH) != 28) {
errln("Actual maximum date for 2/10/1967 should have been 28; got "
+ cal.getActualMaximum(DAY_OF_MONTH));
}
if (cal.getActualMaximum(DAY_OF_YEAR) != 365) {
errln("Number of days in 1967 should have been 365; got "
+ cal.getActualMaximum(DAY_OF_YEAR));
}
cal.set(YEAR, 1968);
if (cal.getActualMaximum(DAY_OF_MONTH) != 29) {
errln("Actual maximum date for 2/10/1968 should have been 29; got "
+ cal.getActualMaximum(DAY_OF_MONTH));
}
if (cal.getActualMaximum(DAY_OF_YEAR) != 366) {
errln("Number of days in 1968 should have been 366; got "
+ cal.getActualMaximum(DAY_OF_YEAR));
}
// Using week settings of SUNDAY/3 (see above)
if (cal.getActualMaximum(WEEK_OF_YEAR) != 52) {
errln("Number of weeks in 1968 should have been 52; got "
+ cal.getActualMaximum(WEEK_OF_YEAR));
}
cal.set(YEAR, 1976);
// Using week settings of SUNDAY/3 (see above)
if (cal.getActualMaximum(WEEK_OF_YEAR) != 53) {
errln("Number of weeks in 1976 should have been 53; got "
+ cal.getActualMaximum(WEEK_OF_YEAR));
}
}
示例10: Test4136399
import java.util.Calendar; //導入方法依賴的package包/類
/**
* Calendar and GregorianCalendar hashCode() methods need improvement.
* Calendar needs a good implementation that subclasses can override,
* and GregorianCalendar should use that implementation.
*/
public void Test4136399() {
/* Note: This test is actually more strict than it has to be.
* Technically, there is no requirement that unequal objects have
* unequal hashes. We only require equal objects to have equal hashes.
* It is desirable for unequal objects to have distributed hashes, but
* there is no hard requirement here.
*
* In this test we make assumptions about certain attributes of calendar
* objects getting represented in the hash, which need not always be the
* case (although it does work currently with the given test). */
Calendar a = Calendar.getInstance();
Calendar b = (Calendar) a.clone();
if (a.hashCode() != b.hashCode()) {
errln("Calendar hash code unequal for cloned objects");
}
b.setMinimalDaysInFirstWeek(7 - a.getMinimalDaysInFirstWeek());
if (a.hashCode() == b.hashCode()) {
errln("Calendar hash code ignores minimal days in first week");
}
b.setMinimalDaysInFirstWeek(a.getMinimalDaysInFirstWeek());
b.setFirstDayOfWeek((a.getFirstDayOfWeek() % 7) + 1); // Next day
if (a.hashCode() == b.hashCode()) {
errln("Calendar hash code ignores first day of week");
}
b.setFirstDayOfWeek(a.getFirstDayOfWeek());
b.setLenient(!a.isLenient());
if (a.hashCode() == b.hashCode()) {
errln("Calendar hash code ignores lenient setting");
}
b.setLenient(a.isLenient());
// Assume getTimeZone() returns a reference, not a clone
// of a reference -- this is true as of this writing
b.getTimeZone().setRawOffset(a.getTimeZone().getRawOffset() + 60 * 60 * 1000);
if (a.hashCode() == b.hashCode()) {
errln("Calendar hash code ignores zone");
}
b.getTimeZone().setRawOffset(a.getTimeZone().getRawOffset());
GregorianCalendar c = new GregorianCalendar();
GregorianCalendar d = (GregorianCalendar) c.clone();
if (c.hashCode() != d.hashCode()) {
errln("GregorianCalendar hash code unequal for clones objects");
}
Date cutover = c.getGregorianChange();
d.setGregorianChange(new Date(cutover.getTime() + 24 * 60 * 60 * 1000));
if (c.hashCode() == d.hashCode()) {
errln("GregorianCalendar hash code ignores cutover");
}
}
示例11: getDayOfWeek
import java.util.Calendar; //導入方法依賴的package包/類
/**
* 獲取指定日期是一周的第幾天,星期日是第一天
*
* @param date 日期
* @return 結果
*/
public static int getDayOfWeek(Date date) {
Calendar calendar = getCalendarFromDate(date);
calendar.setMinimalDaysInFirstWeek(DAYS_OF_A_WEEK);
return calendar.get(Calendar.DAY_OF_WEEK) - 1;
}