解读roller系列之----地区和时区的处理
来源:互联网 发布:ubuntu下中文输入法 编辑:程序博客网 时间:2024/05/17 21:43
在roller中获得地区和时区的关键类是StrutsUtil, LocaleComparator和TimeZoneComparator为辅助类,主要是实现对地区和时区的排序功能。
StrutsUtil
类:
该类将用户可以获得的地区和时区封装成
ArrayList
类,并实现了
LabelValueBean
接口,以便在采用
Struts
框架中显示。
代码如下:
package org.roller.presentation.util;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;
import java.util.TreeSet;
import org.apache.struts.util.LabelValueBean;
import org.roller.util.LocaleComparator;
import org.roller.util.TimeZoneComparator;
public class StrutsUtil
{
public static ArrayList locales;
public static ArrayList timeZones;
//-----------------------------------------------------------------------
/**
* LabelValueBeans are Comparable but violate the
* equals() part of the TreeSet requirements.
* And the html:options tag won't recognize
* toString as a property. So we have to put the
* Locales into a TreeSet to sort them, then convert
* them to LabelValueBeans to display them.
* Glad we only have to do this once.
*
* @return List of LabelValueBeans, one for each locale available from the JVM
*/
public static List getLocaleBeans()
{
if (locales == null)
{
locales = new ArrayList();
//
有关
TreeSet
的知识请参考“软道
—java
语言”栏目。
TreeSet locTree = new TreeSet(new LocaleComparator());
Locale[] localeArray = Locale.getAvailableLocales();
for (int i=0; i<localeArray.length; i++)
{
locTree.add(localeArray[i]);
}
java.util.Iterator it = locTree.iterator();
while (it.hasNext())
{
Locale loc = (Locale)it.next();
locales.add(new LabelValueBean(
loc.getDisplayName(),
loc.toString()));
}
}
return locales;
}
//-----------------------------------------------------------------------
/**
* html:options tag recognizes "ID" as a property
* so we don't have to go through all the rigamarole (sp?)
* that we did for Locales.
*/
public static List getTimeZoneBeans()
{
if (timeZones == null)
{
Date today = new Date();
timeZones = new ArrayList();
//
有关
TreeSet
的知识请参考“软道
—java
语言”栏目。
TreeSet zoneTree = new TreeSet(new TimeZoneComparator());
String[] zoneArray = TimeZone.getAvailableIDs();
for (int i=0; i<zoneArray.length; i++)
{
zoneTree.add((TimeZone)TimeZone.getTimeZone(zoneArray[i]));
}
java.util.Iterator it = zoneTree.iterator();
while (it.hasNext())
{
StringBuffer sb = new StringBuffer();
TimeZone zone = (TimeZone)it.next();
sb.append(zone.getDisplayName(zone.inDaylightTime(today), TimeZone.SHORT));
sb.append(" - ");
sb.append(zone.getID());
timeZones.add(new LabelValueBean(
sb.toString(),
zone.getID()));
}
}
return timeZones;
}
}
package org.roller.util;
import java.util.Locale;
import java.util.Comparator;
import java.io.Serializable;
public class LocaleComparator implements Comparator, Serializable
{
public int compare(Object obj1, Object obj2)
{
if (obj1 instanceof Locale && obj2 instanceof Locale)
{
Locale locale1 = (Locale)obj1;
Locale locale2 = (Locale)obj2;
int compName = locale1.getDisplayName().compareTo(locale2.getDisplayName());
if (compName == 0)
{
return locale1.toString().compareTo(locale2.toString());
}
return compName;
}
return 0;
}
/* Do Comparators need to implement equals()? -Lance
public boolean equals(Object obj)
{
if (obj instanceof LocaleComparator)
{
if (obj.equals(this)) return true;
}
return false;
}
*/
}
package org.roller.util;
import java.util.TimeZone;
import java.util.Comparator;
import java.io.Serializable;
public class TimeZoneComparator implements Comparator, Serializable
{
public int compare(Object obj1, Object obj2)
{
if (obj1 instanceof TimeZone && obj2 instanceof TimeZone)
{
TimeZone zone1 = (TimeZone)obj1;
TimeZone zone2 = (TimeZone)obj2;
int compName = zone1.getDisplayName().compareTo(zone2.getDisplayName());
if (compName == 0)
{
return zone1.getID().compareTo(zone2.getID());
}
return compName;
}
return 0;
}
/* Do Comparators need to implement equals()? -Lance
public boolean equals(Object obj)
{
if (obj instanceof TimeZoneComparator)
{
if (obj.equals(this)) return true;
}
return false;
}
*/
}
- 解读roller系列之----地区和时区的处理
- 根据地区和时区获取当地时间
- android地区/时区/语言
- python的时区处理
- GMT与Etc/GMT地区信息的时区转换
- Alamofire源码解读系列(二)之错误处理(AFError)
- Alamofire源码解读系列(二)之错误处理(AFError)
- Alamofire源码解读系列(二)之错误处理(AFError)
- Alamofire源码解读系列(二)之错误处理(AFError)
- Alamofire源码解读系列(三)之通知处理(Notification)
- 44.Oracle数据库SQL开发之 日期和时间的存储与处理——理解时区
- java处理时区的注意事项
- Java处理时区的注意事项
- java处理时区的注意事项
- 世界上的国家和地区
- Alamofire源码解读系列(一)之概述和使用
- Logstash之时区问题的建议和修改
- Logstash之时区问题的建议和修改
- Masm编译环境配置
- 几个网络测试命令
- 用ADOExpress连接数据库
- DB2优化
- ZT:ClientDataSet使用心得和技巧[大富翁转]
- 解读roller系列之----地区和时区的处理
- Access数据库的存储上限
- 如果静态方法里面没有存取静态字段的话, 就没有并发的线程安全的问题...(这句话对吗?)
- request processing lifecycle phase1 restore view
- sizeof的用法
- 关于visual studio 2005的中文版下载(最新详细下载点)
- C/C++单元测试工具Visual Unit新手入门
- 笑话——我太丑了
- 晕啊,才发现原来我的电脑还有问题啊!