當前位置: 首頁>>代碼示例>>Java>>正文


Java GregorianCalendar.set方法代碼示例

本文整理匯總了Java中java.util.GregorianCalendar.set方法的典型用法代碼示例。如果您正苦於以下問題:Java GregorianCalendar.set方法的具體用法?Java GregorianCalendar.set怎麽用?Java GregorianCalendar.set使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.util.GregorianCalendar的用法示例。


在下文中一共展示了GregorianCalendar.set方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: getDateValue

import java.util.GregorianCalendar; //導入方法依賴的package包/類
private Date getDateValue(int year, int month, int day, int hours, int minutes, int sec, int msec, int offsetInMinutes)
{
    // minute in millis
    int millisInMinute = 1000 * 60;

    GregorianCalendar gc = new GregorianCalendar();

    // set correct offset
    String[] tzArray = TimeZone.getAvailableIDs(millisInMinute * offsetInMinutes);
    if (tzArray.length > 0)
    {
        gc.setTimeZone(TimeZone.getTimeZone(tzArray[0]));
    }

    // set date
    gc.set(GregorianCalendar.YEAR, year);
    gc.set(GregorianCalendar.MONTH, month - 1);
    gc.set(GregorianCalendar.DAY_OF_MONTH, day);
    gc.set(GregorianCalendar.HOUR_OF_DAY, hours);
    gc.set(GregorianCalendar.MINUTE, minutes);
    gc.set(GregorianCalendar.SECOND, sec);
    gc.set(GregorianCalendar.MILLISECOND, msec);

    return gc.getTime();
}
 
開發者ID:Alfresco,項目名稱:alfresco-core,代碼行數:26,代碼來源:ISO8601DateFormatTest.java

示例2: isBefore

import java.util.GregorianCalendar; //導入方法依賴的package包/類
/**
 * Compare two dates without looking at their time component
 * @param one the first date to compare
 * @param two the second date to compare
 * @return whether the first date is before the second one
 */
public static boolean isBefore(Date one, Date two){
    if (one == null || two == null)
        return false;

    GregorianCalendar gc1 = new GregorianCalendar();
    gc1.setTime(one);
    gc1.set(Calendar.HOUR, 2);
    gc1.set(Calendar.MINUTE, 1);
    gc1.set(Calendar.SECOND, 1);

    GregorianCalendar gc2 = new GregorianCalendar();
    gc2.setTime(two);
    gc2.set(Calendar.HOUR, 2);
    gc2.set(Calendar.MINUTE, 1);
    gc2.set(Calendar.SECOND, 1);

    return gc1.getTime().before(gc2.getTime());
}
 
開發者ID:CMPUT301F17T15,項目名稱:CIA,代碼行數:25,代碼來源:DateUtilities.java

示例3: test_IsoChrono_vsCalendar

import java.util.GregorianCalendar; //導入方法依賴的package包/類
@Test(dataProvider = "RangeVersusCalendar")
public void test_IsoChrono_vsCalendar(LocalDate isoStartDate, LocalDate isoEndDate) {
    GregorianCalendar cal = new GregorianCalendar();
    assertEquals(cal.getCalendarType(), "gregory", "Unexpected calendar type");
    LocalDate isoDate = IsoChronology.INSTANCE.date(isoStartDate);

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

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

        isoDate = isoDate.plus(1, ChronoUnit.DAYS);
        cal.add(Calendar.DAY_OF_MONTH, 1);
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:21,代碼來源:TestIsoChronoImpl.java

示例4: _getDates

import java.util.GregorianCalendar; //導入方法依賴的package包/類
private static Date[] _getDates(
  Locale[] locales,
  TimeZone[] timeZones,
  int[] day,
  int[] month,
  int[] year,
  int[] hours,
  int[] mins,
  int[] secs
  )
{
  Date[] dates = new Date[timeZones.length];
  for (int i = 0; i < timeZones.length; ++i)
  {
    GregorianCalendar gcal = new GregorianCalendar();
    gcal.set(year[i],month[i],day[i],hours[i],mins[i],secs[i]);
    gcal.setTimeZone(timeZones[i]);
    dates[i] = gcal.getTime();
  }
  return dates;
}
 
開發者ID:apache,項目名稱:myfaces-trinidad,代碼行數:22,代碼來源:DateTimeConverterTestCase.java

示例5: getToday

import java.util.GregorianCalendar; //導入方法依賴的package包/類
Pair<Date, Date> getToday() {
    final GregorianCalendar today = new GregorianCalendar();
    today.set(Calendar.HOUR_OF_DAY, today.getActualMaximum(Calendar.HOUR_OF_DAY));
    today.set(Calendar.MINUTE, today.getActualMaximum(Calendar.MINUTE));
    today.set(Calendar.SECOND, today.getActualMaximum(Calendar.SECOND));
    today.set(Calendar.MILLISECOND, today.getActualMaximum(Calendar.MILLISECOND));

    final Date end = today.getTime();
    today.add(Calendar.DATE, -1);
    today.set(Calendar.HOUR_OF_DAY, 0);
    today.set(Calendar.MINUTE, 0);
    today.set(Calendar.SECOND, 0);
    today.set(Calendar.MILLISECOND, 0);

    return new Pair<>(today.getTime(), end);
}
 
開發者ID:PacktPublishing,項目名稱:Hands-On-Android-UI-Development,代碼行數:17,代碼來源:AllowanceOverviewPresenter.java

示例6: handleDateTimeByYear

import java.util.GregorianCalendar; //導入方法依賴的package包/類
/**
 * <pre>
 * 描述:間隔指定年數的指定月份指定周數指定星期數後日期(例如:每年1月份第一個星期一)
 * 作者:ZhangYi
 * 時間:2016年5月5日 下午4:29:07
 * 參數:(參數列表)
 * @param dateTime	指定日期
 * @param interval	間隔年(間隔幾年)
 * @param month		指定月份(1-12:月份)
 * @param num		指定周數(1-4:第幾個星期)
 * @param week		指定周幾(1-7:周一至周日,-1:不指定周幾[默認星期一])
 * @return
 * </pre>
 */
public static Date handleDateTimeByYear(Date dateTime, int interval, int month, int num, int week) {
	try {
		GregorianCalendar calendar = new GregorianCalendar();
		calendar.setTime(dateTime);
		calendar.add(Calendar.YEAR, interval);
		calendar.set(Calendar.MONTH, month - 1);
		if (num < 0) {// 最後一個星期
			calendar.set(Calendar.DAY_OF_WEEK_IN_MONTH, -1);
		} else {
			calendar.set(Calendar.DAY_OF_WEEK_IN_MONTH, num);
		}
		if (week < 0) {// [默認星期一]
			calendar.set(Calendar.DAY_OF_WEEK, 1 % 7 + 1);
		} else {
			calendar.set(Calendar.DAY_OF_WEEK, week % 7 + 1);
		}
		dateTime = calendar.getTime();
	} catch (Exception e) {
		logger.error("--間隔指定年數的指定月份指定周數指定星期數後日期異常!", e);
	}
	return dateTime;
}
 
開發者ID:dev-share,項目名稱:database-transform-tool,代碼行數:37,代碼來源:DateUtil.java

示例7: getThisWeek

import java.util.GregorianCalendar; //導入方法依賴的package包/類
Pair<Date, Date> getThisWeek() {
    final GregorianCalendar today = new GregorianCalendar();
    today.set(Calendar.HOUR_OF_DAY, today.getActualMaximum(Calendar.HOUR_OF_DAY));
    today.set(Calendar.MINUTE, today.getActualMaximum(Calendar.MINUTE));
    today.set(Calendar.SECOND, today.getActualMaximum(Calendar.SECOND));
    today.set(Calendar.MILLISECOND, today.getActualMaximum(Calendar.MILLISECOND));

    final Date end = today.getTime();
    today.add(Calendar.DATE, -(today.get(Calendar.DAY_OF_WEEK) - Calendar.SUNDAY));

    today.set(Calendar.HOUR_OF_DAY, 0);
    today.set(Calendar.MINUTE, 0);
    today.set(Calendar.SECOND, 0);
    today.set(Calendar.MILLISECOND, 0);

    return new Pair<>(today.getTime(), end);
}
 
開發者ID:PacktPublishing,項目名稱:Hands-On-Android-UI-Development,代碼行數:18,代碼來源:AllowanceOverviewPresenter.java

示例8: TestRog

import java.util.GregorianCalendar; //導入方法依賴的package包/類
public void TestRog() {
    GregorianCalendar gc = new GregorianCalendar();

    int year = 1997, month = APRIL, date = 1;
    gc.set(year, month, date); // April 1, 1997

    gc.set(HOUR_OF_DAY, 23);
    gc.set(MINUTE, 0);
    gc.set(SECOND, 0);
    gc.set(MILLISECOND, 0);

    for (int i = 0; i < 9; i++, gc.add(DATE, 1)) {
        if (gc.get(YEAR) != year
                || gc.get(MONTH) != month
                || gc.get(DATE) != (date + i)) {
            errln("FAIL: Date " + gc.getTime() + " wrong");
        }
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:20,代碼來源:CalendarTest.java

示例9: handleDateTimeByMonth

import java.util.GregorianCalendar; //導入方法依賴的package包/類
/**
 * <pre>
 * 描述:間隔指定月數的指定周數指定星期數後日期(例如:每3個月第一個星期一)
 * 作者:ZhangYi
 * 時間:2016年5月5日 下午4:29:07
 * 參數:(參數列表)
 * @param dateTime	指定日期
 * @param interval	間隔月數(間隔幾個月)
 * @param num		指定周數(1-4:第幾個星期)
 * @param week		指定周幾(1-7:周一至周日,-1:不指定周幾(JDK默認星期一))
 * @return
 * </pre>
 */
public static Date handleDateTimeByMonth(Date dateTime, int interval, int num, int week) {
	try {
		GregorianCalendar calendar = new GregorianCalendar();
		calendar.setTime(dateTime);
		calendar.add(Calendar.MONTH, interval);
		if (num < 0) {// 最後一個星期
			calendar.set(Calendar.DAY_OF_WEEK_IN_MONTH, -1);
		} else {
			calendar.set(Calendar.DAY_OF_WEEK_IN_MONTH, num);
		}
		if (week < 0) {// [默認星期一]
			calendar.set(Calendar.DAY_OF_WEEK, 1 % 7 + 1);
		} else {
			calendar.set(Calendar.DAY_OF_WEEK, week % 7 + 1);
		}
		dateTime = calendar.getTime();
	} catch (Exception e) {
		logger.error("--間隔指定月數的指定周數指定星期數後日期異常!", e);
	}
	return dateTime;
}
 
開發者ID:dev-share,項目名稱:database-transform-tool,代碼行數:35,代碼來源:DateUtil.java

示例10: TestFieldSet4781

import java.util.GregorianCalendar; //導入方法依賴的package包/類
public void TestFieldSet4781() {
    try {
        GregorianCalendar g = new GregorianCalendar();
        GregorianCalendar g2 = new GregorianCalendar();
        // At this point UTC value is set, various fields are not.
        // Now set to noon.
        g2.set(HOUR, 12);
        g2.set(MINUTE, 0);
        g2.set(SECOND, 0);
        // At this point the object thinks UTC is NOT set, but fields are set.
        // The following line will result in IllegalArgumentException because
        // it thinks the YEAR is set and it is NOT.
        if (g2.equals(g)) {
            logln("Same");
        } else {
            logln("Different");
        }
    } catch (IllegalArgumentException e) {
        errln("Unexpected exception seen: " + e);
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:22,代碼來源:CalendarTest.java

示例11: Test4162587

import java.util.GregorianCalendar; //導入方法依賴的package包/類
/**
 * Calendar and Date HOUR broken.  If HOUR is out-of-range, Calendar
 * and Date classes will misbehave.
 */
public void Test4162587() {
    TimeZone savedTz = TimeZone.getDefault();
    TimeZone tz = TimeZone.getTimeZone("PST");
    TimeZone.setDefault(tz);
    GregorianCalendar cal = new GregorianCalendar(tz);
    Date d;

    try {
        for (int i = 0; i < 5; ++i) {
            if (i > 0) {
                logln("---");
            }

            cal.clear();
            cal.set(1998, APRIL, 5, i, 0);
            d = cal.getTime();
            String s0 = d.toString();
            logln("0 " + i + ": " + s0);

            cal.clear();
            cal.set(1998, APRIL, 4, i + 24, 0);
            d = cal.getTime();
            String sPlus = d.toString();
            logln("+ " + i + ": " + sPlus);

            cal.clear();
            cal.set(1998, APRIL, 6, i - 24, 0);
            d = cal.getTime();
            String sMinus = d.toString();
            logln("- " + i + ": " + sMinus);

            if (!s0.equals(sPlus) || !s0.equals(sMinus)) {
                errln("Fail: All three lines must match");
            }
        }
    } finally {
        TimeZone.setDefault(savedTz);
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:44,代碼來源:CalendarRegression.java

示例12: testPreviousStandardDateDay

import java.util.GregorianCalendar; //導入方法依賴的package包/類
/**
 * A basic check for the testPreviousStandardDate() method when the
 * tick unit is 1 day.
 */
public void testPreviousStandardDateDay() {
    MyDateAxis axis = new MyDateAxis("Day");
    GregorianCalendar calendar = new GregorianCalendar();
    calendar.set(2006, Calendar.AUGUST, 25, 12, 0);
    Date d1 = calendar.getTime();
    calendar.set(2007, Calendar.MAY, 20, 12, 0);
    Date d2 = calendar.getTime();
    axis.setRange(d1, d2);
    DateTickUnit unit = new DateTickUnit(DateTickUnit.DAY, 1);
    axis.setTickUnit(unit);
    
    axis.setTickMarkPosition(DateTickMarkPosition.START);
    Date psd1 = axis.previousStandardDate(d1, unit);
    calendar.setTime(psd1);
    assertEquals(2006, calendar.get(Calendar.YEAR));
    assertEquals(Calendar.AUGUST, calendar.get(Calendar.MONTH));
    assertEquals(25, calendar.get(Calendar.DATE));
    
    axis.setTickMarkPosition(DateTickMarkPosition.MIDDLE);
    Date psd2 = axis.previousStandardDate(d1, unit);
    calendar.setTime(psd2);
    assertEquals(2006, calendar.get(Calendar.YEAR));
    assertEquals(Calendar.AUGUST, calendar.get(Calendar.MONTH));
    assertEquals(25, calendar.get(Calendar.DATE));

    axis.setTickMarkPosition(DateTickMarkPosition.END);
    Date psd3 = axis.previousStandardDate(d1, unit);
    calendar.setTime(psd3);
    assertEquals(2006, calendar.get(Calendar.YEAR));
    assertEquals(Calendar.AUGUST, calendar.get(Calendar.MONTH));
    assertEquals(24, calendar.get(Calendar.DATE));
}
 
開發者ID:parabuild-ci,項目名稱:parabuild-ci,代碼行數:37,代碼來源:DateAxisTests.java

示例13: testBug7686

import java.util.GregorianCalendar; //導入方法依賴的package包/類
public void testBug7686() throws SQLException {
    String tableName = "testBug7686";
    createTable(tableName, "(id1 int(10) unsigned NOT NULL, id2 DATETIME, field1 varchar(128) NOT NULL default '', PRIMARY KEY  (id1, id2))", "InnoDB;");

    this.stmt.executeUpdate("insert into " + tableName + " (id1, id2, field1) values (1, '2005-01-05 13:59:20', 'foo')");

    String sQuery = " SELECT * FROM " + tableName + " WHERE id1 = ? AND id2 = ?";
    this.pstmt = this.conn.prepareStatement(sQuery, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);

    this.conn.setAutoCommit(false);
    this.pstmt.setInt(1, 1);
    GregorianCalendar cal = new GregorianCalendar();
    cal.clear();
    cal.set(2005, 00, 05, 13, 59, 20);

    Timestamp jan5before2pm = null;
    jan5before2pm = new java.sql.Timestamp(cal.getTimeInMillis());

    this.pstmt.setTimestamp(2, jan5before2pm);
    this.rs = this.pstmt.executeQuery();
    assertTrue(this.rs.next());
    this.rs.absolute(1);
    this.rs.updateString("field1", "bar");
    this.rs.updateRow();
    this.conn.commit();
    this.conn.setAutoCommit(true);

}
 
開發者ID:bragex,項目名稱:the-vigilantes,代碼行數:29,代碼來源:ResultSetRegressionTest.java

示例14: testPreviousStandardDateHour

import java.util.GregorianCalendar; //導入方法依賴的package包/類
/**
 * A basic check for the testPreviousStandardDate() method when the
 * tick unit is 1 hour.
 */
public void testPreviousStandardDateHour() {
    MyDateAxis axis = new MyDateAxis("Hour");
    GregorianCalendar calendar = new GregorianCalendar();
    calendar.set(2006, Calendar.AUGUST, 25, 12, 0);
    Date d1 = calendar.getTime();
    calendar.set(2007, Calendar.MAY, 20, 12, 0);
    Date d2 = calendar.getTime();
    axis.setRange(d1, d2);
    DateTickUnit unit = new DateTickUnit(DateTickUnit.HOUR, 1);
    axis.setTickUnit(unit);
    
    axis.setTickMarkPosition(DateTickMarkPosition.START);
    Date psd1 = axis.previousStandardDate(d1, unit);
    calendar.setTime(psd1);
    assertEquals(2006, calendar.get(Calendar.YEAR));
    assertEquals(Calendar.AUGUST, calendar.get(Calendar.MONTH));
    assertEquals(25, calendar.get(Calendar.DATE));
    assertEquals(12, calendar.get(Calendar.HOUR_OF_DAY));
    
    axis.setTickMarkPosition(DateTickMarkPosition.MIDDLE);
    Date psd2 = axis.previousStandardDate(d1, unit);
    calendar.setTime(psd2);
    assertEquals(2006, calendar.get(Calendar.YEAR));
    assertEquals(Calendar.AUGUST, calendar.get(Calendar.MONTH));
    assertEquals(25, calendar.get(Calendar.DATE));
    assertEquals(11, calendar.get(Calendar.HOUR_OF_DAY));

    axis.setTickMarkPosition(DateTickMarkPosition.END);
    Date psd3 = axis.previousStandardDate(d1, unit);
    calendar.setTime(psd3);
    assertEquals(2006, calendar.get(Calendar.YEAR));
    assertEquals(Calendar.AUGUST, calendar.get(Calendar.MONTH));
    assertEquals(25, calendar.get(Calendar.DATE));
    assertEquals(11, calendar.get(Calendar.HOUR_OF_DAY));
}
 
開發者ID:parabuild-ci,項目名稱:parabuild-ci,代碼行數:40,代碼來源:DateAxisTests.java

示例15: testPreviousStandardDateMinute

import java.util.GregorianCalendar; //導入方法依賴的package包/類
/**
 * A basic check for the testPreviousStandardDate() method when the
 * tick unit is 1 minute.
 */
public void testPreviousStandardDateMinute() {
    MyDateAxis axis = new MyDateAxis("Minute");
    GregorianCalendar calendar = new GregorianCalendar();
    calendar.set(2006, Calendar.AUGUST, 25, 12, 0, 10);
    Date d1 = calendar.getTime();
    calendar.set(2007, Calendar.MAY, 20, 12, 0, 10);
    Date d2 = calendar.getTime();
    axis.setRange(d1, d2);
    DateTickUnit unit = new DateTickUnit(DateTickUnit.MINUTE, 1);
    axis.setTickUnit(unit);
    
    axis.setTickMarkPosition(DateTickMarkPosition.START);
    Date psd1 = axis.previousStandardDate(d1, unit);
    calendar.setTime(psd1);
    assertEquals(2006, calendar.get(Calendar.YEAR));
    assertEquals(Calendar.AUGUST, calendar.get(Calendar.MONTH));
    assertEquals(25, calendar.get(Calendar.DATE));
    assertEquals(12, calendar.get(Calendar.HOUR_OF_DAY));
    assertEquals(0, calendar.get(Calendar.MINUTE));
    
    axis.setTickMarkPosition(DateTickMarkPosition.MIDDLE);
    Date psd2 = axis.previousStandardDate(d1, unit);
    calendar.setTime(psd2);
    assertEquals(2006, calendar.get(Calendar.YEAR));
    assertEquals(Calendar.AUGUST, calendar.get(Calendar.MONTH));
    assertEquals(25, calendar.get(Calendar.DATE));
    assertEquals(11, calendar.get(Calendar.HOUR_OF_DAY));
    assertEquals(59, calendar.get(Calendar.MINUTE));

    axis.setTickMarkPosition(DateTickMarkPosition.END);
    Date psd3 = axis.previousStandardDate(d1, unit);
    calendar.setTime(psd3);
    assertEquals(2006, calendar.get(Calendar.YEAR));
    assertEquals(Calendar.AUGUST, calendar.get(Calendar.MONTH));
    assertEquals(25, calendar.get(Calendar.DATE));
    assertEquals(11, calendar.get(Calendar.HOUR_OF_DAY));
    assertEquals(59, calendar.get(Calendar.MINUTE));
}
 
開發者ID:parabuild-ci,項目名稱:parabuild-ci,代碼行數:43,代碼來源:DateAxisTests.java


注:本文中的java.util.GregorianCalendar.set方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。