2012年9月5日
#
- import Java.util.*;
- public class ShowDate {
- public static void main(String[] args) {
- Calendar calendar = new GregorianCalendar();
- Date trialTime = new Date();
- calendar.setTime(trialTime);
-
- System.out.println("ERA: " + calendar.get(Calendar.ERA));
- System.out.println("YEAR: " + calendar.get(Calendar.YEAR));
- System.out.println("MONTH: " + calendar.get(Calendar.MONTH));
- System.out.println("WEEK_OF_YEAR: " + calendar.get(Calendar.WEEK_OF_YEAR));
- System.out.println("WEEK_OF_MONTH: " + calendar.get(Calendar.WEEK_OF_MONTH));
- System.out.println("DATE: " + calendar.get(Calendar.DATE));
- System.out.println("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH));
- System.out.println("DAY_OF_YEAR: " + calendar.get(Calendar.DAY_OF_YEAR));
- System.out.println("DAY_OF_WEEK: " + calendar.get(Calendar.DAY_OF_WEEK));
- System.out.println("DAY_OF_WEEK_IN_MONTH: " + calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));
- System.out.println("AM_PM: " + calendar.get(Calendar.AM_PM));
- System.out.println("HOUR: " + calendar.get(Calendar.HOUR));
- System.out.println("HOUR_OF_DAY: " + calendar.get(Calendar.HOUR_OF_DAY));
- System.out.println("MINUTE: " + calendar.get(Calendar.MINUTE));
- System.out.println("SECOND: " + calendar.get(Calendar.SECOND));
- System.out.println("MILLISECOND: " + calendar.get(Calendar.MILLISECOND));
- System.out.println("ZONE_OFFSET: " + (calendar.get(Calendar.ZONE_OFFSET)/(60*60*1000)));
- System.out.println("DST_OFFSET: " + (calendar.get(Calendar.DST_OFFSET)/(60*60*1000)));
- System.out.println("Current Time, with hour reset to 3");
- calendar.clear(Calendar.HOUR_OF_DAY);
- calendar.set(Calendar.HOUR, 3);
- System.out.println("ERA: " + calendar.get(Calendar.ERA));
- System.out.println("YEAR: " + calendar.get(Calendar.YEAR));
- System.out.println("MONTH: " + calendar.get(Calendar.MONTH));
- System.out.println("WEEK_OF_YEAR: " + calendar.get(Calendar.WEEK_OF_YEAR));
- System.out.println("WEEK_OF_MONTH: " + calendar.get(Calendar.WEEK_OF_MONTH));
- System.out.println("DATE: " + calendar.get(Calendar.DATE));
- System.out.println("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH));
- System.out.println("DAY_OF_YEAR: " + calendar.get(Calendar.DAY_OF_YEAR));
- System.out.println("DAY_OF_WEEK: " + calendar.get(Calendar.DAY_OF_WEEK));
- System.out.println("DAY_OF_WEEK_IN_MONTH: " + calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));
Android SDK下载和更新失败的解决方法
最近刚换了电脑,开始搭建Android开发环境的时候,下载SDK总是会出现如下错误:
1.Failed to fetch URL http://dl-ssl.google.com/Android/repository/addons_list-1.xml.
据说dl-ssl.google.com在大陆被强了,伟大的天朝真是不让人活了,解决方法就是修改C:\Windows\System32\drivers\etc\hosts文件。添加一行:
1.74.125.237.1 dl-ssl.google.com
这里需要注意的是hosts文件是只读的,我们没有权限修改,需要我们将hosts文件复制到桌面或者其他地方,然后修改,代码如下:
1.# Copyright (c) 1993-2009 Microsoft Corp.
2.#
3.# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
4.#
5.# This file contains the mappings of IP addresses to host names. Each
6.# entry should be kept on an individual line. The IP address should
7.# be placed in the first column followed by the corresponding host name.
8.# The IP address and the host name should be separated by at least one
9.# space.
10.#
11.# Additionally, comments (such as these) may be inserted on individual
12.# lines or following the machine name denoted by a '#' symbol.
13.#
14.# For example:
15.#
16.# 102.54.94.97 rhino.acme.com # source server
17.# 38.25.63.10 x.acme.com # x client host
18.
19.# localhost name resolution is handled within DNS itself.
20.# 127.0.0.1 localhost
21.# ::1 localhost
22.//亲,就是增加这一句哦
23.74.125.237.1 dl-ssl.google.com
然后保存,复制修改后的hosts文件到C:\Windows\System32\drivers\etc 目录,替换文件就好!!!我们再次下载SDK的时候就会成功啦,如下图:
嘿嘿,大功告成啦!!!
PS:补充下,在mac或Linux中,hosts文件所在位置为/etc/hosts,可以使用sudo vim /etc/hosts来编辑。
Tomcat 6.0.35 的功能有些不同于Tomcat 6.0.20。我下载的Tomcat 6.0.35是免安装的,而以前使用的Tomcat 6.0.20是需要安装的,而且Tomcat 6.0.20 的安装密码,即进入Tomcat Manager 这个地方的密码是在安装的时候指定的,但是呢,Tomcat 6.0.35 由于是免安装的,所以就没有地方去指定密码了,当我们访问Tomcat 6.0.35 的Tomcat Manager的时候 ,输入以前默认的用户名:admin 密码为空,会发现出现了403 错误,即access deny 。调试了好久,最后终于找出原因了。根据403页面的提示,需要在tomcat的配置文件中(即conf/tomcat-users.xml)加入
<role rolename="manager-gui"/>
<user username="admin" password="" roles="manager-gui"/>
有一点必须注意,其中的rolename 必须是“manager-gui” ,更改成admin 或者 admin-gui,都不好用,而下面的username password是可以任意的,但是后面的roles 必须和上面role 中的rolename相同。
通过 jconsole查看tomcat运行情况的配置方法
——基于JDK1.5、Linux(Redhat5.5)、Tomcat6
由于项目的原因,需要使用jconsole对tomcat进行远程监控,结合网上的资料对配置方法进行了总结。
第一步、配置tomcat
打开%TOMCAT_HOME%/bin下的文件catalina.sh,搜索“JAVA_OPTS”找到下面这行:
if [ -z "$LOGGING_MANAGER" ]; then
JAVA_OPTS="$JAVA_OPTS -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager"
else
JAVA_OPTS="$JAVA_OPTS $LOGGING_MANAGER"
fi
在每个“JAVA_OPTS”后边都添加以下标黄代码段,且在一行显示:
if [ -z "$LOGGING_MANAGER" ]; then JAVA_OPTS="$JAVA_OPTS -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.rmi.server.hostname=192.9.100.48 -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port="9004" -Dcom.sun.management.jmxremote.authenticate="false" -Dcom.sun.management.jmxremote.ssl="false"" else JAVA_OPTS="$JAVA_OPTS $LOGGING_MANAGER -Djava.rmi.server.hostname=192.9.100.48 -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port="9004" -Dcom.sun.management.jmxremote.authenticate="false" -Dcom.sun.management.jmxremote.ssl="false"" fi |
其中-Djava.rmi.server.hostname项必须设置,否则远程连接会因为解析到127.0.0.1失败,该项的值就是你在windows客户端连接linux时的ip地址
-Dcom.sun.management.jmxremote.port="9004"项设置远程连接端口,不要与其他应用冲突
ssl和authenticate设置为false,如果需要安全,请不要false
第二步、重启tomcat
使用root身份登录系统,进入%TOMCAT_HOME%/bin目录下:
[root@test ~]#ps –ef |grep tomcat –-输入命令查看是否存在tomcat进程 [root@test ~]#./shutdown.sh--停止tomcat服务,如果无效使用kill命令杀掉进程 [root@test ~]#./startup.sh --启动tomcat服务 |
第三步、运行jconsole
进入JDK安装目录%JDK_HOME%/bin下,找到“jconsole.exe”,点击运行并选择【远程】选项卡:
在【主机名或ip】输入要远程监控的tomcat服务器地址
在【端口】输入上文设置的端口号:9004
【用户名、口令】为空,点击【连接】进入监控界面。
- import java.lang.management.MemoryUsage;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.Formatter;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.Set;
-
- import javax.management.MBeanAttributeInfo;
- import javax.management.MBeanInfo;
- import javax.management.MBeanServerConnection;
- import javax.management.ObjectInstance;
- import javax.management.ObjectName;
- import javax.management.openmbean.CompositeDataSupport;
- import javax.management.remote.JMXConnector;
- import javax.management.remote.JMXConnectorFactory;
- import javax.management.remote.JMXServiceURL;
-
- public class test {
-
-
-
- public static void main(String[] args) {
- try {
-
- String jmxURL = "service:jmx:rmi:///jndi/rmi://192.168.1.126:10090/jmxrmi";//tomcat jmx url
- JMXServiceURL serviceURL = new JMXServiceURL(jmxURL);
-
- Map map = new HashMap();
- String[] credentials = new String[] { "monitorRole", "QED" };
- map.put("jmx.remote.credentials", credentials);
- JMXConnector connector = JMXConnectorFactory.connect(serviceURL, map);
- MBeanServerConnection mbsc = connector.getMBeanServerConnection();
-
-
- ObjectName threadObjName = new ObjectName("Catalina:type=ThreadPool,name=http-8089");
- MBeanInfo mbInfo = mbsc.getMBeanInfo(threadObjName);
-
- String attrName = "currentThreadCount";
- MBeanAttributeInfo[] mbAttributes = mbInfo.getAttributes();
- System.out.println("currentThreadCount:" + mbsc.getAttribute(threadObjName, attrName));
-
-
- for (int j = 0; j < mbsc.getDomains().length; j++) {
- System.out.println("###########" + mbsc.getDomains()[j]);
- }
- Set MBeanset = mbsc.queryMBeans(null, null);
- System.out.println("MBeanset.size() : " + MBeanset.size());
- Iterator MBeansetIterator = MBeanset.iterator();
- while (MBeansetIterator.hasNext()) {
- ObjectInstance objectInstance = (ObjectInstance) MBeansetIterator.next();
- ObjectName objectName = objectInstance.getObjectName();
- String canonicalName = objectName.getCanonicalName();
- System.out.println("canonicalName : " + canonicalName);
- if (canonicalName.equals("Catalina:host=localhost,type=Cluster")) {
-
- System.out.println("Cluster MBeans Details:");
- System.out.println("=========================================");
-
- String canonicalKeyPropList = objectName.getCanonicalKeyPropertyListString();
- }
- }
-
- ObjectName runtimeObjName = new ObjectName("java.lang:type=Runtime");
- System.out.println("厂商:" + (String) mbsc.getAttribute(runtimeObjName, "VmVendor"));
- System.out.println("程序:" + (String) mbsc.getAttribute(runtimeObjName, "VmName"));
- System.out.println("版本:" + (String) mbsc.getAttribute(runtimeObjName, "VmVersion"));
- Date starttime = new Date((Long) mbsc.getAttribute(runtimeObjName, "StartTime"));
- SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- System.out.println("启动时间:" + df.format(starttime));
-
- Long timespan = (Long) mbsc.getAttribute(runtimeObjName, "Uptime");
- System.out.println("连续工作时间:" + test.formatTimeSpan(timespan));
-
-
- ObjectName heapObjName = new ObjectName("java.lang:type=Memory");
- MemoryUsage heapMemoryUsage = MemoryUsage.from((CompositeDataSupport) mbsc.getAttribute(heapObjName,
- "HeapMemoryUsage"));
- long maxMemory = heapMemoryUsage.getMax();
- long commitMemory = heapMemoryUsage.getCommitted();
- long usedMemory = heapMemoryUsage.getUsed();
- System.out.println("heap:" + (double) usedMemory * 100 / commitMemory + "%");
-
- MemoryUsage nonheapMemoryUsage = MemoryUsage.from((CompositeDataSupport) mbsc.getAttribute(heapObjName,
- "NonHeapMemoryUsage"));
- long noncommitMemory = nonheapMemoryUsage.getCommitted();
- long nonusedMemory = heapMemoryUsage.getUsed();
- System.out.println("nonheap:" + (double) nonusedMemory * 100 / noncommitMemory + "%");
-
- ObjectName permObjName = new ObjectName("java.lang:type=MemoryPool,name=Perm Gen");
- MemoryUsage permGenUsage = MemoryUsage.from((CompositeDataSupport) mbsc.getAttribute(permObjName, "Usage"));
- long committed = permGenUsage.getCommitted();
- long used = heapMemoryUsage.getUsed();
- System.out.println("perm gen:" + (double) used * 100 / committed + "%");
-
-
- ObjectName managerObjName = new ObjectName("Catalina:type=Manager,*");
- Set<ObjectName> s = mbsc.queryNames(managerObjName, null);
- for (ObjectName obj : s) {
- System.out.println("应用名:" + obj.getKeyProperty("path"));
- ObjectName objname = new ObjectName(obj.getCanonicalName());
- System.out.println("最大会话数:" + mbsc.getAttribute(objname, "maxActiveSessions"));
- System.out.println("会话数:" + mbsc.getAttribute(objname, "activeSessions"));
- System.out.println("活动会话数:" + mbsc.getAttribute(objname, "sessionCounter"));
- }
-
-
- ObjectName threadpoolObjName = new ObjectName("Catalina:type=ThreadPool,*");
- Set<ObjectName> s2 = mbsc.queryNames(threadpoolObjName, null);
- for (ObjectName obj : s2) {
- System.out.println("端口名:" + obj.getKeyProperty("name"));
- ObjectName objname = new ObjectName(obj.getCanonicalName());
- System.out.println("最大线程数:" + mbsc.getAttribute(objname, "maxThreads"));
- System.out.println("当前线程数:" + mbsc.getAttribute(objname, "currentThreadCount"));
- System.out.println("繁忙线程数:" + mbsc.getAttribute(objname, "currentThreadsBusy"));
- }
-
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
- public static String formatTimeSpan(long span) {
- long minseconds = span % 1000;
-
- span = span / 1000;
- long seconds = span % 60;
-
- span = span / 60;
- long mins = span % 60;
-
- span = span / 60;
- long hours = span % 24;
-
- span = span / 24;
- long days = span;
- return (new Formatter()).format("%1$d天 %2$02d:%3$02d:%4$02d.%5$03d", days, hours, mins, seconds, minseconds)
- .toString();
- }
-
- }
- import java.util.Iterator;
- import java.util.Set;
- import javax.management.Attribute;
- import javax.management.MBeanInfo;
- import javax.management.MBeanServerConnection;
- import javax.management.MBeanServerInvocationHandler;
- import javax.management.ObjectInstance;
- import javax.management.ObjectName;
- import javax.management.remote.JMXConnector;
- import javax.management.remote.JMXConnectorFactory;
- import javax.management.remote.JMXServiceURL;
- public class Client {
- public static void main(String[] args) throws Exception {
- JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:9999/server");
- JMXConnector jmxc = JMXConnectorFactory.connect(url, null);
- MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
- ObjectName mbeanName = new ObjectName("chengang:name=HelloWorld");
-
- System.out.println("Domains:---------------");
- String domains[] = mbsc.getDomains();
- for (int i = 0; i < domains.length; i++) {
- System.out.println("\tDomain[" + i + "] = " + domains[i]);
- }
-
- System.out.println("MBean count = " + mbsc.getMBeanCount());
-
- mbsc.setAttribute(mbeanName, new Attribute("Name", "PANDA"));
- System.out.println("Name = " + mbsc.getAttribute(mbeanName, "Name"));
-
- HelloMBean proxy = (HelloMBean) MBeanServerInvocationHandler.newProxyInstance(mbsc, mbeanName, HelloMBean.class, false);
- proxy.printHello();
- proxy.printHello("Raymend");
-
- mbsc.invoke(mbeanName, "printHello", null, null);
- mbsc.invoke(mbeanName, "printHello", new Object[] { "熊猫烧香" }, new String[] { String.class.getName() });
-
- MBeanInfo info = mbsc.getMBeanInfo(mbeanName);
- System.out.println("Hello Class: " + info.getClassName());
- System.out.println("Hello Attriber:" + info.getAttributes()[0].getName());
- System.out.println("Hello Operation:" + info.getOperations()[0].getName());
-
- System.out.println("all ObjectName:---------------");
- Set set = mbsc.queryMBeans(null, null);
- for (Iterator it = set.iterator(); it.hasNext();) {
- ObjectInstance oi = (ObjectInstance) it.next();
- System.out.println("\t" + oi.getObjectName());
- }
-
- jmxc.close();
- }
- }
由于现在项目中用到了LinkedHashMap,并不是太熟悉就到网上搜了一下。
import
java.util.HashMap;
import
java.util.Iterator;
import
java.util.LinkedHashMap;
import
java.util.Map;
public
class TestLinkedHashMap {
public static
void main(String args[])
{
System.out.println( "*************************LinkedHashMap*************" );
Map<Integer,String> map = new LinkedHashMap<Integer,String>();
map.put( 6 , "apple" );
map.put( 3 , "banana" );
map.put( 2 , "pear" );
for (Iterator it = map.keySet().iterator();it.hasNext();)
{
Object key = it.next();
System.out.println( key+ "=" + map.get(key));
}
System.out.println( "*************************HashMap*************" );
Map<Integer,String> map1 = new HashMap<Integer,String>();
map1.put( 6 , "apple" );
map1.put( 3 , "banana" );
map1.put( 2 , "pear" );
for (Iterator it = map1.keySet().iterator();it.hasNext();)
{
Object key = it.next();
System.out.println( key+ "=" + map1.get(key));
}
}
}
|
运行结果如下:
*************************LinkedHashMap*************
6=apple
3=banana
2=pear
*************************HashMap**************************
2=pear
6=apple
3=banana
分析:LinkedHashmap 的特点是put进去的对象位置未发生变化,而HashMap会发生变化.
再普及下:
java为数据结构中的映射定义了一个接口java.util.Map;它有四个实现类,分别是HashMap
Hashtable LinkedHashMap 和TreeMap.
Map主要用于存储健值对,根据键得到值,因此不允许键重复(重复了覆盖了),但允许值重复。
Hashmap
是一个最常用的Map,它根据键的HashCode值存储数据,根据键可以直接获取它的值,具有很快的访问速度,遍历时,取得数据的顺序是完全随机的。
HashMap最多只允许一条记录的键为Null;允许多条记录的值为
Null;HashMap不支持线程的同步,即任一时刻可以有多个线程同时写HashMap;可能会导致数据的不一致。如果需要同步,可以用
Collections的synchronizedMap方法使HashMap具有同步的能力,或者使用ConcurrentHashMap。
Hashtable与
HashMap类似,它继承自Dictionary类,不同的是:它不允许记录的键或者值为空;它支持线程的同步,即任一时刻只有一个线程能写Hashtable,因此也导致了
Hashtable在写入时会比较慢。
LinkedHashMap
是HashMap的一个子类,保存了记录的插入顺序,在用Iterator遍历LinkedHashMap时,先得到的记录肯定是先插入的.也可以在构造时用带参数,按照应用次数排序。在遍历的时候会比HashMap慢,不过有种情况例外,当HashMap容量很大,实际数据较少时,遍历起来可能会比
LinkedHashMap慢,因为LinkedHashMap的遍历速度只和实际数据有关,和容量无关,而HashMap的遍历速度和他的容量有关。
TreeMap实现SortMap接口,能够把它保存的记录根据键排序,默认是按键值的升序排序,也可以指定排序的比较器,当用Iterator
遍历TreeMap时,得到的记录是排过序的。
一般情况下,我们用的最多的是HashMap,在Map 中插入、删除和定位元素,HashMap
是最好的选择。但如果您要按自然顺序或自定义顺序遍历键,那么TreeMap会更好。如果需要输出的顺序和输入的相同,那么用LinkedHashMap
可以实现,它还可以按读取顺序来排列.
HashMap是一个最常用的Map,它根据键的hashCode值存储数据,根据键可以直接获取它的值,具有很快的访问速度。HashMap最多只允许一条记录的键为NULL,允许多条记录的值为NULL。
HashMap不支持线程同步,即任一时刻可以有多个线程同时写HashMap,可能会导致数据的不一致性。如果需要同步,可以用Collections的synchronizedMap方法使HashMap具有同步的能力。
Hashtable与HashMap类似,不同的是:它不允许记录的键或者值为空;它支持线程的同步,即任一时刻只有一个线程能写Hashtable,因此也导致了Hashtable在写入时会比较慢。
LinkedHashMap保存了记录的插入顺序,在用Iterator遍历LinkedHashMap时,先得到的记录肯定是先插入的。
在遍历的时候会比HashMap慢TreeMap能够把它保存的记录根据键排序,默认是按升序排序,也可以指定排序的比较器。当用Iterator遍历TreeMap时,得到的记录是排过序的。