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


Java Calendar.set方法代碼示例

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


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

示例1: addDayOfMonth

import java.util.Calendar; //導入方法依賴的package包/類
/**
 * 日期加上天數
 * @param date
 * @param dateFormat
 * @param numDay
 * @return
 */
public String addDayOfMonth(String date, DateFormat dateFormat, int numDay){

    SimpleDateFormat df = new SimpleDateFormat(dateFormat.getValue());
    Calendar c = Calendar.getInstance();
    try {
        c.setTime(df.parse(date));
        c.set(Calendar.DAY_OF_MONTH, c.get(Calendar.DAY_OF_MONTH) + numDay);

        return df.format(c.getTime());
    } catch (ParseException e) {
        e.printStackTrace();
        System.err.print(e);
        return null;
    }
}
 
開發者ID:Shmilyz,項目名稱:Swap,代碼行數:23,代碼來源:DateUtil.java

示例2: start

import java.util.Calendar; //導入方法依賴的package包/類
public static void start(Context ctx) {
    AlarmManager manager = (AlarmManager) ctx.getSystemService("alarm");
    PendingIntent pending = PendingIntent.getBroadcast(ctx, 152, new Intent(ctx,
            SportRemindReceiver.class), 134217728);
    Helper.showLog(TAG, "SportRemindReceiver start");
    Calendar calNow = Calendar.getInstance();
    Calendar calSet = (Calendar) calNow.clone();
    calSet.setTimeInMillis(System.currentTimeMillis());
    int remindHour = 20;
    int remindMinute = 30;
    if (!TextUtil.isEmpty(OnePreference.getPrefSportRemindTime())) {
        String[] times = OnePreference.getPrefSportRemindTime().split("#");
        if (times.length == 2) {
            remindHour = Integer.parseInt(times[0]);
            remindMinute = Integer.parseInt(times[1]);
        }
    }
    calSet.set(11, remindHour);
    calSet.set(12, remindMinute);
    calSet.set(13, 0);
    calSet.set(14, 0);
    if (calSet.compareTo(calNow) <= 0) {
        calSet.add(5, 1);
    }
    manager.setRepeating(0, calSet.getTimeInMillis(), a.h, pending);
}
 
開發者ID:JackChan1999,項目名稱:boohee_v5.6,代碼行數:27,代碼來源:SportRemindReceiver.java

示例3: calculateAlarm

import java.util.Calendar; //導入方法依賴的package包/類
/**
 * Given an alarm in hours and minutes, return a time suitable for
 * setting in AlarmManager.
 */
static Calendar calculateAlarm(int hour, int minute,
                               Alarm.DaysOfWeek daysOfWeek) {

  // start with now
  Calendar c = Calendar.getInstance();
  c.setTimeInMillis(System.currentTimeMillis());

  int nowHour = c.get(Calendar.HOUR_OF_DAY);
  int nowMinute = c.get(Calendar.MINUTE);

  // if alarm is behind current time, advance one day
  if (hour < nowHour
      || hour == nowHour && minute <= nowMinute) {
    c.add(Calendar.DAY_OF_YEAR, 1);
  }
  c.set(Calendar.HOUR_OF_DAY, hour);
  c.set(Calendar.MINUTE, minute);
  c.set(Calendar.SECOND, 0);
  c.set(Calendar.MILLISECOND, 0);

  int addDays = daysOfWeek.getNextAlarm(c);
  if (addDays > 0) c.add(Calendar.DAY_OF_WEEK, addDays);
  return c;
}
 
開發者ID:nordfalk,項目名稱:EsperantoRadio,代碼行數:29,代碼來源:Alarms.java

示例4: getSomeMonthsBefore

import java.util.Calendar; //導入方法依賴的package包/類
/**
 * 獲取本日的n個月前的日期,如本日=2015-03-01,n=3,則輸出2014-12-01
 *
 * @param n
 * @return
 */
public static Date getSomeMonthsBefore(int n) {
    Calendar cl = Calendar.getInstance();
    cl.set(Calendar.HOUR_OF_DAY, 0);
    cl.set(Calendar.MINUTE, 0);
    cl.set(Calendar.SECOND, 0);
    cl.set(Calendar.MILLISECOND, 0);

    int year = cl.get(Calendar.YEAR);
    int month = cl.get(Calendar.MONTH);
    int day = cl.get(Calendar.DAY_OF_MONTH);
    int by = n / 12;
    int bm = n % 12;
    if (by > 0) {
        year -= by;
    }
    if (bm > month) {
        year--;
    }
    month = (12 + month - bm) % 12;
    cl.set(Calendar.YEAR, year);
    cl.set(Calendar.MONTH, month);
    cl.set(Calendar.DAY_OF_MONTH, 1);
    if (day > cl.getActualMaximum(Calendar.DAY_OF_MONTH)) {//cl為n個月前的1號,如果該月的最大日數比現在的日數小,則取當月的最大日數
        cl.set(Calendar.DAY_OF_MONTH, cl.getActualMaximum(Calendar.DAY_OF_MONTH));
    } else {
        cl.set(Calendar.DAY_OF_MONTH, day);
    }
    return cl.getTime();
}
 
開發者ID:LingjuAI,項目名稱:AssistantBySDK,代碼行數:36,代碼來源:TimeUtils.java

示例5: getLastYearMonths

import java.util.Calendar; //導入方法依賴的package包/類
/**
 * 獲取最近1年所有月份數據
 * 
 * @return
 */
public static List<String> getLastYearMonths() {
	List<String> list_res = new ArrayList<String>();
	Calendar cal = Calendar.getInstance();
	cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) + 1); // 要先+1,才能把本月的算進去</span>
	for (int i = 0; i < 12; i++) {
		cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) - 1); // 逐次往前推1個月
		list_res.add(cal.get(Calendar.YEAR) + "-" + fillZero((cal.get(Calendar.MONTH) + 1) + "", 2));
	}
	return list_res;
}
 
開發者ID:noseparte,項目名稱:Spring-Boot-Server,代碼行數:16,代碼來源:DateUtils.java

示例6: setStartTime

import java.util.Calendar; //導入方法依賴的package包/類
private synchronized static void setStartTime(long millis) {
    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(millis);
    cal.set(Calendar.DAY_OF_MONTH, 1);
    cal.set(Calendar.HOUR, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    startTime = cal.getTimeInMillis();
    cal.add(Calendar.MONTH, 1);
    nextStartTime = cal.getTimeInMillis();
}
 
開發者ID:lyy4j,項目名稱:rmq4note,代碼行數:13,代碼來源:MessageClientIDSetter.java

示例7: hourEndTime

import java.util.Calendar; //導入方法依賴的package包/類
public static Date hourEndTime(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.set(Calendar.MINUTE, 59);
    calendar.set(Calendar.SECOND, 59);
    calendar.set(Calendar.MILLISECOND, 0);
    return calendar.getTime();
}
 
開發者ID:Wangsu-Cloud-Storage,項目名稱:wcs-android-sdk,代碼行數:9,代碼來源:DateUtil.java

示例8: loadMonth

import java.util.Calendar; //導入方法依賴的package包/類
/**
 * This method fill calendar GridView with days
 *
 * @param position Position of current page in ViewPager
 */
private void loadMonth(int position) {
    ArrayList<Date> days = new ArrayList<>();

    // Get Calendar object instance
    Calendar calendar = (Calendar) mCalendarProperties.getCurrentDate().clone();

    // Add months to Calendar (a number of months depends on ViewPager position)
    calendar.add(Calendar.MONTH, position);

    // Set day of month as 1
    calendar.set(Calendar.DAY_OF_MONTH, 1);

    // Get a number of the first day of the week
    int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);

    // Count when month is beginning
    int monthBeginningCell = dayOfWeek + (dayOfWeek == 1 ? 5 : -2);

    // Subtract a number of beginning days, it will let to load a part of a previous month
    calendar.add(Calendar.DAY_OF_MONTH, -monthBeginningCell);

    /*
    Get all days of one page (42 is a number of all possible cells in one page
    (a part of previous month, current month and a part of next month))
     */
    while (days.size() < 42) {
        days.add(calendar.getTime());
        calendar.add(Calendar.DAY_OF_MONTH, 1);
    }

    mPageMonth = calendar.get(Calendar.MONTH) - 1;
    CalendarDayAdapter calendarDayAdapter = new CalendarDayAdapter(this, mContext,
            mCalendarProperties, days, mPageMonth);

    mCalendarGridView.setAdapter(calendarDayAdapter);
}
 
開發者ID:Applandeo,項目名稱:Material-Calendar-View,代碼行數:42,代碼來源:CalendarPageAdapter.java

示例9: getFirstDayOfMonth

import java.util.Calendar; //導入方法依賴的package包/類
/**
 * 返回月的第一天
 * 
 * @return
 */
public static long getFirstDayOfMonth()
{
	Calendar calendar = new GregorianCalendar();

	calendar.set(Calendar.DAY_OF_MONTH, 1);

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

	Log.D(TAG, "getFirstDayOfMonth: " + calendar.getTime());
	return calendar.getTimeInMillis();
}
 
開發者ID:benniaobuguai,項目名稱:android-project-gallery,代碼行數:20,代碼來源:DateUtils.java

示例10: testBug15604

import java.util.Calendar; //導入方法依賴的package包/類
public void testBug15604() throws Exception {
    createTable("testBug15604_date_cal", "(field1 DATE)");
    Properties props = new Properties();
    props.setProperty("useLegacyDatetimeCode", "false");
    props.setProperty("sessionVariables", "time_zone='America/Chicago'");

    Connection nonLegacyConn = getConnectionWithProps(props);

    Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));

    cal.set(Calendar.YEAR, 2005);
    cal.set(Calendar.MONTH, 4);
    cal.set(Calendar.DAY_OF_MONTH, 15);
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);

    java.sql.Date sqlDate = new java.sql.Date(cal.getTime().getTime());

    Calendar cal2 = Calendar.getInstance();
    cal2.setTime(sqlDate);
    System.out.println(new java.sql.Date(cal2.getTime().getTime()));
    this.pstmt = nonLegacyConn.prepareStatement("INSERT INTO testBug15604_date_cal VALUES (?)");

    this.pstmt.setDate(1, sqlDate, cal);
    this.pstmt.executeUpdate();
    this.rs = nonLegacyConn.createStatement().executeQuery("SELECT field1 FROM testBug15604_date_cal");
    this.rs.next();

    assertEquals(sqlDate.getTime(), this.rs.getDate(1, cal).getTime());
}
 
開發者ID:JuanJoseFJ,項目名稱:ProyectoPacientes,代碼行數:33,代碼來源:ResultSetRegressionTest.java

示例11: bcdbuf2Date

import java.util.Calendar; //導入方法依賴的package包/類
/**
 * Translate bcd buffer to GMT time
 *
 * @param buffer byte[]{year-2000,month,day_of_month,hour_of_day,minute,seconds}
 * @param offset The postion where buffer to translate from
 * @return The GMT time encode by bcd
 * @throws Exception The time need 6 unsigned8bit at least, if the buffer avaible size less than 6, this will happen
 */
public static long bcdbuf2Date(byte[] buffer, int offset) throws Exception {
    if (buffer.length - offset < 6) {
        throw new Exception("buffer length is not enough");
    }
    Calendar now = Calendar.getInstance();
    now.set(Calendar.YEAR, BCDUtil.bcd2value(buffer, offset + 0, 1) + 2000);
    now.set(Calendar.MONTH, BCDUtil.bcd2value(buffer, offset + 1, 1) - 1);
    now.set(Calendar.DAY_OF_MONTH, BCDUtil.bcd2value(buffer, offset + 2, 1));
    now.set(Calendar.HOUR_OF_DAY, BCDUtil.bcd2value(buffer, offset + 3, 1));
    now.set(Calendar.MINUTE, BCDUtil.bcd2value(buffer, offset + 4, 1));
    now.set(Calendar.SECOND, BCDUtil.bcd2value(buffer, offset + 5, 1));
    now.set(Calendar.MILLISECOND, 0);
    return now.getTimeInMillis();
}
 
開發者ID:ioprotocol,項目名稱:engine,代碼行數:23,代碼來源:ByteBufferUtil.java

示例12: test50

import java.util.Calendar; //導入方法依賴的package包/類
@Test
public void test50() {
    Calendar cal = Calendar.getInstance();
    Timestamp ts1 = new Timestamp(System.currentTimeMillis());
    cal.setTimeInMillis(ts1.getTime());
    cal.add(Calendar.MONTH, -1);
    cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
    Timestamp ts2 = new Timestamp(cal.getTimeInMillis());
    assertTrue(!ts1.equals(ts2) && ts1.equals(ts1));
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:11,代碼來源:TimestampTests.java

示例13: daylightSavingHourShiftOccurredAndAdvanceNeeded

import java.util.Calendar; //導入方法依賴的package包/類
private boolean daylightSavingHourShiftOccurredAndAdvanceNeeded(Calendar newTime, int initialHourOfDay, Date afterTime) {
    if(isPreserveHourOfDayAcrossDaylightSavings() && newTime.get(Calendar.HOUR_OF_DAY) != initialHourOfDay) {
        newTime.set(Calendar.HOUR_OF_DAY, initialHourOfDay);
        if (newTime.get(Calendar.HOUR_OF_DAY) != initialHourOfDay) {
            return isSkipDayIfHourDoesNotExist();
        } else {
            return !newTime.getTime().after(afterTime);
        }
    }
    return false;
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:12,代碼來源:CalendarIntervalTriggerImpl.java

示例14: getMonthStartDate

import java.util.Calendar; //導入方法依賴的package包/類
/**
 * 獲取本月的第一天
 *
 * @return
 */
public static String getMonthStartDate() {
    SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd");
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.DAY_OF_MONTH, 1);
    cal.getTime();
    return dateFormater.format(cal.getTime()) + "";
}
 
開發者ID:tututututututu,項目名稱:BaseCore,代碼行數:13,代碼來源:TimeUtils.java

示例15: getTime

import java.util.Calendar; //導入方法依賴的package包/類
/**
 * 根據年月日時分秒毫秒獲取時間
 * @param year
 * @param month
 * @param date
 * @param hour
 * @param minute
 * @param second
 * @param millisecond
 * @return
 */
public static long getTime(int year, int month, int date, int hour, int minute, int second, int millisecond) {
	Calendar calendar = Calendar.getInstance();
	calendar.set(Calendar.YEAR, year);
	calendar.set(Calendar.MONTH, month - 1 + Calendar.JANUARY);
	calendar.set(Calendar.DATE, date);
	calendar.set(Calendar.HOUR_OF_DAY, hour);
	calendar.set(Calendar.MINUTE, minute);
	calendar.set(Calendar.SECOND, second);
	calendar.set(Calendar.MILLISECOND, millisecond);
	return calendar.getTimeInMillis();
}
 
開發者ID:AnnyBaby,項目名稱:Mvp-Retrofit-Rxjava-Rxbus,代碼行數:23,代碼來源:DateUtils.java


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