- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
这是我在这个论坛上的第一个问题,请耐心等待。
Oracle 说“ZonedDateTime 是一个具有完全限定时区的日期和时间。这可以解决任何时间点的偏移量。经验法则是,如果您想表示日期和时间而不依赖于上下文特定服务器的,您应该使用 ZonedDateTime。”这正是我想要做的,因为应用程序正在处理全局交互,但 MySQL 似乎只将 DATETIME 保存为 TIMESTAMP,但它显然将其保存为 UTC,以便它可以转换为任何时区。我们将在其上运行的服务器将在多个时区运行,我们不知道哪个将在何处运行,因为云提供商将根据需求和维护动态移动它们。
因此,在此应用程序中维护日期/时间/时区似乎非常适合新的 ZonedDateTime 构造,但我一直感到困惑,试图在 PrimeFaces 和其他组件代码仍然提供的遗留日期之间保持一切正常和 MySQL,它想要处理最终将在 2038 年过时的时间戳。
我们不想使用任何外部日期库,例如 Joda 或 Apache。
我的问题相当直截了当,但答案对我来说似乎难以捉摸,而且细微差别似乎很多:将 java ZonedDateTime 保存到 MySQL 数据库、读回它以便工作的最佳实践是什么可以由用户通过 java 在全局范围内执行即时计算,这对本地用户来说是正确的,并且无论 Glassfish 服务器或 MySQL 服务器的位置如何,它们都可能彼此处于不同的时区,而且每天都在不同的时区天?
最佳答案
我认为这让我们震惊的方式是:MySQL 在将日期保存为时间戳时将日期保存为 UTC,因此只要我这样做,MySQL 位于何处都无关紧要。
Glassfish 可以通过查询服务器告诉您它所在的位置,但它也可以为家庭办公室设置一个属性,为您提供一个在服务器所在的任何地方都一致的操作基础。你可以在 web.xml 中做到这一点
<context-param>
<param-name>GLASSFISH_HOME_TIME_ZONE</param-name>
<param-value>America/New_York</param-value>
</context-param>
数据 bean 需要完成大部分工作,以便它与所有数据使用中的数据保持一致。未更新到 ZonedDateTime 或仅部分更新的组件库的问题通常会使用 getter 调用数据,因此使用重载应该允许组件库找到它喜欢的特定方法。我创建了一个看起来像这样的数据 bean:
公共(public)类 DataBean {
private final ZoneId GLASSFISH_HOME_TIME_ZONE = ZoneId.of(FacesContext.getCurrentInstance().getExternalContext().getInitParameter( "GLASSFISH_HOME_TIME_ZONE"));
private ZonedDateTime dateToUseInGlassfish = null;
public DataBean (
Timestamp dateFromMySQL)
{
if ( dateFromMySQL == null ) {
this.dateToUseInGlassfish = null;
} else {
this.dateToUseInGlassfish = LocalDateTime.ofInstant(dateFromMySQL.toInstant(), GLASSFISH_HOME_TIME_ZONE ).atZone( GLASSFISH_HOME_TIME_ZONE );
}
}
/** Formatter for Date/Time */
private final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("MM/dd/yyyy ' at ' h:mm a z");
/** Formatter for Date only */
private final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
/** Get the date string formatted with date and time */
public String getDateToUseInGlassfishDateTimeFormatted() {
if ( dateToUseInGlassfish == null ) { return null; }
String formattedDate = dateTimeFormatter.format( dateToUseInGlassfish );
return formattedDate;
}
/** Get the date string formatted with date only */
public String getgetDateToUseInGlassfishDateFormatted() {
if ( dateToUseInGlassfish == null) { return null; }
String formattedDate = dateFormatter.format( dateToUseInGlassfish );
return formattedDate;
}
/** Get the date ZDT formatted (for calculations) */
public ZonedDateTime getgetDateToUseInGlassfish() {
return dateToUseInGlassfish;
}
/** Get the date as Date (for component libraries that automatically fetch then throw up with ZDT) */
public Date getDateToUseInGlassfishDate() {
if ( dateToUseInGlassfish == null) { return null; }
return Date.from( dateToUseInGlassfish.toInstant());
}
/** Set the date from ZDT (results from calculations stored in bean) */
public void setDateToUseInGlassfish( ZonedDateTime dateToUseInGlassfish ) {
this.dateToUseInGlassfish = dateToUseInGlassfish;
}
/** Set the date from Date with an automatic convert to ZDT */
public void setDateToUseInGlassfish( Date dateToUseInGlassfish ) {
if (dateToUseInGlassfish == null) {
this.dateToUseInGlassfish = null;
} else {
this.dateToUseInGlassfish = LocalDateTime.ofInstant( Instant.ofEpochMilli( dateToUseInGlassfish.getTime()), GLASSFISH_HOME_TIME_ZONE ).atZone( GLASSFISH_HOME_TIME_ZONE );
}
}
从 MySQL 获取日期作为时间戳是将其作为 UTC 时间点获取,它看起来像这样:
ResultSet resultSet = preparedSelectQuoteSql.executeQuery()) {
while (resultSet.next()) {
quoteBean = new QuoteBean(
resultSet.getTimestamp("MySQLDateColumn")
);
}
}
将其从 ZonedDateTime 插入/更新到 MySQL 中,成为 MySQL 自动转换为 UTC 的时间戳,这样我们就可以让 MySQL 存在于任何我们希望它存在的地方,并及时读回相同的瞬间:
if ( insertValue instanceof ZonedDateTime ) {
if ( insertValue != null ) {
Timestamp convertedDate = Timestamp.from( ((ZonedDateTime) insertValue).toInstant() );
preparedStatement.setTimestamp( paramNumber, convertedDate );
} else {
preparedStatement.setNull ( paramNumber, Types.TIMESTAMP );
}
}
我认为这是可行的,但我欢迎批评。
关于java - 在 MySQL 和 Glassfish 中保存 ZonedDateTime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31909006/
如何将 glassfish 服务器的所有配置保存到另一个 glassfish 服务器? 最佳答案 看到这个问题:GlassFish v3 - portability of settings glass
我安装了 JDK 1.6 b45 并尝试运行 glassfish v2.2。当我尝试启动它时,总是会抛出以下错误: [#|2013-06-18T08:50:20.196+0200|WARNING|su
glassfish 访问日志位于哪里?我希望它们位于domains/domainX/logs 中。我需要更改设置才能激活访问日志记录吗? 谢谢 最佳答案 路径似乎正确,请查看此链接 http://bl
我想在没有互联网连接的情况下升级 Glassfish。但我已经下载了最新版本。 我已完成以下步骤, 例如。 galssfish-3.0 是旧版本,glassfish-3.1 是新版本。 步骤:1我刚刚
我在 Windows 8.1 中启动 glassfishs 服务时遇到问题 我得到了这个错误: c:\glassfishv3\bin>asadmin 创建服务 Found the Windows Se
我们最近切换到 Glassfish 3.1.2.2,并将几个 Web 应用程序打包为 war 文件。有时,这些应用程序所需的上下文根不同于文件名。 回到我们使用 Weblogic 时,我们通过在 we
我只是测试在 glassfish 服务器队列上发送消息,因此我在 glassfish 服务器上创建了 JMSResource,如下所示: 现在我已经在 Netbeans IDE 中创建了一个 Java
我想知道 GlassFish 项目是死了还是还活着?从一开始,我就非常喜欢玻璃鱼。我开始使用 GlassFish2 进行项目,并且有很多客户使用 GlassFish3。 几个月以来,我尝试将我的一些项
我使用 Glassfish 应用服务器,根据 README 文件,它只是说打开 CMD,键入一些命令,但我得到了 Nullpointer。我需要有人帮助我。我在 Windows 上。 asadmin
我最近将 Glassfish 从 3.1.2 更新到 4.0,并想设置一个我以前在我的应用程序上使用过的 JDBCRealm,它使用基于 FORM 的身份验证。密码在数据库中使用 SHA-256 散列
您如何确定正在运行哪个版本的GlassFish服务器? 该命令: /var/glassfish/bin/asadmin version ...回: Version = Sun Java System
所以,基本上: 在 RHEL 6.2 和 Java 6 上有一个独立的(无集群)Glassfish 3.1.2 新安装,没有任何部署的应用程序(真正的新安装)。 我第一次在服务器上启动了默认域 dom
我有一个带有 Glassfish 应用程序服务器的 EJB WEB 应用程序。 现在我想要像这样“/”的上下文根。 我当前的网址是“http://localhost:8080/Make ” 但我想要这
我正在尝试在 Windows 7 64 位上安装带有 Eclipse 的 GlassFish 4。一切正常,但是当我尝试设置服务器 root 时会发生以下情况(目录存在): 最佳答案 Glassfis
如何使用密码文件更改 Glassfish 域的管理员密码?我知道在提示时手动输入密码的传统方法。 但是,我想使用脚本更改管理员密码,而无需手动输入密码。 最佳答案 这是可能的,但如果您想以最简单的方式
我想在Glassfish 3.1中设置JDBC领域,并且关注此博客http://blog.gamatam.com/2009/11/jdbc-realm-setup-with-glassfish-v3.
我在远程机器上运行 Glassfish。是否有从命令提示符以 Debug模式启动 GF 的命令?远程机器运行 Ubuntu 10.04,我的机器运行 Windows 7(不确定这是否重要)。 我连接没
我们每周都会发布新的生产代码,Glassfish 没有遇到任何问题。这个周末我们不得不在我们的托管服务提供商处移动机架。没有任何代码更改(它们只是关闭电源、移动、重新安装和打开电源)但是我们在一个新的
对于生产环境,Apache Geronimo 是否更适合使用 ActiveMQ、Derby、Solr 的应用程序? 最佳答案 简而言之,如果您想要一个轻量级部署容器,请使用 Geronimo(它基于
我正在尝试设置一个备用文档根目录,以便提供上传的文档。我已将以下内容包含在我的 glassfish web xml 中 /dom 然后,我在名为 cars.pdf 的 Test 文件夹中存储了一
我是一名优秀的程序员,十分优秀!