org.threeten.bp.format.DateTimeFormatter.withZone()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(3.9k)|赞(0)|评价(0)|浏览(131)

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

DateTimeFormatter.withZone介绍

[英]Returns a copy of this formatter with a new override zone.

This returns a formatter with similar state to this formatter but with the override zone set. By default, a formatter has no override zone, returning null.

If an override is added, then any instant that is printed or parsed will be affected.

When printing, if the Temporal object contains an instant then it will be converted to a zoned date-time using the override zone. If the input has a chronology then it will be retained unless overridden. If the input does not have a chronology, such as Instant, then the ISO chronology will be used. The converted result will behave in a manner equivalent to an implementation of ChronoZonedDateTime.

When parsing, the override zone will be used to interpret the ChronoField into an instant unless the formatter directly parses a valid zone.

This instance is immutable and unaffected by this method call.
[中]返回此格式化程序的副本,其中包含新的覆盖区域。
这将返回一个与此格式化程序状态相似但设置了覆盖区域的格式化程序。默认情况下,格式化程序没有覆盖区域,返回null。
如果添加了覆盖,则打印或解析的任何瞬间都将受到影响。
打印时,如果临时对象包含一个瞬间,则将使用覆盖区域将其转换为分区日期时间。如果输入有年表,则除非重写,否则将保留它。如果输入没有时间顺序,例如Instant,则将使用ISO时间顺序。转换后的结果将以相当于ChronoZonedDateTime实现的方式运行。
解析时,除非格式化程序直接解析有效区域,否则重写区域将用于将ChronoField解释为瞬间。
此实例是不可变的,不受此方法调用的影响。

代码示例

代码示例来源:origin: googleapis/google-cloud-java

project.setCreateTime(
  DateTimeFormatter.ISO_DATE_TIME
    .withZone(ZoneOffset.UTC)
    .format(Instant.ofEpochMilli(System.currentTimeMillis())));
if (projects.putIfAbsent(project.getProjectId(), project) != null) {

代码示例来源:origin: googleapis/google-cloud-java

Change toPb() {
 Change pb = new Change();
 // set id
 if (getGeneratedId() != null) {
  pb.setId(getGeneratedId());
 }
 // set timestamp
 if (getStartTimeMillis() != null) {
  pb.setStartTime(
    DateTimeFormatter.ISO_DATE_TIME
      .withZone(ZoneOffset.UTC)
      .format(Instant.ofEpochMilli(getStartTimeMillis())));
 }
 // set status
 if (status() != null) {
  pb.setStatus(status().name().toLowerCase());
 }
 // set a list of additions
 pb.setAdditions(Lists.transform(getAdditions(), RecordSet.TO_PB_FUNCTION));
 // set a list of deletions
 pb.setDeletions(Lists.transform(getDeletions(), RecordSet.TO_PB_FUNCTION));
 return pb;
}

代码示例来源:origin: googleapis/google-cloud-java

com.google.api.services.cloudresourcemanager.model.Project toPb() {
 com.google.api.services.cloudresourcemanager.model.Project projectPb =
   new com.google.api.services.cloudresourcemanager.model.Project();
 projectPb.setName(name);
 projectPb.setProjectId(projectId);
 projectPb.setLabels(labels);
 projectPb.setProjectNumber(projectNumber);
 if (state != null) {
  projectPb.setLifecycleState(state.toString());
 }
 if (createTimeMillis != null) {
  projectPb.setCreateTime(
    DateTimeFormatter.ISO_DATE_TIME
      .withZone(ZoneOffset.UTC)
      .format(Instant.ofEpochMilli(createTimeMillis)));
 }
 if (parent != null) {
  projectPb.setParent(parent.toPb());
 }
 return projectPb;
}

代码示例来源:origin: com.github.joschi.jackson/jackson-datatype-threetenbp

@Override
  public JsonDeserializer<?> createContextual(DeserializationContext ctxt,
      BeanProperty property) throws JsonMappingException
  {
    JsonFormat.Value format = findFormatOverrides(ctxt, property, handledType());
    if (format != null) {
      if (format.hasPattern()) {
        final String pattern = format.getPattern();
        final Locale locale = format.hasLocale() ? format.getLocale() : ctxt.getLocale();
        DateTimeFormatter df;
        if (locale == null) {
          df = DateTimeFormatter.ofPattern(pattern);
        } else {
          df = DateTimeFormatter.ofPattern(pattern, locale);
        }
        //Issue #69: For instant serializers/deserializers we need to configure the formatter with
        //a time zone picked up from JsonFormat annotation, otherwise serialization might not work
        if (format.hasTimeZone()) {
          df = df.withZone(DateTimeUtils.toZoneId(format.getTimeZone()));
        }
        return withDateFormat(df);
      }
      // any use for TimeZone?
    }
    return this;
  }
}

代码示例来源:origin: com.github.joschi.jackson/jackson-datatype-threetenbp

dtf = dtf.withZone(DateTimeUtils.toZoneId(format.getTimeZone()));

相关文章