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


Java Calendar.add方法代碼示例

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


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

示例1: testGetEventsCompleted

import java.util.Calendar; //導入方法依賴的package包/類
public void testGetEventsCompleted() {
    Habit habit = new Habit("TestTitle","TestReason",new Date());
    HashSet<Integer> schedule = new HashSet<>();
    schedule.add(Calendar.MONDAY);
    schedule.add(Calendar.WEDNESDAY);
    schedule.add(Calendar.FRIDAY);
    habit.setSchedule(schedule);
    Calendar c = Calendar.getInstance();
    while (c.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY){
        c.add(Calendar.DAY_OF_YEAR,1);
    }
    c.add(Calendar.DAY_OF_YEAR,7);
    HabitEvent eventOnMonday = new HabitEvent(c.getTime());
    c.add(Calendar.DAY_OF_YEAR,1);
    HabitEvent eventOnTuesday = new HabitEvent(c.getTime());
    c.add(Calendar.DAY_OF_YEAR,-5);
    HabitEvent eventBeforeStartDate = new HabitEvent(c.getTime());
    habit.addHabitEvent(eventOnMonday);
    habit.addHabitEvent(eventOnTuesday);
    habit.addHabitEvent(eventBeforeStartDate);
    assertEquals(1,habit.getEventsCompleted());
}
 
開發者ID:CMPUT301F17T09,項目名稱:GoalsAndHabits,代碼行數:23,代碼來源:HabitTest.java

示例2: getWeekStr

import java.util.Calendar; //導入方法依賴的package包/類
public static String getWeekStr(int numDay) {
	String weekStr = "";
	Date date = new Date();
	Calendar calendar = new GregorianCalendar();
	calendar.setTime(date);
	calendar.add(Calendar.DATE, numDay);//把日期往後增加一天.整數往後推,負數往前移動
	int mWay = calendar.get(Calendar.DAY_OF_WEEK);
	if (1 == mWay) {
		weekStr = "周日";
	} else if (2 == mWay) {
		weekStr = "周一";
	} else if (3 == mWay) {
		weekStr = "周二";
	} else if (4 == mWay) {
		weekStr = "周三";
	} else if (5 == mWay) {
		weekStr = "周四";
	} else if (6 == mWay) {
		weekStr = "周五";
	} else if (7 == mWay) {
		weekStr = "周六";
	}
	return weekStr;

}
 
開發者ID:ebridfighter,項目名稱:GongXianSheng,代碼行數:26,代碼來源:TimeUtils.java

示例3: generatePersonList

import java.util.Calendar; //導入方法依賴的package包/類
public static List<Person> generatePersonList (int size) {
	List<Person> ret = new ArrayList<>();
	
	String firstNames[] = {"Mary","Patricia","Linda","Barbara","Elizabeth","James","John","Robert","Michael","William"};
	String lastNames[] = {"Smith","Jones","Taylor","Williams","Brown","Davies","Evans","Wilson","Thomas","Roberts"};
	
	Random randomGenerator=new Random();
	for (int i=0; i<size; i++) {
		Person person=new Person();
		person.setId(i);
		person.setFirstName(firstNames[randomGenerator.nextInt(10)]);
		person.setLastName(lastNames[randomGenerator.nextInt(10)]);
		person.setSalary(randomGenerator.nextInt(100000));
		person.setCoeficient(randomGenerator.nextDouble()*10);
		Calendar calendar=Calendar.getInstance();
		calendar.add(Calendar.YEAR, -randomGenerator.nextInt(30));
		Date birthDate=calendar.getTime();
		person.setBirthDate(birthDate);
		
		ret.add(person);
	}
	
	return ret;
}
 
開發者ID:PacktPublishing,項目名稱:Java-9-Concurrency-Cookbook-Second-Edition,代碼行數:25,代碼來源:PersonGenerator.java

示例4: changeRequest

import java.util.Calendar; //導入方法依賴的package包/類
protected void changeRequest ( final int secondsOffset, final int secondsScale )
{
    final Calendar start = Calendar.getInstance ();
    start.setTimeInMillis ( this.requestParameters.getStartTimestamp () );

    final Calendar end = Calendar.getInstance ();
    end.setTimeInMillis ( this.requestParameters.getEndTimestamp () );

    start.add ( Calendar.SECOND, secondsOffset );
    end.add ( Calendar.SECOND, secondsOffset );

    long diff = end.getTimeInMillis () - start.getTimeInMillis ();
    diff = diff / 1000;
    diff += secondsScale;
    if ( diff < 0 )
    {
        diff = 0;
    }
    diff *= 1000;
    end.setTimeInMillis ( start.getTimeInMillis () + diff );

    this.requestParameters = new QueryParameters ( start.getTimeInMillis (), end.getTimeInMillis (), this.requestParameters.getNumberOfEntries () );
    updateRequestParameters ();
}
 
開發者ID:eclipse,項目名稱:neoscada,代碼行數:25,代碼來源:QueryControlView.java

示例5: addSelfSignedCertificate

import java.util.Calendar; //導入方法依賴的package包/類
public void addSelfSignedCertificate(String certificateAlias, String dn, String password) {
	try {
		KeyPair keys = generateKeyPair();

		Calendar start = Calendar.getInstance();
		Calendar expiry = Calendar.getInstance();
		expiry.add(Calendar.YEAR, 1);
		X500Name name = new X500Name(dn);
		X509v3CertificateBuilder certificateBuilder = new X509v3CertificateBuilder(name, BigInteger.ONE,
				start.getTime(), expiry.getTime(), name, SubjectPublicKeyInfo.getInstance(keys.getPublic().getEncoded()));
		ContentSigner signer = new JcaContentSignerBuilder("SHA1WithRSA").setProvider(new BouncyCastleProvider()).build(keys.getPrivate());
		X509CertificateHolder holder = certificateBuilder.build(signer);
		Certificate cert = new JcaX509CertificateConverter().setProvider(new BouncyCastleProvider()).getCertificate(holder);

		Entry entry = new PrivateKeyEntry(keys.getPrivate(), new Certificate[]{ cert });
		keystore.setEntry(certificateAlias, entry, new PasswordProtection(password.toCharArray()));
	} catch (GeneralSecurityException | OperatorCreationException ex) {
		throw new RuntimeException("Unable to generate self-signed certificate", ex);
	}
}
 
開發者ID:xtf-cz,項目名稱:xtf,代碼行數:21,代碼來源:XTFKeyStore.java

示例6: timestampMs

import java.util.Calendar; //導入方法依賴的package包/類
private Date timestampMs ( final Calendar base, final int ms )
{
    final Calendar c = (Calendar)base.clone ();
    c.add ( Calendar.MILLISECOND, ms );
    return c.getTime ();
}
 
開發者ID:eclipse,項目名稱:neoscada,代碼行數:7,代碼來源:QueryBufferTest.java

示例7: doInBackground

import java.util.Calendar; //導入方法依賴的package包/類
@Override
protected Void doInBackground(Void... voids) {
    Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
    SharedPreferences mPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    int autodeleteDays = mPreferences.getInt("autodelete", AnotherRSS.Config.DEFAULT_autodelete);
    if (autodeleteDays < 1) return null;

    Date date = new Date();
    c.setTime(date);
    c.add(Calendar.DAY_OF_MONTH, -1 * autodeleteDays);
    date = c.getTime();
    String dateStr = FeedContract.dbFriendlyDate(date);

    String where = FeedContract.Feeds.COLUMN_Date + "<? and "
            + FeedContract.Feeds.COLUMN_Flag + "<> ?";

    ContentValues values = new ContentValues();
    values.put(FeedContract.Feeds.COLUMN_Deleted, FeedContract.Flag.DELETED);

    getContentResolver().update(
            FeedContentProvider.CONTENT_URI,
            values,
            where,
            new String[]{dateStr, Integer.toString(FeedContract.Flag.FAVORITE)}
    );

    return null;
}
 
開發者ID:no-go,項目名稱:AnotherRSS,代碼行數:29,代碼來源:MainActivity.java

示例8: getToken

import java.util.Calendar; //導入方法依賴的package包/類
@Override
public String getToken(final String username, final String password) {
    if (username == null || password == null) {
        return null;
    }
    final User user = (User) userDetailsService.loadUserByUsername(username);
    Map<String, Object> tokenData = new HashMap<>();
    if (password.equals(user.getPassword())) {
        tokenData.put("clientType", "user");
        tokenData.put("userID", user.getId());
        tokenData.put("username", user.getUsername());
        tokenData.put("token_create_date", LocalDateTime.now());
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MINUTE, tokenExpirationTime);
        tokenData.put("token_expiration_date", calendar.getTime());
        JwtBuilder jwtBuilder = Jwts.builder();
        jwtBuilder.setExpiration(calendar.getTime());
        jwtBuilder.setClaims(tokenData);
        return jwtBuilder.signWith(SignatureAlgorithm.HS512, tokenKey).compact();

    } else {
        throw new ServiceException("Authentication error", this.getClass().getName());
    }
}
 
開發者ID:vlsidlyarevich,項目名稱:Spring-Boot-MongoDB-JWT,代碼行數:25,代碼來源:JsonWebTokenService.java

示例9: setTimers

import java.util.Calendar; //導入方法依賴的package包/類
private void setTimers(){
	//reset the computer every day at 4:00
	Calendar nextReset = new GregorianCalendar();
	nextReset.add(Calendar.DAY_OF_MONTH, 1);
	nextReset.set(Calendar.HOUR_OF_DAY, 4);
	nextReset.set(Calendar.MINUTE, 00);
	TimerTask resetTask = new Resetter();
	resetTimer.schedule(resetTask, nextReset.getTime());
}
 
開發者ID:SensorsINI,項目名稱:jaer,代碼行數:10,代碼來源:BlurringTunnelTracker.java

示例10: addHoursToDate

import java.util.Calendar; //導入方法依賴的package包/類
/**
 * Returns date with {@code hours} added to {@code date}
 *
 * @param date       the date to which hours are to be added
 * @param hours hours to be added to given {@code date}
 * @return date after adding {@code hours} to {@code date}
 * if {@code date} is null then return null
 */
public static Date addHoursToDate(Date date, int hours) {
    if(date == null)
        return null;
    Calendar c = Calendar.getInstance();
    c.setTime(date);
    c.add(Calendar.HOUR_OF_DAY, hours);
    return c.getTime();
}
 
開發者ID:capitalone,項目名稱:Hydrograph,代碼行數:17,代碼來源:DateFunctions.java

示例11: isFirstDayofYear

import java.util.Calendar; //導入方法依賴的package包/類
/**
 * Return if the date is the first Day of the year
 * @param countryName the country name
 * @param calendar Calendar object in query
 * @return true if it is otherwise return false
 */
public static boolean isFirstDayofYear(Calendar calendar, String countryName){
    Calendar local =  (Calendar)calendar.clone();
    if(local == null)
        return false;

    if (isWeekend(local)){
        return false;
    }
    
    if (isPublicHoliday(local, countryName)){
        return false;
    }
        
    int dayOfYear = local.get(Calendar.DAY_OF_YEAR);
    if (dayOfYear == 1){
        return true;
    }else{
        while(local.get(Calendar.DAY_OF_YEAR) != 1) {
            local.add(Calendar.DAY_OF_YEAR, -1);
            if(!isPublicHoliday(local, countryName) && !isWeekend(local)){
                return false;
            }
        }
        //See if the first day of the month is public holiday or not
        if (isPublicHoliday(local,countryName) || isWeekend(local)){
            return true;
        }else{
            return false;
        }
    }
}
 
開發者ID:ztan5,項目名稱:TechnicalAnalysisTool,代碼行數:38,代碼來源:DateUtil.java

示例12: ChineseTwentyFourNextDay

import java.util.Calendar; //導入方法依賴的package包/類
public String ChineseTwentyFourNextDay(Date _date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(_date);
    Date baseDateAndTime = new Date(0, 0, 6, 2, 5, 0); //#1/6/1900 2:05:00 AM#
    Calendar basecalendar = Calendar.getInstance();
    basecalendar.setTime(baseDateAndTime);
    Date newDate;
    double num;
    int y;
    String tempStr = "";

    y = _date.getYear() + 1900;

    for (int i = 1; i <= 24; i++) {
        num = 525948.76 * (y - 1900) + Consts.sTermInfo[i - 1];

        basecalendar.setTime(baseDateAndTime);
        basecalendar.add(Calendar.MINUTE, (int) (num));//按分鍾計算
        if (basecalendar.get(Calendar.DAY_OF_YEAR) > calendar.get(Calendar.DAY_OF_YEAR)) {

            tempStr = String.format("%s[%s]", Consts.SolarTerm[i - 1], sdf.format(basecalendar.getTime()));
            break;

        }
    }
    return tempStr;
}
 
開發者ID:yippeesoft,項目名稱:NotifyTools,代碼行數:28,代碼來源:CalcLunar.java

示例13: getSecond

import java.util.Calendar; //導入方法依賴的package包/類
public static Date getSecond(int second) {

		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.add(Calendar.SECOND, second);
		Date time = calendar.getTime();

		return time;
	}
 
開發者ID:butter-fly,項目名稱:belling-spring-rabbitmq,代碼行數:10,代碼來源:TimeUtils.java

示例14: getMomentsOfSpecificAlcoholLevel

import java.util.Calendar; //導入方法依賴的package包/類
/**
 *
 * @param level alcohol level
 * @param increase if true, it gets only a moment, when the level is increasing, else only if decreasing
 * @return
 */
private List<Calendar>  getMomentsOfSpecificAlcoholLevel(double level, boolean increase) {
    List<Calendar> calendars = new ArrayList<Calendar>();
    double alcoholVolume = 0;
    boolean eventShouldHappen = true;
    for(Consumption consumption : consumptions){
        double alcoholVolumeBeforeBreakDown = alcoholVolume + getAlcoholVolumeOfConsumption(consumption);
        if(alcoholVolumeBeforeBreakDown>=level && eventShouldHappen){
            if (increase){
                calendars.add(consumption.getConsumptionTime());
                eventShouldHappen = false;
            }else{
                int durationOfBreakDownInSecOfDiff = (int)Math.round(getDurationOfBreakDown(alcoholVolumeBeforeBreakDown-level)*60*60);
                Calendar consumptionTimeWithDurationOfBreakDown = Calendar.getInstance();
                consumptionTimeWithDurationOfBreakDown.setTime(consumption.getConsumptionTime().getTime());
                consumptionTimeWithDurationOfBreakDown.add(Calendar.SECOND,durationOfBreakDownInSecOfDiff);
                boolean hasNext = (consumptions.indexOf(consumption)<consumptions.size()-1);
                if(hasNext){
                    Consumption next = consumptions.get(consumptions.indexOf(consumption));
                    if (consumptionTimeWithDurationOfBreakDown.before(next.getConsumptionTime())){
                        calendars.add(consumptionTimeWithDurationOfBreakDown);
                        eventShouldHappen = false;
                    }
                }else{
                    calendars.add(consumptionTimeWithDurationOfBreakDown);
                    eventShouldHappen = false;
                }
            }
        }
        double durationBetweenStartAndEndInSec = getDurationBetweenOneAndNextConsumption(consumption);
        double alcoholBreakDown = getAlcoholBreakDownOfDuration(durationBetweenStartAndEndInSec);
        double alcoholVolumeMinusBreakDown = alcoholVolumeBeforeBreakDown-alcoholBreakDown;
        if (alcoholVolumeMinusBreakDown<0){
            alcoholVolumeMinusBreakDown=0;
        }
        if (alcoholVolumeBeforeBreakDown>level && level > alcoholVolumeMinusBreakDown){
            eventShouldHappen = true;
        }
        alcoholVolume=alcoholVolumeMinusBreakDown;
    }
    return calendars;
}
 
開發者ID:Bartout-Team,項目名稱:Bartout,代碼行數:48,代碼來源:UserStatus.java

示例15: getNewDate

import java.util.Calendar; //導入方法依賴的package包/類
public static String getNewDate(int days){
	Calendar cal = Calendar.getInstance();
       cal.setTime(new Date());
       cal.add(Calendar.DATE, days); 
       return (new SimpleDateFormat("yyyy-MM-dd")).format(cal.getTime());
}
 
開發者ID:AlanYangs,項目名稱:Log4Reports,代碼行數:7,代碼來源:DateUtil.java


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