目标:面向多区域用户
添加更新记录:用户输入本区域时间, 数据库存储转换后的标准时间
查看记录: 数据库时间相同,view层根据用户区域显示相应的时间
说明本项目要求日期格式固定,不按照用户local显示. 如果要求按照用户local显示日期格式则需要使用如下的format, 可参考liferay源码

Java代码 复制代码 收藏代码
  1. Format dateFormat = FastDateFormatFactoryUtil.getSimpleDateFormat("MMMM, yyyy", locale);
  2. dateFormat .setTimeZone(timeZone);
  3.  
  4. Format timeFormat = FastDateFormatFactoryUtil.getSimpleDateFormat("h:mma", locale);
  5. 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

Java代码 复制代码 收藏代码
  1. public class DateUtil {
  2. public static SimpleDateFormat getDateFormatDate(Locale locale,
  3. TimeZone timeZone) {
  4. SimpleDateFormat dateFormatDate = new SimpleDateFormat("yyyy-MM-dd",
  5. locale);
  6. dateFormatDate.setTimeZone(timeZone);
  7. return dateFormatDate;
  8. }
  9.  
  10. public static SimpleDateFormat getDateFormatDateTime(Locale locale,
  11. TimeZone timeZone) {
  12. SimpleDateFormat dateFormatDate = new SimpleDateFormat(
  13. "yyyy-MM-dd HH:mm:ss", locale);
  14. dateFormatDate.setTimeZone(timeZone);
  15. return dateFormatDate;
  16. }
  17.  
  18. public static String safeDate(Object obj, Locale locale, TimeZone timeZone) {
  19.  
  20. SimpleDateFormat dfCustomer = getDateFormatDate(locale, timeZone);
  21.  
  22. SimpleDateFormat dfServer = new SimpleDateFormat("yyyy-MM-dd");
  23.  
  24. String str = "";
  25. try {
  26. if (Validator.isNotNull(obj)) {
  27. if (obj instanceof Date) {
  28. str = dfCustomer.format(obj); //important
  29. } else {
  30. str = dfCustomer.format(dfServer.parse(obj.toString())); //important
  31. }
  32. }
  33. } catch (Exception e) {
  34. str = "";
  35. }
  36. return str;
  37. }
  38.  
  39. public static String safeDateTime(Object obj, Locale locale,
  40. TimeZone timeZone) {
  41.  
  42. SimpleDateFormat dfCustomer = getDateFormatDateTime(locale, timeZone);
  43. SimpleDateFormat dfServer = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  44.  
  45. String str = "";
  46. try {
  47. if (Validator.isNotNull(obj)) {
  48. if (obj instanceof Date) {
  49. str = dfCustomer.format(obj); //important
  50. } else {
  51. str = dfCustomer.format(dfServer.parse(obj.toString())); //important
  52. }
  53. }
  54. } catch (Exception e) {
  55. str = "";
  56. }
  57. return str;
  58. }
  59.  
  60. }

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

Java代码 复制代码 收藏代码
  1. SimpleDateFormat dateFormatDate = DateUtil new SimpleDateFormat("yyyy-MM-dd", locale);
  2. dateFormatDate.setTimeZone(timeZone);
  3.  
  4. SimpleDateFormat dateFormatDateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", locale);
  5. 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);