- 浏览: 51905 次
最新评论
目标:面向多区域用户
添加更新记录:用户输入本区域时间, 数据库存储转换后的标准时间
查看记录: 数据库时间相同,view层根据用户区域显示相应的时间
说明本项目要求日期格式固定,不按照用户local显示. 如果要求按照用户local显示日期格式则需要使用如下的format, 可参考liferay源码
分析
liferay 提供了用户自定义zone (控制面板->setting->zone, 详见"liferay区域设置")。
页面显示时可以根据用户zone格式化取得的时间。
向后台发送数据时根据客户zone取得客户输入时间,转换为tomcat时间后再传入数据库
原则(强烈建议
对于日期类型从后台向前台读取的过程中, 在business层不要做类型转换(例如试图转为long或String ), 只有在页面显示的时候才将其转为合适的日期格式和区域。
对于其他类型做同样的建议,如double, longtext,例如在business层中不要试图截取小数点位数和文本长度,在页面根据需要截取。如果业务上有特殊要求除外(如财务运算)
timezone 涉及的 web app 层次
1.view (jsp)
2.business(portletaction + service + finder/persistence)
//此处因为portlect负责读取输入的日期数据, finder负责写入日期数据,为方便说明timezone的传递,将其归入business层次, 勿纠结于层次划分
3.DB (mysql)
------------- util code -----------
DateUtil类提供公共方法获取包含用户时区和local的format
html/init.jsp提供公共format,包含用户zone和local 也可以直接使用DateUtil类提供的方法
html/init.jsp
------------- zone设置 -----------
用户A,leferay中设置 zone = GMT+7
TOMCAT zone = GMT+8
mysql zone = system = GMT+9
设定
用户试图输入日期 GMT+8 2013-01-01 12:00:00 保存至数据库
执行操作时web server 系统时间 GMT+8 2013-01-01 12:10:10
// web server 系统时区可任意设置,不影响以下结果
------------- 添加更新记录 -----------
VIEW 层
用户A输入日期 " 2013-01-01 11:00:00",提交 (GMT+7)
BUSINESS 层
获取页面输入日期,更新数据库
说明:
(1) dateFormatDateTime 的默认时区为用户时区GMT+7
(2) dateRead 获取时间为用户输入时间, 按照用户时区GMT+7转换为日期后再转换为tomcat时区GMT+8。
(3) new Date() 获取时间为 web server 系统时间, 时区为tomcat设置的时区GMT+8。
注意:
ParamUtil.getDate(actionRequest, "date", dateFormatDateTime)方法实际隐含了两次转换
(1) 用户输入时间 2013-01-01 12:00:00,按用户时区(dateFormatDateTime) 转换为日期类型 GMT+7 2013-01-01 11:00:00
(2) GMT+7 2013-01-01 11:00:00 转换为tomcat时区 GMT+8 2013-01-01 12:00:00
DB层
存储日期
数据库表中存储的dateInput和dateNew分别为
dateInput = 2013-01-01 12:00:00
dateNew = 2013-01-01 04:10:10
注意:
数据库本身有时区定义,参见mysql时区定义。
mysql的时区定义会对now()获得字符串产生影响。
可以认为数据库存储的时间类型字段不包含时区信息,insert/update字符串即是所见字符串
例如:
传入参数:
dateInput = GMT+8 2013-01-01 12:00:00
dateNew = GMT+8 2013-01-01 12:10:10
mysql-〉 select dateInput, dateNew
结果: 2013-01-01 12:00:00 2013-01-01 12:10:10
传入参数:
dateInput = GMT+9 2013-01-01 12:00:00
dateNew = GMT+9 2013-01-01 12:10:10
mysql-〉 select dateInput, dateNew
结果不变: 2013-01-01 12:00:00 2013-01-01 12:10:10
------------- 读取数据到页面 -----------
DB层
存储日期
数据库表中存储的dateInput和dateNew分别为
dateInput = 2013-01-01 12:00:00
dateNew = 2013-01-01 12:10:10
BUSINESS 层
获取数据库存储日期,返回到页面
结果:
dateInput = GMT+8 2013-01-01 12:00:00
dateNew = GMT+8 2013-01-01 12:10:10
VIEW 层
显示日期
<input value=<%=DateUtil.safeDateTime(map.get("dateInput"), locale, timeZone)%> />;
<input value=<%=dateFormatDateTime.format(dateNew)%> />;
<input value=<%=dateFormatDateTime.format(newDate())%> />;
页面显示结果
dateInput : 2013-01-01 11:00:00
dateNew : 2013-01-01 12:10:10
dateNew : 2013-01-01 12:10:10
------------- 总结 -----------
1. web app server 时区与 web server时区无关。
2. db server时区对程序无影响(不要使用now()函数更新数据库)
3. new date(), new dateformat() 生成对象时区为web app server 时区
4. 如果不考虑用户时区,则只需要设定tomcat时区即可保持系统一致。代码中适用new dateformat()格式化日期,部另外设置时区。
------------- [color=blue] END 2013-01-07 -----------
添加更新记录:用户输入本区域时间, 数据库存储转换后的标准时间
查看记录: 数据库时间相同,view层根据用户区域显示相应的时间
说明本项目要求日期格式固定,不按照用户local显示. 如果要求按照用户local显示日期格式则需要使用如下的format, 可参考liferay源码
Format dateFormat = FastDateFormatFactoryUtil.getSimpleDateFormat("MMMM, yyyy", locale); dateFormat .setTimeZone(timeZone); Format timeFormat = FastDateFormatFactoryUtil.getSimpleDateFormat("h:mma", locale); timeFormat .setTimeZone(timeZone);
分析
liferay 提供了用户自定义zone (控制面板->setting->zone, 详见"liferay区域设置")。
页面显示时可以根据用户zone格式化取得的时间。
向后台发送数据时根据客户zone取得客户输入时间,转换为tomcat时间后再传入数据库
原则(强烈建议
对于日期类型从后台向前台读取的过程中, 在business层不要做类型转换(例如试图转为long或String ), 只有在页面显示的时候才将其转为合适的日期格式和区域。
对于其他类型做同样的建议,如double, longtext,例如在business层中不要试图截取小数点位数和文本长度,在页面根据需要截取。如果业务上有特殊要求除外(如财务运算)
timezone 涉及的 web app 层次
1.view (jsp)
2.business(portletaction + service + finder/persistence)
//此处因为portlect负责读取输入的日期数据, finder负责写入日期数据,为方便说明timezone的传递,将其归入business层次, 勿纠结于层次划分
3.DB (mysql)
------------- util code -----------
DateUtil类提供公共方法获取包含用户时区和local的format
public class DateUtil { public static SimpleDateFormat getDateFormatDate(Locale locale, TimeZone timeZone) { SimpleDateFormat dateFormatDate = new SimpleDateFormat("yyyy-MM-dd", locale); dateFormatDate.setTimeZone(timeZone); return dateFormatDate; } public static SimpleDateFormat getDateFormatDateTime(Locale locale, TimeZone timeZone) { SimpleDateFormat dateFormatDate = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss", locale); dateFormatDate.setTimeZone(timeZone); return dateFormatDate; } public static String safeDate(Object obj, Locale locale, TimeZone timeZone) { SimpleDateFormat dfCustomer = getDateFormatDate(locale, timeZone); SimpleDateFormat dfServer = new SimpleDateFormat("yyyy-MM-dd"); String str = ""; try { if (Validator.isNotNull(obj)) { if (obj instanceof Date) { str = dfCustomer.format(obj); //important } else { str = dfCustomer.format(dfServer.parse(obj.toString())); //important } } } catch (Exception e) { str = ""; } return str; } public static String safeDateTime(Object obj, Locale locale, TimeZone timeZone) { SimpleDateFormat dfCustomer = getDateFormatDateTime(locale, timeZone); SimpleDateFormat dfServer = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String str = ""; try { if (Validator.isNotNull(obj)) { if (obj instanceof Date) { str = dfCustomer.format(obj); //important } else { str = dfCustomer.format(dfServer.parse(obj.toString())); //important } } } catch (Exception e) { str = ""; } return str; } }
html/init.jsp提供公共format,包含用户zone和local 也可以直接使用DateUtil类提供的方法
html/init.jsp
SimpleDateFormat dateFormatDate = DateUtil new SimpleDateFormat("yyyy-MM-dd", locale); dateFormatDate.setTimeZone(timeZone); SimpleDateFormat dateFormatDateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", locale); dateFormatDateTime.setTimeZone(timeZone);
------------- zone设置 -----------
用户A,leferay中设置 zone = GMT+7
TOMCAT zone = GMT+8
mysql zone = system = GMT+9
设定
用户试图输入日期 GMT+8 2013-01-01 12:00:00 保存至数据库
执行操作时web server 系统时间 GMT+8 2013-01-01 12:10:10
// web server 系统时区可任意设置,不影响以下结果
------------- 添加更新记录 -----------
VIEW 层
用户A输入日期 " 2013-01-01 11:00:00",提交 (GMT+7)
BUSINESS 层
获取页面输入日期,更新数据库
//portlet: ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute( WebKeys.THEME_DISPLAY); SimpleDateFormat dateFormatDateTime = DateUtil.getDateFormatDateTime(themeDisplay.getLocale(), themeDisplay.getTimeZone()); Date dateRead = ParamUtil.getDate(actionRequest, "date", dateFormatDateTime); Date dateNew = new Date(); XXXLocalServiceUtil.XXX(xx, dateInput, dateNew); /* 上面代码得到的日期为: dateInput = GMT+8 2013-01-01 12:00:00 dateNew = GMT+8 2013-01-01 12:10:10 */ //service: ModelXXX modelxxx = ModelXXXPersistence.create(Id); modelxxx.setdateInput(dateInput); modelxxx.setdateNew(dateNew); ModelXXXPersistence.update(modelxxx, false);
说明:
(1) dateFormatDateTime 的默认时区为用户时区GMT+7
(2) dateRead 获取时间为用户输入时间, 按照用户时区GMT+7转换为日期后再转换为tomcat时区GMT+8。
(3) new Date() 获取时间为 web server 系统时间, 时区为tomcat设置的时区GMT+8。
注意:
ParamUtil.getDate(actionRequest, "date", dateFormatDateTime)方法实际隐含了两次转换
(1) 用户输入时间 2013-01-01 12:00:00,按用户时区(dateFormatDateTime) 转换为日期类型 GMT+7 2013-01-01 11:00:00
(2) GMT+7 2013-01-01 11:00:00 转换为tomcat时区 GMT+8 2013-01-01 12:00:00
DB层
存储日期
数据库表中存储的dateInput和dateNew分别为
dateInput = 2013-01-01 12:00:00
dateNew = 2013-01-01 04:10:10
注意:
数据库本身有时区定义,参见mysql时区定义。
mysql的时区定义会对now()获得字符串产生影响。
可以认为数据库存储的时间类型字段不包含时区信息,insert/update字符串即是所见字符串
例如:
传入参数:
dateInput = GMT+8 2013-01-01 12:00:00
dateNew = GMT+8 2013-01-01 12:10:10
mysql-〉 select dateInput, dateNew
结果: 2013-01-01 12:00:00 2013-01-01 12:10:10
传入参数:
dateInput = GMT+9 2013-01-01 12:00:00
dateNew = GMT+9 2013-01-01 12:10:10
mysql-〉 select dateInput, dateNew
结果不变: 2013-01-01 12:00:00 2013-01-01 12:10:10
------------- 读取数据到页面 -----------
DB层
存储日期
数据库表中存储的dateInput和dateNew分别为
dateInput = 2013-01-01 12:00:00
dateNew = 2013-01-01 12:10:10
BUSINESS 层
获取数据库存储日期,返回到页面
//finder: map.put("dateInput", record[0] == null? "" : record[0]); map.put("dateNew", record[1] == null? "" : record[1]); /* 查询结果集 record[0] = Timestamp GMT+8 2013-01-01 12:00:00 record[1] = Timestamp GMT+8 2013-01-01 12:10:10 */ //service: Date dateInput = (Date)(map.get("dateInput")); Date dateNew = (Date)(map.get("dateNew")); /* 下面一种写法结果与上面代码相同, 但不提倡,finder和service中将Date和String来回转换存在风险SimpleDateFormat dateFormatDateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date dateInput = dateFormatDateTime.parse(map.get("dateInput").toString()); Date dateNew = dateFormatDateTime.parse(map.get("dateNew").toString()); */
结果:
dateInput = GMT+8 2013-01-01 12:00:00
dateNew = GMT+8 2013-01-01 12:10:10
VIEW 层
显示日期
<input value=<%=DateUtil.safeDateTime(map.get("dateInput"), locale, timeZone)%> />;
<input value=<%=dateFormatDateTime.format(dateNew)%> />;
<input value=<%=dateFormatDateTime.format(newDate())%> />;
页面显示结果
dateInput : 2013-01-01 11:00:00
dateNew : 2013-01-01 12:10:10
dateNew : 2013-01-01 12:10:10
------------- 总结 -----------
1. web app server 时区与 web server时区无关。
2. db server时区对程序无影响(不要使用now()函数更新数据库)
3. new date(), new dateformat() 生成对象时区为web app server 时区
4. 如果不考虑用户时区,则只需要设定tomcat时区即可保持系统一致。代码中适用new dateformat()格式化日期,部另外设置时区。
------------- [color=blue] END 2013-01-07 -----------
发表评论
-
AOP
2017-02-28 13:14 446http://blog.csdn.net/moreev ... -
liferay 6.1 CE + LDAP 导入自定义字段
2013-12-21 16:06 866导入自定义字段时无论如何尝试都不成功,但是自定义字段的值导 ... -
liferay 6.1 CE LDAP
2013-12-14 14:20 2282goole了许多网页,讲的是五花八门,最后想起来去port ... -
research
2013-12-10 10:42 0<aui:select label="typ ... -
fetch 与 find 的区别
2013-11-11 09:55 871liferay中 findxxxx 与fetchxxx 方 ... -
liferay 一对多 多对多 关系 (model)
2013-11-07 11:24 610通过model层实现 例如 people 与 ad ... -
liferay 一对多 多对多 关系 (build-service)
2013-11-07 11:04 634通过配置service.xml 实现 说明:一对多 ... -
liferay Logical Architecture
2013-09-11 15:20 1151Logical Architecture 详 ... -
liferay 文件下载失败
2013-07-01 14:48 541现象: http://issues.liferay.com/ ... -
403
2013-06-05 16:41 0portal.properties auth.forward ... -
[转] Liferay多数据源配置及开发
2013-06-05 16:16 724http://www.chinasb.org/archives ... -
aui-autocomplete [ 三 ] 源码解析
2013-01-09 09:53 1557懒了, 只翻译几个目前用到的属性,其实英文写的很明白的,需要花 ... -
aui-autocomplete [ 二 ] 验证未通过保留输入值
2013-01-09 09:25 821<% long myUserId = ParamU ... -
aui-autocomplete [ 一 ] override
2013-01-08 13:58 817liferay aui-autocomplete 如: 数据 ... -
Liferay AutoFields 添加事件
2013-01-08 09:30 0new Liferay.AutoFields( ... -
liferay auto_filed
2012-12-03 14:51 746new Liferay.AutoFields( ... -
liferay at java.util.regex.Pattern$Curly.match0
2012-11-21 13:17 898build-service 出现liferay at java ... -
actionurl 参数顺序
2012-10-19 14:16 1466PortletURL editURL = renderResp ... -
java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.
2012-10-19 14:06 3994现象: liferay中查询数据,用到如下语句 select ... -
Unable to find required classes (javax.activation.DataHandler and javax.mail.int
2012-09-04 13:41 9652.1.8 错误原因: 需要mail.jar和activat ...
相关推荐
appweb-src-3.2.2-1.tar.gz(源码)
angulr-bootstrap-admin-web-app-with-angularjs 2.0版本来拉。由于最近比较忙所有没有持续更新这个版本。在此特地发一下2.0.1版本的。如果有需要我之前版本的请查看我的资源,移驾之前的资源。绝对官网下载的完整包...
最新发布的appweb服务器版本,很不错
Appweb是一個嵌入式HTTP Web服務器,主要的設計思路是安全。這是直接集成到客戶的應用和設備,便於開發和部署基於Web的應用程序和設備。它正迅速( 3500多要求每秒)和緊湊(從11萬) ,其中包括支持動態網頁製作,...
开源技术构建 Web App 架构-Paracutin-豌豆荚
关于uni-app使用web-view页面之间互相跳转、通信等问题的解决方案.docx
webpack-web-app-manifest-plugin webpack-web-app-manifest-plugin是一个Webpack插件,可生成PWA清单并与生成的资产JSON集成。如何安装npm install --save-dev webpack-web-app-manifest-plugin 或者yarn add ...
Web-App-Developer-2-by-InfoQ
uni-app app实现web-view H5图片长按下载
前端开源库-web-app-manifest-loaderWeb应用程序清单加载程序,Web包的Web应用程序清单加载程序
the latest appweb source code.
appweb服务器,适合移植嵌入式系统中
Web-App-Developer-3-by-InfoQ[汇编].pdf
appweb-6.2.1-src.tgz
AppWeb是完全遵循GPL软件许可协议(GNU General Public License)的开源Web服务器。 AppWeb Web服务器软件在功能上比GoAhead更加丰富和实用, 除了GoAhead已有的功能之外,还有如下特点: 支持服务器端的...
WhatsApp业务API设置脚本(英语) 该存储库包含用于使用docker-compose设置WhatsApp Business API的所有配置脚本。 有关与WhatsApp Business API有关的更多详细信息,请访问以下位置的文档: : 执照WhatsApp业务API...
这是个不错的嵌入式web server,功能强大,支持多种动态脚本。
Angulr – Bootstrap Admin Web App with AngularJS-v2.2.1 演示地址: http://themeforest.net/item/angulr-bootstrap-admin-web-app-with-angularjs/8437259
angulr-bootstrap-admin-web-app-with-angularjs 2.2.1版本来拉。由于最近比较忙所有没有持续更新这个版本。在此特地发一下2.2.1版本的。如果有需要之前版本的请查看我的资源,移驾之前的资源。绝对官网下载的完整包...
ROOT_REPO= ' git@github.com:jon-grey/sample-app-module-federation-azure-web-app-angular-azure-function-api-python.git ' APP_REPO= ' git@github....