- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 Vaadin Flow 14.1 版中,我发现只有两个日期时间类型的渲染器实现:
LocalDateRenderer
LocalDateTimeRenderer
LocalDate
中的仅日期值。类,没有时间和时区。那很好。
LocalDateTime
表示带有时间的日期的类,但故意缺少
time zone 的上下文或
offset-from-UTC .足够好。
LocalDateTime
不太频繁,主要是在政治家可以更改时区的定义时(他们已被证明在世界各地经常这样做)来预订 future 的约会。那
LocalDateTime
类不能代表片刻。例如,以今年 1 月 23 日下午 3 点为例。如果没有时区或与 UTC 偏移的上下文,我们不知道这是否意味着日本东京的下午 3 点、法国图卢兹的下午 3 点或美国俄亥俄州托莱多的下午 3 点——三个非常不同的时刻相隔几个小时。
Instant
,
OffsetDateTime
, 或
ZonedDateTime
类。一个
Instant
是
UTC中的片刻, 根据定义,始终为 UTC。一个
OffsetDateTime
表示与 UTC 的偏移量为几小时-分钟-秒的时刻。一个
ZonedDateTime
是通过特定地区的人们使用的挂钟时间看到的时刻,一个时区。这样的时区是该地区使用的偏移量的过去、现在和 future 变化的历史。
最佳答案
我的 InstantRenderer
类(class)
您可以轻松创建自己的渲染器实现。
这是我为处理 Grid
而编写的渲染器显示包含 Instant
的对象的小部件目的。一个 Instant
是一个时刻,时间线上的一个特定点,如 UTC 所示(零小时-分钟-秒的偏移量)。 Instant
class 是 java.time 框架中使用的基本构建 block 类。
这里的想法是我们采用 Instant
对象,应用指定的 ZoneId
获取 ZonedDateTime
目的。那ZonedDateTime
对象使用指定的 DateTimeFormatter
在 String
中生成文本的对象目的。文本代表 ZonedDateTime
的内容对象 automatically localized到指定 Locale
对象的人类语言和文化规范。
ZoneId
和 Locale
附在 DateTimeFormatter
由调用程序员传递。
我的代码是基于 Vaadin Ltd 公司为他们的 LocalDateTimeRenderer
发布的代码。类(class)’ source-code found on their GitHub site .
我修剪了那个类的 API。他们的 API 允许传递格式化模式字符串而不是 DateTimeFormatter
。目的。我不认为渲染器有责任从这样的字符串生成格式化程序对象,因此也处理任何由此产生的错误条件。他们的 API 允许通过 Locale
目的。 Locale
对象可以附加到 DateTimeFormatter
调用程序员传递的对象。我看不出这个渲染器类应该如何不必要地参与将传递的语言环境分配给传递的格式化程序。调用程序可以在将格式化程序传递给我们的渲染器之前完成该分配。
这是定义 InstantRenderer
的典型用法。用于渲染 Instant
在 Grid
中显示的对象在瓦丁 14.
invoicesGrid
.addColumn(
new InstantRenderer <>( Invoice :: getWhenCreated ,
DateTimeFormatter
.ofLocalizedDateTime( FormatStyle.SHORT , FormatStyle.MEDIUM )
.withLocale( Locale.CANADA_FRENCH )
.withZone( ZoneId.of( "America/Montreal" ) )
)
)
.setHeader( "Created" )
;
Continent/Region
,如
America/Montreal
,
Africa/Casablanca
, 或
Pacific/Auckland
.切勿使用 2-4 个字母的缩写,例如
EST
或
IST
因为它们不是真正的时区,不是标准化的,甚至不是唯一的(!)。
withZone
和
withLocale
方法产生新的新鲜
DateTimeFormatter
而不是改变原来的。所以你可能想保留一个全局单例
DateTimeFormatter
根据您对短日期和较长时间的偏好。
DateTimeFormatter f = DateTimeFormatter
.ofLocalizedDateTime(
FormatStyle.SHORT , // Length of date portion.
FormatStyle.MEDIUM // Length of time-of-day portion.
)
;
DateTimeFormatter
对象,而由于 java.time 中使用的不可变对象(immutable对象)模式,原始对象不受影响。
invoicesGrid
.addColumn(
new InstantRenderer <>( Invoice :: getWhenCreated ,
f
.withLocale( user.getPreferredLocale() )
.withZone( user.getPreferredZone() )
)
)
.setHeader( "Created" )
;
String
在
Instant
的情况下使用正在渲染的对象为空。默认是不向用户显示任何文本,一个空的
""
字符串。如果您愿意,可以传递一些其他字符串,例如
null
或
void
.
package work.basil.example.ui;
/*
* Copyright 2000-2020 Vaadin Ltd.
* Copyright 2020 Basil Bourque.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Locale;
import java.util.Objects;
import com.vaadin.flow.data.renderer.BasicRenderer;
import com.vaadin.flow.function.ValueProvider;
/*
* This class is based on source-code directly copied from
* `LocalDateTimeRenderer.java` of Vaadin 14.1.x
* as written and published by Vaadin Ltd. from their GitHub page.
*
* https://github.com/vaadin/flow/blob/master/flow-data/src/main/java/com/vaadin/flow/data/renderer/LocalDateTimeRenderer.java
*
* I re-purposed that class to handle `Instant` objects rather than `LocalDateTime`
* objects. An `Instant` represents a moment, whereas `LocalDateTime` cannot because
* of it lacking any concept of time zone or offset-from-UTC. In contrast, `Instant`
* represents a moment in UTC (an offset-from-UTC of zero hours-minutes-seconds).
*
* By default, a `Instant` object renders in Vaadin by way of its `toString` method
* generating text in standard ISO 8601 format YYYY-MM-DDTHH:MM:SS.SSSSSSSSSZ.
*
* If you want other than ISO 8601 format in UTC, use this class. In this class, we
* apply a time zone (`ZoneId`) to the `Instant` to adjust from UTC.
*
* The `ZoneId` object comes from one of three places:
* - Passed implicitly by being set as a property on a `DateTimeFormatter`
* object passed as an argument. This is the best case.
* - Defaults to calling `ZoneId.systemDefault` if not found
* on the `DateTimeFormatter` object (where `getZone` returns null).
*
* I deleted the constructors taking a formatting pattern string. Parsing such a string
* and instantiating a `DateTimeFormatter` and handling resulting error conditions
* should *not* be the job of this class. I believe the Vaadin team made a poor choice
* in having constructors taking a string formatting pattern rather than just a
* `DateTimeFormatter` object.
*
* Locale is another critical issue. A `Locale` object determines:
*
* (a) The human language used for translating items such as name of month and
* name of day.
*
* (b) The cultural norms used in deciding localization issues such as the ordering
* of elements (ex: day comes before or after month), abbreviation, capitalization,
* punctuation, and so on.
*
* Again, I deleted the constructors taking a `Locale` object. The `DateTimeFormatter`
* object passed by the calling programmer carries a `Locale`. That calling programmer
* should have attached their intended locale object to that `DateTimeFormatter` object
* by calling `DateTimeFormatter::withLocale`. Usually a `DateTimeFormatter` has a default
* `Locale` assigned. But if found lacking, here we attach the JVM’s current default locale.
*
* Following the logic discussed above, I chose to not take a `ZoneId` as an argument.
* A `ZoneId` can be attached to the `DateTimeFormatter` by calling `withZoneId`.
* If the passed `DateTimeFormatter` is found lacking, here we attach the JVM’s current
* default time zone.
*
* Typical usage, passing 2 arguments, a method reference and a `DateTimeFormatter` object
* while omitting 3rd optional argument for null-representation to go with an blank empty string:
*
* myGrid
* .addColumn(
* new InstantRenderer <>( TheBusinessObject :: getWhenCreated ,
* DateTimeFormatter
* .ofLocalizedDateTime( FormatStyle.SHORT , FormatStyle.MEDIUM )
* .withLocale( Locale.CANADA_FRENCH )
* .withZone( ZoneId.of( "America/Montreal" ) )
* )
* )
*
* This code is written for Java 8 or later.
*
* For criticisms and suggestions, contact me via LinkedIn at: basilbourque
*/
/**
* A template renderer for presenting {@code Instant} objects.
*
* @param <SOURCE> the type of the input item, from which the {@link Instant}
* is extracted
* @author Vaadin Ltd
* @since 1.0.
*/
public class InstantRenderer < SOURCE >
extends BasicRenderer < SOURCE, Instant >
{
private DateTimeFormatter formatter;
private String nullRepresentation;
/**
* Creates a new InstantRenderer.
* <p>
* The renderer is configured to render with the format style
* {@code FormatStyle.LONG} for the date and {@code FormatStyle.SHORT} for
* time, with an empty string as its null representation.
*
* @param valueProvider the callback to provide a {@link Instant} to the
* renderer, not <code>null</code>
* @see <a href=
* "https://docs.oracle.com/javase/8/docs/api/java/time/format/FormatStyle.html#LONG">
* FormatStyle.LONG</a>
* @see <a href=
* "https://docs.oracle.com/javase/8/docs/api/java/time/format/FormatStyle.html#SHORT">
* FormatStyle.SHORT</a>
*/
public InstantRenderer (
ValueProvider < SOURCE, Instant > valueProvider )
{
this(
valueProvider ,
DateTimeFormatter
.ofLocalizedDateTime( FormatStyle.LONG )
.withZone( ZoneId.systemDefault() )
.withLocale( Locale.getDefault() ) ,
""
);
}
/**
* Creates a new InstantRenderer.
* <p>
* The renderer is configured to render with the given formatter, with the
* empty string as its null representation.
*
* @param valueProvider the callback to provide a {@link Instant} to the
* renderer, not <code>null</code>
* @param formatter the formatter to use, not <code>null</code>
*/
public InstantRenderer (
ValueProvider < SOURCE, Instant > valueProvider ,
DateTimeFormatter formatter
)
{
this(
valueProvider ,
formatter ,
""
);
}
/**
* Creates a new InstantRenderer.
* <p>
* The renderer is configured to render with the given formatter.
*
* @param valueProvider the callback to provide a {@link Instant} to the
* renderer, not <code>null</code>
* @param formatter the formatter to use, not <code>null</code>
* @param nullRepresentation the textual representation of the <code>null</code> value
*/
public InstantRenderer (
final ValueProvider < SOURCE, Instant > valueProvider ,
final DateTimeFormatter formatter ,
final String nullRepresentation
)
{
super( valueProvider );
this.formatter = Objects.requireNonNull( formatter , "formatter may not be null" );
this.nullRepresentation = Objects.requireNonNull( nullRepresentation , "null-representation may not be null" );
// If the formatter provided by the calling programmer lacks a time zone, apply the JVM's current default zone.
// This condition is less than ideal. The calling programmer should have set an appropriate zone.
// Often the appropriate zone is one specifically chosen or confirmed by the user.
if ( Objects.isNull( this.formatter.getZone() ) )
{
this.formatter = this.formatter.withZone( ZoneId.systemDefault() );
}
// If the formatter provided by the calling programmer lacks a locale, apply the JVM's current default locale.
// This condition is less than ideal. The calling programmer should have set an appropriate locale.
// Often the appropriate locale is one specifically chosen or confirmed by the user.
if ( Objects.isNull( this.formatter.getLocale() ) )
{
this.formatter = this.formatter.withLocale( Locale.getDefault() );
}
}
@Override
protected String getFormattedValue ( final Instant instant )
{
// If null, return the null representation.
// If not null, adjust the `Instant` from UTC into the time zone attached to the `DateTimeFormatter` object.
// This adjustment, made by calling `Instant::atZone`, produces a `ZonedDateTime` object.
// We then create a `String` with text representing the value of that `ZonedDateTime` object.
// That text is automatically localized per the `Locale` attached to the `DateTimeFormatter` object.
String s = Objects.isNull( instant ) ? nullRepresentation : formatter.format( instant.atZone( this.formatter.getZone() ) );
return s;
}
}
关于java - 用于 java.time 日期时间类型的 Vaadin Flow 渲染器,不仅限于 LocalDateTime 和 LocalDate 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59817621/
我有一个示例应用程序,包含以下三个步骤 输入用户详细信息 -> 个人详细信息 -> 完成 当我点击一个链接时,它会调用我的 vaadin 应用程序。问题是一旦我的三步过程完成,当我再次尝试点击我的 v
我希望框架捕获每个未捕获的异常并显示适当的通知消息。 因此我在我的 UI 中设置了一个自定义的 ErrorHandler(灵感来自“Book of Vaadin”:https://vaadin.com
我在 Vaadin 有一张 table 。一列是 String 类型,因此其大小是可变的。因此,我想让列的宽度固定(具有扩展比率),但每行的高度应该是可变的(根据其内容 - 字符串)。 即,这是预期的
有谁知道用于 vaadin 框架的数据可视化(图表、饼图等)的任何 UI 组件库? 最佳答案 我建议 dChart: https://vaadin.com/directory#!addon/dchar
我在 Grid 中使用 BeanItemContainer 来显示我的数据。我需要根据单元格中的数据分别为每个单元格着色。 最佳答案 section about the Grid在 Vaadin 的书
在我的一个应用程序中,我创建了一个页脚: public Footer(){ hl.setHeight("120px"); hl.setWidth("100%");
是否有任何特定方法可以在 Vaadin 表中实现分页/延迟加载。 我试图找到一些文档,但我找不到。 最佳答案 使用Viritin add-on及其 MTable 组件。 I 是最有效的(服务器资源副)
我正在尝试使用 Vaadin Charts 创建饼图。 这段代码为图表添加了漂亮的标签,但小数点 t 后两位数字就足够了。 dataLabels.setFormatter("''+ this.poi
我是 vaadin 框架的新手,一切正常,直到......表头没有正确显示,......我正在开发一个在 exoplatform 上运行的 portlet,我正在使用 vaadin 6,我的表只显示第
我有一个带有基于复杂查询填充数据的表的页面(即使我使用分页也需要很多时间)。我使用 BeanItemcontainer 将数据加载到表中。现在遇到问题,我希望以异步方式将数据加载到表中(在用户屏幕上加
我有一个现有的应用程序(在 WebGuiToolkit.org 中编写),我正在尝试在其中嵌入一个 Vaadin 14 页面。 我看过几个 Vaadin 8 指南,比如 https://vaadin.
更改pom.xml文件的vaadin版本号和vaadin插件版本号后构建失败 7.4.5 7.4.5 然后我尝试清理并重建项目,但显示构建失败: Copying 3 resources --- va
我想通过更改文件列表中元素的外观来设置 Vaadin Upload 组件 (vaadin-upload) 的样式,例如隐藏命令按钮(开始、删除、重试)。文件列表包含 vaadin-upload-fil
如何向文本字段、选项卡、链接、标签等添加悬停文本(即当我将鼠标悬停在组件上时出现的包含一些解释或详细信息的文本框) 我用谷歌搜索并浏览了“Book of Vaadin”和“Building Moder
this.grid.asSingleSelect().addValueChangeListener(event -> { if (!Objects.isNull(event.getVa
在 Vaadin 中,一旦您有了 TabSheet,并且已经打开了一些选项卡,您就不会希望多次打开包含相同内容的同一个 Tab。 如何检查选项卡是否已打开并将其设置为选定的选项卡? 最佳答案 默认情况
我已经使用 vaadin 创建了表格。现在我想为该表格中的特定列内容设置字体大小。是否可以为该表格中的特定列设置字体大小?。 如果是这样,请给我设置字体大小的想法。如果你能提供我一些代码片段。 最佳答
是否可以在 vaadin 中以百分比设置表格的列宽。如果可以,请告诉我如何使用示例代码片段来做到这一点。提前谢谢。 最佳答案 是的。为此使用 Table.setColumnExpandRatio(co
我正在开发 Vaadin 8 UI。目前,每次修改 UI 时,我都必须重新启动我的应用程序。 有没有更好的办法?我曾尝试附加 Java 调试器并使用 IntelliJ 的 Reload Changed
序言:我是Vaadin的高级开发人员(我用过6、7,现在我的所有项目都已迁移到Vaadin 8)。 我开始学习Vaadin 10/Flow,但发现自己在一些热水中。 我实际上在挣扎的是“项目”本身。
我是一名优秀的程序员,十分优秀!