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

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

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

Calendar.clear介绍

[英]Clears the values of all the time fields, marking them all unset and assigning them all a value of zero. The actual field values will be determined the next time the fields are accessed.
[中]清除所有时间字段的值,将其全部标记为未设置,并将其全部赋值为零。实际字段值将在下次访问字段时确定。

代码示例

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

private static Calendar makeCalendar(int year, int month, int day) {
 Calendar date = Calendar.getInstance();
 date.clear();
 date.set(year, month - 1, day, 0, 0, 0);
 return date;
}

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

// get today and clear time of day
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 0); // ! clear would not reset the hour of day !
cal.clear(Calendar.MINUTE);
cal.clear(Calendar.SECOND);
cal.clear(Calendar.MILLISECOND);

// get start of the month
cal.set(Calendar.DAY_OF_MONTH, 1);
System.out.println("Start of the month:       " + cal.getTime());
System.out.println("... in milliseconds:      " + cal.getTimeInMillis());

// get start of the next month
cal.add(Calendar.MONTH, 1);
System.out.println("Start of the next month:  " + cal.getTime());
System.out.println("... in milliseconds:      " + cal.getTimeInMillis());

代码示例来源:origin: greenrobot/essentials

/** @param calendar helper object needed for conversion */
public static long getTimeForDay(Calendar calendar, int year, int month, int day) {
  calendar.clear();
  calendar.set(year, month - 1, day);
  return calendar.getTimeInMillis();
}

代码示例来源:origin: oblac/jodd

@Test
void testSerializeCalendar() throws Exception {
  final Calendar input = Calendar.getInstance(TimeZone.getTimeZone("Europe/Berlin"), Locale.GERMANY);
  input.clear();
  input.setTimeInMillis(1512166354000L);
  final String expected_json = "1512166354000";
  final String actual_json = new JsonSerializer().serialize(input);
  // asserts
  assertNotNull(actual_json);
  assertEquals(expected_json, actual_json);
}

代码示例来源:origin: org.apache.commons/commons-lang3

@Test
public void testParser() throws ParseException {
  final DateParser parser = new FastDateParser("YYYY-'W'ww-u", TimeZone.getDefault(), Locale.getDefault());
  final Calendar cal = Calendar.getInstance();
  cal.setMinimalDaysInFirstWeek(4);
  cal.setFirstDayOfWeek(Calendar.MONDAY);
  cal.clear();
  parser.parse(isoForm, new ParsePosition(0), cal);
  assertEquals(vulgar.getTime(), cal.getTime());
}

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

GregorianCalendar date = new GregorianCalendar();
date.clear();
//Use Gregorian calendar for all values
date.setGregorianChange(new Date(Long.MIN_VALUE));

date.setTimeZone( TimeZone.getTimeZone("UTC"));
date.setTime(new Date(-62135596800000L));

System.out.println(
    date.get(GregorianCalendar.YEAR) + "-" +
    (date.get(GregorianCalendar.MONTH) + 1) + "-" + 
    date.get(GregorianCalendar.DAY_OF_YEAR) + " " +
    date.get(GregorianCalendar.HOUR_OF_DAY) + ":" +
    date.get(GregorianCalendar.MINUTE) + ":" + 
    date.get(GregorianCalendar.SECOND) + "." +
    date.get(GregorianCalendar.MILLISECOND)
);
//Prints 1-1-1 0:0:0.0

代码示例来源:origin: lealone/Lealone

/**
 * Convert the time to the specified time zone.
 *
 * @param value the time (might be ValueNull)
 * @param calendar the calendar
 * @return the time using the correct time zone
 */
public static Time convertTime(Value value, Calendar calendar) {
  if (value == ValueNull.INSTANCE) {
    return null;
  }
  ValueTime t = (ValueTime) value.convertTo(Value.TIME);
  Calendar cal = (Calendar) calendar.clone();
  cal.clear();
  cal.setLenient(true);
  long nanos = t.getNanos();
  long millis = nanos / 1000000;
  nanos -= millis * 1000000;
  long s = millis / 1000;
  millis -= s * 1000;
  long m = s / 60;
  s -= m * 60;
  long h = m / 60;
  m -= h * 60;
  setCalendarFields(cal, 1970, 1, 1, (int) h, (int) m, (int) s, (int) millis);
  long ms = cal.getTimeInMillis();
  return new Time(ms);
}

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

// get today and clear time of day
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 0); // ! clear would not reset the hour of day !
cal.clear(Calendar.MINUTE);
cal.clear(Calendar.SECOND);
cal.clear(Calendar.MILLISECOND);

// get start of this week in milliseconds
cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
System.out.println("Start of this week:       " + cal.getTime());
System.out.println("... in milliseconds:      " + cal.getTimeInMillis());

// start of the next week
cal.add(Calendar.WEEK_OF_YEAR, 1);
System.out.println("Start of the next week:   " + cal.getTime());
System.out.println("... in milliseconds:      " + cal.getTimeInMillis());

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

/**
   *
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   *
   * @generated modifiable
   */
  public String encode(Object object, String value) {
    final Time time = (Time) object;
    Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
    calendar.clear();
    calendar.setTimeInMillis(time.getTime());
    return DatatypeConverterImpl.getInstance().printTime(calendar);
  }
}

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

private static Calendar makeCalendar(int year, int dayOfYear) {
 Calendar date = Calendar.getInstance();
 date.clear();
 date.set(Calendar.YEAR, year);
 date.set(Calendar.DAY_OF_YEAR, dayOfYear);
 return date;
}

代码示例来源:origin: greenrobot/essentials

/** @param calendar helper object needed for conversion */
public static long getTimeForDay(Calendar calendar, int year, int month, int day) {
  calendar.clear();
  calendar.set(year, month - 1, day);
  return calendar.getTimeInMillis();
}

代码示例来源:origin: lealone/Lealone

/**
 * Convert the date to the specified time zone.
 *
 * @param value the date (might be ValueNull)
 * @param calendar the calendar
 * @return the date using the correct time zone
 */
public static Date convertDate(Value value, Calendar calendar) {
  if (value == ValueNull.INSTANCE) {
    return null;
  }
  ValueDate d = (ValueDate) value.convertTo(Value.DATE);
  Calendar cal = (Calendar) calendar.clone();
  cal.clear();
  cal.setLenient(true);
  long dateValue = d.getDateValue();
  setCalendarFields(cal, yearFromDateValue(dateValue), monthFromDateValue(dateValue), dayFromDateValue(dateValue),
      0, 0, 0, 0);
  long ms = cal.getTimeInMillis();
  return new Date(ms);
}

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

static long roundDownMinute(long timeInMs) {
    Calendar date = Calendar.getInstance();
    date.setTimeInMillis(timeInMs);
    date.clear(Calendar.SECOND);
    date.clear(Calendar.MILLISECOND);
    return date.getTimeInMillis();
  }
}

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

public String encode(Object object, String value) throws Exception {
    final Date date = (Date) object;
    Object hint = Hints.getSystemDefault(Hints.LOCAL_DATE_TIME_HANDLING);
    Calendar calendar;
    if (Boolean.TRUE.equals(hint)) {
      calendar = Calendar.getInstance();
    } else {
      calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
    }
    calendar.clear();
    calendar.setTimeInMillis(date.getTime());

    return DatatypeConverterImpl.getInstance().printDate(calendar);
  }
}

代码示例来源: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: greenrobot/essentials

/** @param calendar helper object needed for conversion */
public static long getTimeFromDayReadableInt(Calendar calendar, int readableDay, int hour) {
  int day = readableDay % 100;
  int month = readableDay / 100 % 100;
  int year = readableDay / 10000;
  calendar.clear(); // We don't set all fields, so we should clear the calendar first
  calendar.set(Calendar.HOUR_OF_DAY, hour);
  calendar.set(Calendar.DAY_OF_MONTH, day);
  calendar.set(Calendar.MONTH, month - 1);
  calendar.set(Calendar.YEAR, year);
  return calendar.getTimeInMillis();
}

代码示例来源:origin: lealone/Lealone

cal.clear();
cal.setLenient(true);
long dateValue = ts.getDateValue();
setCalendarFields(cal, yearFromDateValue(dateValue), monthFromDateValue(dateValue), dayFromDateValue(dateValue),
    (int) h, (int) m, (int) s, (int) millis);
long ms = cal.getTimeInMillis();
Timestamp x = new Timestamp(ms);
x.setNanos((int) (nanos + millis * 1000000));

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

static long roundUpToNextMinute(long timeInMs) {
  Calendar date = Calendar.getInstance();
  date.setTimeInMillis(timeInMs);
  date.add(Calendar.MINUTE, 1);
  date.clear(Calendar.SECOND);
  date.clear(Calendar.MILLISECOND);
  return date.getTimeInMillis();
}

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

/**
   *
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   *
   * @generated modifiable
   */
  public String encode(Object object, String value) {
    final Date timestamp = Converters.convert(object, Date.class);
    Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
    cal.clear();
    cal.setTimeInMillis(timestamp.getTime());
    return DatatypeConverterImpl.getInstance().printDateTime(cal);
  }
}

代码示例来源:origin: org.apache.commons/commons-lang3

@Test
public void testHourFormats() {
  final Calendar calendar = Calendar.getInstance();
  calendar.clear();
  final DatePrinter printer = getInstance("K k H h");
  calendar.set(Calendar.HOUR_OF_DAY, 0);
  assertEquals("0 24 0 12", printer.format(calendar));
  calendar.set(Calendar.HOUR_OF_DAY, 12);
  assertEquals("0 12 12 12", printer.format(calendar));
  calendar.set(Calendar.HOUR_OF_DAY, 23);
  assertEquals("11 23 23 11", printer.format(calendar));
}

相关文章