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

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

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

Calendar.set介绍

[英]Sets the given field to the given value.
[中]将给定字段设置为给定值。

代码示例

代码示例来源:origin: ctripcorp/apollo

private static Date getDayBeginTime(Date date) {
 Calendar calendar = Calendar.getInstance();
 calendar.setTime(date);
 calendar.set(Calendar.HOUR, 0);
 calendar.set(Calendar.MINUTE, 0);
 calendar.set(Calendar.SECOND, 0);
 calendar.set(Calendar.MILLISECOND, 0);
 return new Date(calendar.getTime().getTime());
}

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

Calendar cal = Calendar.getInstance(); // locale-specific
cal.setTime(dateObject);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
long time = cal.getTimeInMillis();

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

/**
 * between 2:00 am to 6:00 am, the time is random.
 *
 * @return
 */
long calculateStartTime() {
  Calendar calendar = Calendar.getInstance();
  long nowMill = calendar.getTimeInMillis();
  calendar.set(Calendar.HOUR_OF_DAY, 0);
  calendar.set(Calendar.MINUTE, 0);
  calendar.set(Calendar.SECOND, 0);
  calendar.set(Calendar.MILLISECOND, 0);
  long subtract = calendar.getTimeInMillis() + ONE_DAY_IN_MIll - nowMill;
  return subtract + (FOUR_HOURS_IN_MIll / 2) + ThreadLocalRandom.current().nextInt(FOUR_HOURS_IN_MIll);
}

代码示例来源:origin: spring-projects/spring-framework

private Date getDate() {
  Calendar cal = Calendar.getInstance();
  cal.set(Calendar.YEAR, 10);
  cal.set(Calendar.MONTH, 10);
  cal.set(Calendar.DATE, 10);
  cal.set(Calendar.HOUR, 10);
  cal.set(Calendar.MINUTE, 10);
  cal.set(Calendar.SECOND, 10);
  return cal.getTime();
}

代码示例来源:origin: apache/kylin

public static long getYearStart(long ts) {
  Calendar calendar = Calendar.getInstance(gmt, Locale.ROOT);
  calendar.setTimeInMillis(ts);
  int year = calendar.get(Calendar.YEAR);
  calendar.clear();
  calendar.set(year, 0, 1);
  return calendar.getTimeInMillis();
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testIncrementDayOfMonthAndRollover() throws Exception {
  CronTrigger trigger = new CronTrigger("* * * 10 * *", timeZone);
  calendar.set(Calendar.DAY_OF_MONTH, 11);
  Date date = calendar.getTime();
  calendar.add(Calendar.MONTH, 1);
  calendar.set(Calendar.DAY_OF_MONTH, 10);
  calendar.set(Calendar.HOUR_OF_DAY, 0);
  calendar.set(Calendar.MINUTE, 0);
  calendar.set(Calendar.SECOND, 0);
  TriggerContext context = getTriggerContext(date);
  assertEquals(calendar.getTime(), trigger.nextExecutionTime(context));
}

代码示例来源:origin: apache/rocketmq

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_OF_DAY, 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();
}

代码示例来源:origin: spring-projects/spring-framework

calendar.setTime(date);
calendar.set(Calendar.MILLISECOND, 0);
long originalTimestamp = calendar.getTimeInMillis();
doNext(calendar, calendar.get(Calendar.YEAR));
if (calendar.getTimeInMillis() == originalTimestamp) {
  calendar.add(Calendar.SECOND, 1);
  doNext(calendar, calendar.get(Calendar.YEAR));
return calendar.getTime();

代码示例来源:origin: stanfordnlp/CoreNLP

private static Calendar copyCalendar(Calendar c) {
  Calendar date = Calendar.getInstance();
  date.clear();
  date.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), c.get(Calendar.HOUR_OF_DAY), c
    .get(Calendar.MINUTE), c.get(Calendar.SECOND));
  return date;
 }
}

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

public static Calendar getDatePart(Date date){
  Calendar cal = Calendar.getInstance();       // get calendar instance
  cal.setTime(date);      
  cal.set(Calendar.HOUR_OF_DAY, 0);            // set hour to midnight
  cal.set(Calendar.MINUTE, 0);                 // set minute in hour
  cal.set(Calendar.SECOND, 0);                 // set second in minute
  cal.set(Calendar.MILLISECOND, 0);            // set millisecond in second

  return cal;                                  // return the date part
}

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

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

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testMonthlyTriggerInShortMonth() throws Exception {
  CronTrigger trigger = new CronTrigger("0 0 0 1 * *", timeZone);
  calendar.set(Calendar.MONTH, 9);
  calendar.set(Calendar.DAY_OF_MONTH, 30);
  Date date = calendar.getTime();
  calendar.set(Calendar.MONTH, 10);
  calendar.set(Calendar.DAY_OF_MONTH, 1);
  calendar.set(Calendar.HOUR_OF_DAY, 0);
  calendar.set(Calendar.MINUTE, 0);
  calendar.set(Calendar.SECOND, 0);
  TriggerContext context = getTriggerContext(date);
  assertEquals(calendar.getTime(), trigger.nextExecutionTime(context));
}

代码示例来源:origin: redisson/redisson

/**
 * Advance the calendar to the particular hour paying particular attention
 * to daylight saving problems.
 * 
 * @param cal the calendar to operate on
 * @param hour the hour to set
 */
protected void setCalendarHour(Calendar cal, int hour) {
  cal.set(java.util.Calendar.HOUR_OF_DAY, hour);
  if (cal.get(java.util.Calendar.HOUR_OF_DAY) != hour && hour != 24) {
    cal.set(java.util.Calendar.HOUR_OF_DAY, hour + 1);
  }
}

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

@Override
  void setTo(Calendar c, int i) {
    int v = i-offset;
    int was = c.get(field);
    c.set(field,v);
    final int firstDayOfWeek = c.getFirstDayOfWeek();
    if (v < firstDayOfWeek && was >= firstDayOfWeek) {
      // in crontab, the first DoW is always Sunday, but in Java, it can be Monday or in theory arbitrary other days.
      // When first DoW is 1/2 Monday, calendar points to 1/2 Monday, setting the DoW to Sunday makes
      // the calendar moves forward to 1/8 Sunday, instead of 1/1 Sunday. So we need to compensate that effect here.
      addTo(c,-7);
    } else if (was < firstDayOfWeek && firstDayOfWeek <= v) {
      // If we wrap the other way around, we need to adjust in the opposite direction of above.
      addTo(c, 7);
    }
  }
};

代码示例来源:origin: spring-projects/spring-framework

private void roundup(Calendar calendar) {
  calendar.add(Calendar.SECOND, 1);
  calendar.set(Calendar.MILLISECOND, 0);
}

代码示例来源:origin: macrozheng/mall

/**
   * 从Date类型的时间中提取时间部分
   */
  public static Date getTime(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.set(Calendar.YEAR, 1970);
    calendar.set(Calendar.MONTH, 0);
    calendar.set(Calendar.DAY_OF_MONTH, 1);
    return calendar.getTime();
  }
}

代码示例来源:origin: spring-projects/spring-framework

private Date getDate() {
  Calendar cal = Calendar.getInstance();
  cal.set(Calendar.YEAR, 10);
  cal.set(Calendar.MONTH, 10);
  cal.set(Calendar.DATE, 10);
  cal.set(Calendar.HOUR, 10);
  cal.set(Calendar.MINUTE, 10);
  cal.set(Calendar.SECOND, 10);
  return cal.getTime();
}

代码示例来源:origin: apache/kylin

public static long getQuarterStart(long ts) {
  Calendar calendar = Calendar.getInstance(gmt, Locale.ROOT);
  calendar.setTimeInMillis(ts);
  int year = calendar.get(Calendar.YEAR);
  int month = calendar.get(Calendar.MONTH);
  calendar.clear();
  calendar.set(year, month / 3 * 3, 1);
  return calendar.getTimeInMillis();
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testIncrementDayOfMonthByOne() throws Exception {
  CronTrigger trigger = new CronTrigger("* * * 10 * *", timeZone);
  calendar.set(Calendar.DAY_OF_MONTH, 9);
  Date date = calendar.getTime();
  calendar.add(Calendar.DAY_OF_MONTH, 1);
  calendar.set(Calendar.HOUR_OF_DAY, 0);
  calendar.set(Calendar.MINUTE, 0);
  calendar.set(Calendar.SECOND, 0);
  TriggerContext context = getTriggerContext(date);
  assertEquals(calendar.getTime(), trigger.nextExecutionTime(context));
}

代码示例来源:origin: apache/rocketmq

public static long computNextMorningTimeMillis() {
  Calendar cal = Calendar.getInstance();
  cal.setTimeInMillis(System.currentTimeMillis());
  cal.add(Calendar.DAY_OF_MONTH, 1);
  cal.set(Calendar.HOUR_OF_DAY, 0);
  cal.set(Calendar.MINUTE, 0);
  cal.set(Calendar.SECOND, 0);
  cal.set(Calendar.MILLISECOND, 0);
  return cal.getTimeInMillis();
}

相关文章