java.util.Calendar.before()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(8.9k)|赞(0)|评价(0)|浏览(175)

本文整理了Java中java.util.Calendar.before()方法的一些代码示例,展示了Calendar.before()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Calendar.before()方法的具体详情如下:
包路径:java.util.Calendar
类名称:Calendar
方法名:before

Calendar.before介绍

[英]Returns whether the Date represented by this Calendar instance is before the Date represented by the parameter. The comparison is not dependent on the time zones of the Calendar.
[中]返回此日历实例表示的日期是否早于参数表示的日期。比较不依赖于日历的时区。

代码示例

代码示例来源:origin: stackoverflow.com

/**
 * This method also assumes endDate >= startDate
**/
public static long daysBetween(Date startDate, Date endDate) {
 Calendar sDate = getDatePart(startDate);
 Calendar eDate = getDatePart(endDate);

 long daysBetween = 0;
 while (sDate.before(eDate)) {
   sDate.add(Calendar.DAY_OF_MONTH, 1);
   daysBetween++;
 }
 return daysBetween;
}

代码示例来源:origin: stackoverflow.com

Calendar start = Calendar.getInstance();
start.setTime(startDate);
Calendar end = Calendar.getInstance();
end.setTime(endDate);

for (Date date = start.getTime(); start.before(end); start.add(Calendar.DATE, 1), date = start.getTime()) {
  // Do your job here with `date`.
  System.out.println(date);
}

代码示例来源:origin: stackoverflow.com

Calendar cal1 = Calendar.getInstance();
     Calendar cal2 = Calendar.getInstance();
     cal1.setTime(date1);
     cal2.setTime(date2);
     if(cal1.after(cal2)){
       System.out.println("Date1 is after Date2");
     }
     if(cal1.before(cal2)){
       System.out.println("Date1 is before Date2");
     }
     if(cal1.equals(cal2)){
       System.out.println("Date1 is equal Date2");
     }

代码示例来源:origin: apache/incubator-gobblin

private boolean isWithinWindow(int windowStartInHours, int windowEndInHours, String outputScheduledString)
   throws ParseException {
  Calendar windowStart = Calendar.getInstance();
  windowStart.set(Calendar.HOUR_OF_DAY, windowStartInHours);
  windowStart.set(Calendar.MINUTE, 0);
  windowStart.set(Calendar.SECOND, 0);

  Calendar windowEnd = Calendar.getInstance();
  windowEnd.set(Calendar.HOUR_OF_DAY, windowEndInHours);
  windowEnd.set(Calendar.MINUTE, 0);
  windowEnd.set(Calendar.SECOND, 0);

  Date outputDate = new SimpleDateFormat("hh,mm,a,z").parse(outputScheduledString);
  Calendar receivedTime = Calendar.getInstance();
  receivedTime.set(Calendar.HOUR_OF_DAY, Integer.parseInt(new SimpleDateFormat("HH").format(outputDate)));
  receivedTime.set(Calendar.MINUTE, Integer.parseInt(new SimpleDateFormat("mm").format(outputDate)));

  log.info("Window start time is: " + new SimpleDateFormat("MM/dd/yyyy hh,mm,a,z").format(windowStart.getTime()));
  log.info("Window end time is: " + new SimpleDateFormat("MM/dd/yyyy hh,mm,a,z").format(windowEnd.getTime()));
  log.info("Output time is: " + new SimpleDateFormat("MM/dd/yyyy hh,mm,a,z").format(receivedTime.getTime()));

  return receivedTime.after(windowStart) && receivedTime.before(windowEnd);
 }
}

代码示例来源:origin: Adobe-Consulting-Services/acs-aem-commons

@Override
  protected void adjustExpires(Calendar next) {
   if (next.before(Calendar.getInstance())) {
     next.add(Calendar.DAY_OF_MONTH, 1);
   }
  }
}

代码示例来源:origin: jenkinsci/jenkins

@Restricted(NoExternalUse.class) // just for form validation
public @CheckForNull Calendar previous() {
  Calendar nearest = null;
  for (CronTab tab : tabs) {
    Calendar scheduled = tab.floor(tab.getTimeZone() == null ? Calendar.getInstance() : Calendar.getInstance(tab.getTimeZone()));
    if (nearest == null || nearest.before(scheduled)) {
      nearest = scheduled;
    }
  }
  return nearest;
}

代码示例来源:origin: org.codehaus.groovy/groovy

@Deprecated
public static int minus(Calendar self, Calendar then) {
  Calendar a = self;
  Calendar b = then;
  boolean swap = a.before(b);
  if (swap) {
    Calendar t = a;
    a = b;
    b = t;
  }
  int days = 0;
  b = (Calendar) b.clone();
  while (a.get(Calendar.YEAR) > b.get(Calendar.YEAR)) {
    days += 1 + (b.getActualMaximum(Calendar.DAY_OF_YEAR) - b.get(Calendar.DAY_OF_YEAR));
    b.set(Calendar.DAY_OF_YEAR, 1);
    b.add(Calendar.YEAR, 1);
  }
  days += a.get(Calendar.DAY_OF_YEAR) - b.get(Calendar.DAY_OF_YEAR);
  if (swap) days = -days;
  return days;
}

代码示例来源:origin: drawers/SpinnerDatePicker

private void setDate(int year, int month, int dayOfMonth) {
  mCurrentDate.set(year, month, dayOfMonth);
  if (mCurrentDate.before(mMinDate)) {
    mCurrentDate.setTimeInMillis(mMinDate.getTimeInMillis());
  } else if (mCurrentDate.after(mMaxDate)) {
    mCurrentDate.setTimeInMillis(mMaxDate.getTimeInMillis());
  }
}

代码示例来源:origin: quartz-scheduler/quartz

startCal.set(Calendar.HOUR_OF_DAY, rangeStartingHourOfDay);
startCal.set(Calendar.MINUTE, rangeStartingMinute);
startCal.set(Calendar.SECOND, rangeStartingSecond);
startCal.set(Calendar.MILLISECOND, rangeStartingMillis);
endCal.set(Calendar.MILLISECOND, rangeEndingMillis);
if (!startCal.before(endCal)) {
  throw new IllegalArgumentException(invalidTimeRange +
      rangeStartingHourOfDay + ":" +

代码示例来源:origin: stackoverflow.com

Calendar lastDate = Calendar.getInstance();
lastDate.setTime(toDate);
lastDate.add(Calendar.DATE, -1);

Calendar cal = Calendar.getInstance();
cal.setTime(fromDate);
while (cal.before(lastDate)) {...}

代码示例来源:origin: stackoverflow.com

public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
  Calendar now = Calendar.getInstance();
  Calendar alarm = Calendar.getInstance();
  alarm.set(Calendar.HOUR_OF_DAY, hourOfDay);
  alarm.set(Calendar.MINUTE, minute);
  long alarmMillis = alarm.getTimeInMillis();
  if (alarm.before(now)) alarmMillis+= 86400000L;  //Add 1 day if time selected before now
  setAlarm(alarmMillis);
}

public void setAlarm(long millis) { 
  /** Set your alarm here */
}

代码示例来源:origin: ZHENFENG13/My-Blog

public static int getUserAge(Date birthday) {
  if(birthday == null) {
    return 0;
  } else {
    Calendar cal = Calendar.getInstance();
    if(cal.before(birthday)) {
      return 0;
    } else {
      int yearNow = cal.get(1);
      cal.setTime(birthday);
      int yearBirth = cal.get(1);
      return yearNow - yearBirth;
    }
  }
}

代码示例来源:origin: com.adobe.acs/acs-aem-commons-bundle

@Override
  protected void adjustExpires(Calendar next) {
    if (next.before(Calendar.getInstance())) {
      next.add(Calendar.DAY_OF_MONTH, 1);
    }
  }
}

代码示例来源:origin: glyptodon/guacamole-client

/**
 * Given a time when a particular state changes from inactive to active,
 * and a time when a particular state changes from active to inactive,
 * determines whether that state is currently active.
 *
 * @param activeStart
 *     The time at which the state changes from inactive to active.
 *
 * @param inactiveStart
 *     The time at which the state changes from active to inactive.
 *
 * @return
 *     true if the state is currently active, false otherwise.
 */
private boolean isActive(Calendar activeStart, Calendar inactiveStart) {
  // If end occurs before start, convert to equivalent case where start
  // start is before end
  if (inactiveStart != null && activeStart != null && inactiveStart.before(activeStart))
    return !isActive(inactiveStart, activeStart);
  // Get current time
  Calendar current = Calendar.getInstance();
  // State is active iff the current time is between the start and end
  return !(activeStart != null && current.before(activeStart))
    && !(inactiveStart != null && current.after(inactiveStart));
}

代码示例来源:origin: stackoverflow.com

FileWriter fileWriter = new FileWriter("myFile");
 Calendar now = new GregorianCalendar();
 Calendar c = new GregorianCalendar();
 c.set(Calendar.HOUR_OF_DAY, 0);
 c.set(Calendar.MINUTE, 0);
 DateFormat df = new SimpleDateFormat("HH:mm");
 while (c.before(now)) {
   c.add(Calendar.MINUTE, 1);
   fileWriter.write(df.format(c.getTime())+System.lineSeparator());
 }
 fileWriter.close();

代码示例来源:origin: lany192/DateTimePicker

private void setDate(int year, int month, int dayOfMonth, int hourOfDay, int minute, int second) {
  mCurrentDate.set(year, month, dayOfMonth, hourOfDay, minute, second);
  if (mCurrentDate.before(mMinDate)) {
    mCurrentDate.setTimeInMillis(mMinDate.getTimeInMillis());
  } else if (mCurrentDate.after(mMaxDate)) {
    mCurrentDate.setTimeInMillis(mMaxDate.getTimeInMillis());
  }
}

代码示例来源:origin: stackoverflow.com

/** Using Calendar - THE CORRECT WAY**/  
public static long daysBetween(Calendar startDate, Calendar endDate) {  
 //assert: startDate must be before endDate  
 Calendar date = (Calendar) startDate.clone();  
 long daysBetween = 0;  
 while (date.before(endDate)) {  
  date.add(Calendar.DAY_OF_MONTH, 1);  
  daysBetween++;  
 }  
 return daysBetween;  
}

代码示例来源:origin: stackoverflow.com

GregorianCalendar twopm = new GregorianCalendar();
twopm.set(GregorianCalendar.HOUR_OF_DAY, 14);
twopm.set(GregorianCalendar.MINUTE, 0);
twopm.set(GregorianCalendar.SECOND, 0);
twopm.set(GregorianCalendar.MILLISECOND, 0);
if(twopm.before(new GregorianCalendar())){
  twopm.add(GregorianCalendar.DAY_OF_MONTH, 1);
}
alarmManager.setRepeating(type, twopm.getTimeInMillis(), 1000*60*60*24, intent);

代码示例来源:origin: stackoverflow.com

import java.util.Calendar;
import java.util.Date;

@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  requestWindowFeature(Window.FEATURE_NO_TITLE);
  setContentView(new Panel(this));

  Date dat  = new Date();//initializes to now
  Calendar cal_alarm = Calendar.getInstance();
  Calendar cal_now = Calendar.getInstance();
  cal_now.setTime(dat);
  cal_alarm.setTime(dat);
  cal_alarm.set(Calendar.HOUR_OF_DAY,5);//set the alarm time
  cal_alarm.set(Calendar.MINUTE, 59);
  cal_alarm.set(Calendar.SECOND,0);
  if(cal_alarm.before(cal_now)){//if its in the past increment
    cal_alarm.add(Calendar.DATE,1);
  }
  //SET YOUR AlarmManager here

}

代码示例来源:origin: stackoverflow.com

public static boolean isAfterToday(int year, int month, int day)
{
  Calendar today = Calendar.getInstance();
  Calendar myDate = Calendar.getInstance();

  myDate.set(year, month, day);

  if (myDate.before(today)) 
  {
    return false;
  }
  return true;
}

相关文章