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


Java DateTimeBuilder類代碼示例

本文整理匯總了Java中java.time.calendrical.DateTimeBuilder的典型用法代碼示例。如果您正苦於以下問題:Java DateTimeBuilder類的具體用法?Java DateTimeBuilder怎麽用?Java DateTimeBuilder使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


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

示例1: from

import java.time.calendrical.DateTimeBuilder; //導入依賴的package包/類
/**
 * Obtains an instance of {@code LocalTime} from a date-time object.
 * <p>
 * A {@code DateTimeAccessor} represents some form of date and time information. This factory converts the
 * arbitrary date-time object to an instance of {@code LocalTime}.
 * <p>
 * The conversion extracts the {@link ChronoField#NANO_OF_DAY nano-of-day} field.
 * 
 * @param dateTime the date-time object to convert, not null
 * @return the local time, not null
 * @throws DateTimeException if unable to convert to a {@code LocalTime}
 */
public static LocalTime from(DateTimeAccessor dateTime) {

  if (dateTime instanceof LocalTime) {
    return (LocalTime) dateTime;
  } else if (dateTime instanceof ChronoLocalDateTime) {
    return ((ChronoLocalDateTime<?>) dateTime).getTime();
  } else if (dateTime instanceof ZonedDateTime) {
    return ((ChronoZonedDateTime<?>) dateTime).getTime();
  }
  // handle builder as a special case
  if (dateTime instanceof DateTimeBuilder) {
    DateTimeBuilder builder = (DateTimeBuilder) dateTime;
    LocalTime time = builder.extract(LocalTime.class);
    if (time != null) {
      return time;
    }
  }
  return ofNanoOfDay(dateTime.getLong(NANO_OF_DAY));
}
 
開發者ID:kiegroup,項目名稱:optashift-employee-rostering,代碼行數:32,代碼來源:LocalTime.java

示例2: from

import java.time.calendrical.DateTimeBuilder; //導入依賴的package包/類
/**
 * Obtains an instance of {@code LocalDate} from a date-time object.
 * <p>
 * A {@code DateTimeAccessor} represents some form of date and time information. This factory converts the
 * arbitrary date-time object to an instance of {@code LocalDate}.
 * <p>
 * The conversion extracts the {@link ChronoField#EPOCH_DAY epoch-day} field.
 * 
 * @param dateTime the date-time object to convert, not null
 * @return the local date, not null
 * @throws DateTimeException if unable to convert to a {@code LocalDate}
 */
public static LocalDate from(DateTimeAccessor dateTime) {

  if (dateTime instanceof LocalDate) {
    return (LocalDate) dateTime;
  } else if (dateTime instanceof LocalDateTime) {
    return ((LocalDateTime) dateTime).getDate();
  } else if (dateTime instanceof ZonedDateTime) {
    return ((ZonedDateTime) dateTime).getDate();
  }
  // handle builder as a special case
  if (dateTime instanceof DateTimeBuilder) {
    DateTimeBuilder builder = (DateTimeBuilder) dateTime;
    LocalDate date = builder.extract(LocalDate.class);
    if (date != null) {
      return date;
    }
  }
  return ofEpochDay(dateTime.getLong(EPOCH_DAY));
}
 
開發者ID:kiegroup,項目名稱:optashift-employee-rostering,代碼行數:32,代碼來源:LocalDate.java

示例3: parseToBuilder

import java.time.calendrical.DateTimeBuilder; //導入依賴的package包/類
/**
 * Parses the text to a builder.
 * <p>
 * This parses to a {@code DateTimeBuilder} ensuring that the text is fully parsed. This method throws
 * {@link DateTimeParseException} if unable to parse, or some other {@code DateTimeException} if another
 * date/time problem occurs.
 * 
 * @param text the text to parse, not null
 * @return the engine representing the result of the parse, not null
 * @throws DateTimeParseException if the parse fails
 * @throws DateTimeException if there is a date/time problem
 */
public DateTimeBuilder parseToBuilder(CharSequence text) {

  Jdk7Methods.Objects_requireNonNull(text, "text");
  String str = text.toString(); // parsing whole String, so this makes sense
  ParsePosition pos = new ParsePosition(0);
  DateTimeBuilder result = parseToBuilder(str, pos);
  if (result == null || pos.getErrorIndex() >= 0 || pos.getIndex() < str.length()) {
    String abbr = str.toString();
    if (abbr.length() > 64) {
      abbr = abbr.substring(0, 64) + "...";
    }
    if (pos.getErrorIndex() >= 0) {
      throw new DateTimeParseException("Text '" + abbr + "' could not be parsed at index " + pos.getErrorIndex(),
          str, pos.getErrorIndex());
    } else {
      throw new DateTimeParseException("Text '" + abbr + "' could not be parsed, unparsed text found at index "
          + pos.getIndex(), str, pos.getIndex());
    }
  }
  return result;
}
 
開發者ID:m-m-m,項目名稱:java8-backports,代碼行數:34,代碼來源:DateTimeFormatter.java

示例4: parseSecs

import java.time.calendrical.DateTimeBuilder; //導入依賴的package包/類
private int parseSecs(String str) {

    if (str.equals("-")) {
      return 0;
    }
    int pos = 0;
    if (str.startsWith("-")) {
      pos = 1;
    }
    ParsePosition pp = new ParsePosition(pos);
    DateTimeBuilder bld = TIME_PARSER.parseToBuilder(str, pp);
    if (bld == null || pp.getErrorIndex() >= 0) {
      throw new IllegalArgumentException(str);
    }
    Map<DateTimeField, Long> parsed = bld.getFieldValueMap();
    long hour = parsed.get(HOUR_OF_DAY);
    Long min = parsed.get(MINUTE_OF_HOUR);
    Long sec = parsed.get(SECOND_OF_MINUTE);
    int secs = (int) (hour * 60 * 60 + (min != null ? min : 0) * 60 + (sec != null ? sec : 0));
    if (pos == 1) {
      secs = -secs;
    }
    return secs;
  }
 
開發者ID:m-m-m,項目名稱:java8-backports,代碼行數:25,代碼來源:TzdbZoneRulesCompiler.java

示例5: createTime

import java.time.calendrical.DateTimeBuilder; //導入依賴的package包/類
private DateTimeBuilder createTime(Integer hour, Integer min, Integer sec, Integer nano) {

    DateTimeBuilder test = new DateTimeBuilder();
    if (hour != null) {
      test.addFieldValue(HOUR_OF_DAY, hour);
    }
    if (min != null) {
      test.addFieldValue(MINUTE_OF_HOUR, min);
    }
    if (sec != null) {
      test.addFieldValue(SECOND_OF_MINUTE, sec);
    }
    if (nano != null) {
      test.addFieldValue(NANO_OF_SECOND, nano);
    }
    return test;
  }
 
開發者ID:m-m-m,項目名稱:java8-backports,代碼行數:18,代碼來源:TCKDateTimeFormatters.java

示例6: test_derive

import java.time.calendrical.DateTimeBuilder; //導入依賴的package包/類
@Test(dataProvider = "combine")
public void test_derive(DateTimeField field1, Number value1, DateTimeField field2, Number value2,
    DateTimeField field3, Number value3, DateTimeField field4, Number value4, Class<?> query, Object expectedVal) {

  DateTimeBuilder builder = new DateTimeBuilder(field1, value1.longValue());
  if (field2 != null) {
    builder.addFieldValue(field2, value2.longValue());
  }
  if (field3 != null) {
    builder.addFieldValue(field3, value3.longValue());
  }
  if (field4 != null) {
    builder.addFieldValue(field4, value4.longValue());
  }
  builder.resolve();
  assertEquals(builder.extract((Class<?>) query), expectedVal);
}
 
開發者ID:m-m-m,項目名稱:java8-backports,代碼行數:18,代碼來源:TestDateTimeBuilderCombinations.java

示例7: test_normalized

import java.time.calendrical.DateTimeBuilder; //導入依賴的package包/類
@Test(dataProvider = "normalized")
public void test_normalized(DateTimeField field1, Number value1, DateTimeField field2, Number value2,
    DateTimeField field3, Number value3, DateTimeField query, Number expectedVal) {

  DateTimeBuilder builder = new DateTimeBuilder(field1, value1.longValue());
  if (field2 != null) {
    builder.addFieldValue(field2, value2.longValue());
  }
  if (field3 != null) {
    builder.addFieldValue(field3, value3.longValue());
  }
  builder.resolve();
  if (expectedVal != null) {
    assertEquals(builder.getLong(query), expectedVal.longValue());
  } else {
    assertEquals(builder.containsFieldValue(query), false);
  }
}
 
開發者ID:m-m-m,項目名稱:java8-backports,代碼行數:19,代碼來源:TestDateTimeBuilderCombinations.java

示例8: test_split

import java.time.calendrical.DateTimeBuilder; //導入依賴的package包/類
public void test_split() {

    DateTimeBuilder builder = new DateTimeBuilder();
    builder.addCalendrical(LocalDateTime.of(2012, 6, 30, 12, 30));
    builder.addCalendrical(ZoneOffset.ofHours(2));
    builder.resolve();
    assertEquals(builder.build(LocalDate.class), LocalDate.of(2012, 6, 30));
    assertEquals(builder.build(LocalTime.class), LocalTime.of(12, 30));
    assertEquals(builder.build(ZoneOffset.class), ZoneOffset.ofHours(2));

    assertEquals(builder.build(LocalDateTime.class), LocalDateTime.of(2012, 6, 30, 12, 30));
    assertEquals(builder.build(OffsetDate.class), OffsetDate.of(2012, 6, 30, ZoneOffset.ofHours(2)));
    assertEquals(builder.build(OffsetTime.class), OffsetTime.of(12, 30, ZoneOffset.ofHours(2)));
    // assertEquals(builder.build(OffsetDateTime.class), OffsetDateTime.of(2012, 6, 30, 12, 30,
    // ZoneOffset.ofHours(2)));
  }
 
開發者ID:m-m-m,項目名稱:java8-backports,代碼行數:17,代碼來源:TestDateTimeBuilderCombinations.java

示例9: toBuilder

import java.time.calendrical.DateTimeBuilder; //導入依賴的package包/類
/**
 * Returns a {@code DateTimeBuilder} that can be used to interpret the results of the parse.
 * <p>
 * This method is typically used once parsing is complete to obtain the parsed data. Parsing will typically
 * result in separate fields, such as year, month and day. The returned builder can be used to combine the
 * parsed data into meaningful objects such as {@code LocalDate}, potentially applying complex processing to
 * handle invalid parsed data.
 * 
 * @return a new builder with the results of the parse, not null
 */
public DateTimeBuilder toBuilder() {

  List<Object> cals = currentParsed().parsed;
  DateTimeBuilder builder = new DateTimeBuilder();
  for (Object obj : cals) {
    if (obj instanceof FieldValue) {
      FieldValue fv = (FieldValue) obj;
      builder.addFieldValue(fv.field, fv.value);
    } else {
      builder.addCalendrical(obj);
    }
  }
  return builder;
}
 
開發者ID:m-m-m,項目名稱:java8-backports,代碼行數:25,代碼來源:DateTimeParseContext.java

示例10: test_toBuilder

import java.time.calendrical.DateTimeBuilder; //導入依賴的package包/類
@Test(groups = { "tck" })
public void test_toBuilder() throws Exception {

  this.context.setParsedField(YEAR, 2008);
  this.context.setParsedField(MONTH_OF_YEAR, 6);

  DateTimeBuilder builder = this.context.toBuilder();
  Map<DateTimeField, Long> fields = builder.getFieldValueMap();
  assertEquals(fields.size(), 2);
  assertEquals(fields.get(YEAR), Long.valueOf(2008));
  assertEquals(fields.get(MONTH_OF_YEAR), Long.valueOf(6));
}
 
開發者ID:m-m-m,項目名稱:java8-backports,代碼行數:13,代碼來源:TCKDateTimeParseContext.java

示例11: test_print

import java.time.calendrical.DateTimeBuilder; //導入依賴的package包/類
@Test(dataProvider = "offsets")
public void test_print(String pattern, String expected, ZoneOffset offset) throws Exception {

  this.buf.append("EXISTING");
  this.printContext.setDateTime(new DateTimeBuilder(OFFSET_SECONDS, offset.getTotalSeconds()));
  ZoneOffsetPrinterParser pp = new ZoneOffsetPrinterParser("NO-OFFSET", pattern);
  pp.print(this.printContext, this.buf);
  assertEquals(this.buf.toString(), "EXISTING" + expected);
}
 
開發者ID:m-m-m,項目名稱:java8-backports,代碼行數:10,代碼來源:TestZoneOffsetPrinter.java

示例12: test_print_emptyAppendable

import java.time.calendrical.DateTimeBuilder; //導入依賴的package包/類
public void test_print_emptyAppendable() throws Exception {

    this.printContext.setDateTime(new DateTimeBuilder(OFFSET_SECONDS, OFFSET_0130.getTotalSeconds()));
    ZoneOffsetPrinterParser pp = new ZoneOffsetPrinterParser("Z", "+HH:MM:ss");
    pp.print(this.printContext, this.buf);
    assertEquals(this.buf.toString(), "+01:30");
  }
 
開發者ID:m-m-m,項目名稱:java8-backports,代碼行數:8,代碼來源:TestZoneOffsetPrinter.java

示例13: resolve1

import java.time.calendrical.DateTimeBuilder; //導入依賴的package包/類
private static void resolve1() {

    DateTimeBuilder builder = new DateTimeBuilder();
    builder.addFieldValue(ChronoField.YEAR, 2012);
    builder.addFieldValue(ChronoField.MONTH_OF_YEAR, 4);
    builder.addFieldValue(ChronoField.DAY_OF_MONTH, 18);
    System.out.println("Setup: " + builder);
    System.out.println("Resolved: " + builder.resolve());
    System.out.println("Date: " + LocalDate.from(builder));
  }
 
開發者ID:m-m-m,項目名稱:java8-backports,代碼行數:11,代碼來源:UsabilityBasic.java

示例14: resolve2

import java.time.calendrical.DateTimeBuilder; //導入依賴的package包/類
private static void resolve2() {

    DateTimeBuilder builder = new DateTimeBuilder();
    builder.addFieldValue(ChronoField.YEAR, 2012);
    builder.addFieldValue(ChronoField.MONTH_OF_YEAR, 4);
    builder.addFieldValue(ChronoField.DAY_OF_MONTH, 18);
    builder.addFieldValue(ChronoField.DAY_OF_WEEK, 1);
    System.out.println("Setup: " + builder);
    try {
      builder.resolve();
    } catch (RuntimeException ex) {
      System.err.println(ex.toString());
    }
  }
 
開發者ID:m-m-m,項目名稱:java8-backports,代碼行數:15,代碼來源:UsabilityBasic.java

示例15: resolve3

import java.time.calendrical.DateTimeBuilder; //導入依賴的package包/類
private static void resolve3() {

    DateTimeBuilder builder = new DateTimeBuilder();
    builder.addCalendrical(LocalDate.of(2012, 1, 2));
    builder.addCalendrical(ZonedDateTime.of(LocalDateTime.of(2012, 4, 3, 12, 30), ZoneOffset.ofHours(2)));
    System.out.println("Setup: " + builder);
    try {
      builder.resolve();
    } catch (RuntimeException ex) {
      System.err.println(ex.toString());
    }
  }
 
開發者ID:m-m-m,項目名稱:java8-backports,代碼行數:13,代碼來源:UsabilityBasic.java


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