热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

java.time.format.DateTimeFormatter.withZone()方法的使用及代码示例

本文整理了Java中java.time.format.DateTimeFormatter.withZone()方法的一些代码示例,展示了DateTimeFo

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

private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
this.dateTimeFormatter = DateTimeFormatter.ofPattern(formatString).withZone(zoneId);
}

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

public String getDate() {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd").withZone(ZoneId.of("UTC"));
return formatter.format(timestamp);
}
}

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

public TimestampFormatter(@Nullable String timestampFormat, ZoneId zoneId) {
if (timestampFormat != null) {
dateTimeFormatter = Optional.ofNullable(FORMATTERS.get(timestampFormat))
.orElseGet(() -> DateTimeFormatter.ofPattern(timestampFormat))
.withZone(zoneId);
} else {
dateTimeFormatter = null;
}
}

代码示例来源:origin: blynkkk/blynk-server

public DateTimeFormatter makeFormatter() {
return (format == null || format == Format.TS)
? null
: DateTimeFormatter.ofPattern(format.pattern).withZone(tzName);
}

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

@Override
public String getBucketId(IN element, BucketAssigner.Context context) {
if (dateTimeFormatter == null) {
dateTimeFormatter = DateTimeFormatter.ofPattern(formatString).withZone(zoneId);
}
return dateTimeFormatter.format(Instant.ofEpochMilli(context.currentProcessingTime()));
}

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

public static String formatDate(Date date, boolean zoned) {
if (zoned) {
return DateTimeFormatter.ISO_OFFSET_DATE_TIME.withZone(ZoneId.systemDefault()).format(date.toInstant());
} else {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
}
}

代码示例来源:origin: Netflix/Priam

/**
* Format the instant based on the pattern passed. If instant or pattern is null, null is
* returned.
*
* @param pattern Pattern that should
* @param instant Instant in time
* @return The formatted instant based on the pattern. Null, if pattern or instant is null.
*/
public static String formatInstant(String pattern, Instant instant) {
if (instant == null || StringUtils.isEmpty(pattern)) return null;
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern).withZone(utcZoneId);
return formatter.format(instant);
}

代码示例来源:origin: OpenHFT/Chronicle-Queue

private RollingResourcesCache(final int length,
@NotNull String format, long epoch,
@NotNull Function nameToFile,
@NotNull Function fileToName) {
this.length = length;
this.fileTOname= fileToName;
this.values = new Resource[CACHE_SIZE];
final long millisInDay = epoch % ONE_DAY_IN_MILLIS;
this.epoch = millisInDay >= 0 ? epoch - millisInDay : -ONE_DAY_IN_MILLIS;
this.format = format;
this.formatter = DateTimeFormatter.ofPattern(this.format).withZone(ZoneId.of("UTC"));
this.fileFactory = nameToFile;
}

代码示例来源:origin: jooby-project/jooby

/**
* Keep the default formatter but use the provided timezone.
*
* @param zoneId Zone id.
* @return This instance.
*/
public RequestLogger dateFormatter(final ZoneId zoneId) {
return dateFormatter(FORMATTER.withZone(zoneId));
}

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

/**
* Creates a new {@code DateTimeBucketer} with the given date/time format string using the given timezone.
*
* @param formatString The format string that will be given to {@code DateTimeFormatter} to determine
* the bucket path.
* @param zoneId The timezone used to format {@code DateTimeFormatter} for bucket path.
*/
public DateTimeBucketer(String formatString, ZoneId zoneId) {
this.formatString = Preconditions.checkNotNull(formatString);
this.zOneId= Preconditions.checkNotNull(zoneId);
this.dateTimeFormatter = DateTimeFormatter.ofPattern(this.formatString).withZone(zoneId);
}

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

SizeRotatingFileAuditEndpoint(Builder builder) throws IOException {
super(builder);
this.rotateSize = builder.rotateSize;
this.maxBackupIndex = builder.maxBackupIndex;
this.rotateOnBoot= builder.rotateOnBoot;
this.suffix = builder.suffix;
this.dateTimeFormatter = this.suffix != null ? DateTimeFormatter.ofPattern(this.suffix).withZone(builder.timeZone) : null;
final File file = getFile();
if (rotateOnBoot && maxBackupIndex > 0 && file != null && file.exists() && file.length() > 0L) {
rotate(file);
}
}

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

/**
* Get the DateTimeFormatter with the this context's settings
* applied to the base {@code formatter}.
* @param formatter the base formatter that establishes default
* formatting rules, generally context-independent
* @return the contextual DateTimeFormatter
*/
public DateTimeFormatter getFormatter(DateTimeFormatter formatter) {
if (this.chronology != null) {
formatter = formatter.withChronology(this.chronology);
}
if (this.timeZone != null) {
formatter = formatter.withZone(this.timeZone);
}
else {
LocaleContext localeCOntext= LocaleContextHolder.getLocaleContext();
if (localeContext instanceof TimeZoneAwareLocaleContext) {
TimeZone timeZOne= ((TimeZoneAwareLocaleContext) localeContext).getTimeZone();
if (timeZone != null) {
formatter = formatter.withZone(timeZone.toZoneId());
}
}
}
return formatter;
}

代码示例来源:origin: nutzam/nutz

@Override
public void toJson(Mirror mirror, Object currentObj, JsonRender r, JsonFormat jf) throws IOException {
String df = jf.getDateFormatRaw();
if (df == null)
df = "yyyy-MM-dd HH:mm:ss.SSS";
Locale locale = null;
String tmp = jf.getLocale();
if (tmp != null)
locale = Locale.forLanguageTag(tmp);
else
locale = Locale.getDefault();
r.string2Json(DateTimeFormatter.ofPattern(df, locale).withZone(ZoneId.systemDefault()).format((TemporalAccessor) currentObj));
}

代码示例来源:origin: oracle/helidon

private static DateTimeFormatter buildDateTimeFormatter(String stringValue) {
/*
A Java 8 bug causes DateTimeFormatter.withZone to override an explicit
time zone in the parsed string, contrary to the documented behavior. So
if the string includes a zone do NOT use withZone in building the formatter.
*/
DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE_TIME;
ParsePosition pp = new ParsePosition(0);
TemporalAccessor accessor = formatter.parseUnresolved(stringValue, pp);
if (!accessor.isSupported(ChronoField.OFFSET_SECONDS)) {
formatter = formatter.withZone(ZoneId.of("UTC"));
}
return formatter;
}

代码示例来源:origin: org.springframework/spring-context

/**
* Get the DateTimeFormatter with the this context's settings
* applied to the base {@code formatter}.
* @param formatter the base formatter that establishes default
* formatting rules, generally context-independent
* @return the contextual DateTimeFormatter
*/
public DateTimeFormatter getFormatter(DateTimeFormatter formatter) {
if (this.chronology != null) {
formatter = formatter.withChronology(this.chronology);
}
if (this.timeZone != null) {
formatter = formatter.withZone(this.timeZone);
}
else {
LocaleContext localeCOntext= LocaleContextHolder.getLocaleContext();
if (localeContext instanceof TimeZoneAwareLocaleContext) {
TimeZone timeZOne= ((TimeZoneAwareLocaleContext) localeContext).getTimeZone();
if (timeZone != null) {
formatter = formatter.withZone(timeZone.toZoneId());
}
}
}
return formatter;
}

代码示例来源:origin: Alluxio/alluxio

/**
* Generates a backup file name used a time that includes some randomness.
*
* @return a backup file name
*/
private String generateBackupFileName() {
Instant time = Instant.now().minusMillis(mRandom.nextInt());
return String.format(BackupManager.BACKUP_FILE_FORMAT,
DateTimeFormatter.ISO_LOCAL_DATE.withZone(ZoneId.of("UTC")).format(time),
time.toEpochMilli());
}

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

dateTimeFormatter = dateTimeFormatter.withZone(this.timeZone.toZoneId());

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

protected List schemaAndValuesForTstzRangeTypes() {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSSx");
Instant begin = dateTimeFormatter.parse("2017-06-05 11:29:12.549426+00", Instant::from);
Instant end = dateTimeFormatter.parse("2017-06-05 12:34:56.789012+00", Instant::from);
// Acknowledge timezone expectation of the system running the test
String beginSystemTime = dateTimeFormatter.withZone(ZoneId.systemDefault()).format(begin);
String endSystemTime = dateTimeFormatter.withZone(ZoneId.systemDefault()).format(end);
String expectedField1 = String.format("[\"%s\",)", beginSystemTime);
String expectedField2 = String.format("[\"%s\",\"%s\"]", beginSystemTime, endSystemTime);
return Arrays.asList(
new SchemaAndValueField("unbounded_exclusive_range", Schema.OPTIONAL_STRING_SCHEMA, expectedField1),
new SchemaAndValueField("bounded_inclusive_range", Schema.OPTIONAL_STRING_SCHEMA, expectedField2)
);
}

代码示例来源:origin: OpenHFT/Chronicle-Queue

@Test
public void shouldConvertCyclesToResourceNamesWithNoEpoch() throws Exception {
final int epoch = 0;
final RollingResourcesCache cache =
new RollingResourcesCache(RollCycles.DAILY, epoch, File::new, File::getName);
final int cycle = RollCycles.DAILY.current(System::currentTimeMillis, 0);
assertCorrectConversion(cache, cycle, Instant.now(),
DateTimeFormatter.ofPattern("yyyyMMdd").withZone(ZoneId.of("GMT")));
}

代码示例来源:origin: prestodb/presto

@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(format.getTimeZone().toZoneId());
}
return withDateFormat(df);
}
// any use for TimeZone?
}
return this;
}

推荐阅读
  • Python语法上的区别及注意事项
    本文介绍了Python2x和Python3x在语法上的区别,包括print语句的变化、除法运算结果的不同、raw_input函数的替代、class写法的变化等。同时还介绍了Python脚本的解释程序的指定方法,以及在不同版本的Python中如何执行脚本。对于想要学习Python的人来说,本文提供了一些注意事项和技巧。 ... [详细]
  • 标题: ... [详细]
  • 本文介绍了Python爬虫技术基础篇面向对象高级编程(中)中的多重继承概念。通过继承,子类可以扩展父类的功能。文章以动物类层次的设计为例,讨论了按照不同分类方式设计类层次的复杂性和多重继承的优势。最后给出了哺乳动物和鸟类的设计示例,以及能跑、能飞、宠物类和非宠物类的增加对类数量的影响。 ... [详细]
  • 本文介绍了在处理不规则数据时如何使用Python自动提取文本中的时间日期,包括使用dateutil.parser模块统一日期字符串格式和使用datefinder模块提取日期。同时,还介绍了一段使用正则表达式的代码,可以支持中文日期和一些特殊的时间识别,例如'2012年12月12日'、'3小时前'、'在2012/12/13哈哈'等。 ... [详细]
  • 本文介绍了在iOS开发中使用UITextField实现字符限制的方法,包括利用代理方法和使用BNTextField-Limit库的实现策略。通过这些方法,开发者可以方便地限制UITextField的字符个数和输入规则。 ... [详细]
  • 本文介绍了使用Spark实现低配版高斯朴素贝叶斯模型的原因和原理。随着数据量的增大,单机上运行高斯朴素贝叶斯模型会变得很慢,因此考虑使用Spark来加速运行。然而,Spark的MLlib并没有实现高斯朴素贝叶斯模型,因此需要自己动手实现。文章还介绍了朴素贝叶斯的原理和公式,并对具有多个特征和类别的模型进行了讨论。最后,作者总结了实现低配版高斯朴素贝叶斯模型的步骤。 ... [详细]
  • Flink使用java实现读取csv文件简单实例首先我们来看官方文档中给出的几种方法:首先我们来看官方文档中给出的几种方法:第一种:Da ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • 本文详细介绍了Spring的JdbcTemplate的使用方法,包括执行存储过程、存储函数的call()方法,执行任何SQL语句的execute()方法,单个更新和批量更新的update()和batchUpdate()方法,以及单查和列表查询的query()和queryForXXX()方法。提供了经过测试的API供使用。 ... [详细]
  • springmvc学习笔记(十):控制器业务方法中通过注解实现封装Javabean接收表单提交的数据
    本文介绍了在springmvc学习笔记系列的第十篇中,控制器的业务方法中如何通过注解实现封装Javabean来接收表单提交的数据。同时还讨论了当有多个注册表单且字段完全相同时,如何将其交给同一个控制器处理。 ... [详细]
  • Java学习笔记之面向对象编程(OOP)
    本文介绍了Java学习笔记中的面向对象编程(OOP)内容,包括OOP的三大特性(封装、继承、多态)和五大原则(单一职责原则、开放封闭原则、里式替换原则、依赖倒置原则)。通过学习OOP,可以提高代码复用性、拓展性和安全性。 ... [详细]
  • 本文整理了315道Python基础题目及答案,帮助读者检验学习成果。文章介绍了学习Python的途径、Python与其他编程语言的对比、解释型和编译型编程语言的简述、Python解释器的种类和特点、位和字节的关系、以及至少5个PEP8规范。对于想要检验自己学习成果的读者,这些题目将是一个不错的选择。请注意,答案在视频中,本文不提供答案。 ... [详细]
  • 如何设置定时器在c#中的特定时间执行我有一个要求,我需要在每天00:01:00AM执行计时器…但我没有得到如何实现这一点..如果我正在采取系统时间,它可以是不同的格式. ... [详细]
  • Spring Boot 中 Java8 LocalDateTime 序列化问题
    LoginController页面如下:publicObjectlogin(@RequestBodyUseruser){returnxxxx ... [详细]
author-avatar
天蝎快乐公主_594
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有