当前位置: 首页>>代码示例>>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;未经允许,请勿转载。