目标:面向多区域用户
添加更新记录:用户输入本区域时间, 数据库存储转换后的标准时间
查看记录: 数据库时间相同,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);
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;
- }
- }
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);
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);
转载于:https://blog.51cto.com/daheyuan/1134219