……天天向上

好的想法总是无穷无尽

统计

留言簿(1)

阅读排行榜

评论排行榜

2012年9月5日 #

calendar获取当前日期及时间的用例

  1. import Java.util.*;
  2.   public class ShowDate {
  3.   public static void main(String[] args) {
  4.   Calendar calendar = new GregorianCalendar();
  5.   Date trialTime = new Date();
  6.   calendar.setTime(trialTime);
  7.   // print out a bunch of interesting things
  8.   System.out.println("ERA: " + calendar.get(Calendar.ERA));
  9.   System.out.println("YEAR: " + calendar.get(Calendar.YEAR));
  10.   System.out.println("MONTH: " + calendar.get(Calendar.MONTH));
  11.   System.out.println("WEEK_OF_YEAR: " + calendar.get(Calendar.WEEK_OF_YEAR));
  12.   System.out.println("WEEK_OF_MONTH: " + calendar.get(Calendar.WEEK_OF_MONTH));
  13.   System.out.println("DATE: " + calendar.get(Calendar.DATE));
  14.   System.out.println("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH));
  15.   System.out.println("DAY_OF_YEAR: " + calendar.get(Calendar.DAY_OF_YEAR));
  16.   System.out.println("DAY_OF_WEEK: " + calendar.get(Calendar.DAY_OF_WEEK));
  17.   System.out.println("DAY_OF_WEEK_IN_MONTH: " + calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));
  18.   System.out.println("AM_PM: " + calendar.get(Calendar.AM_PM));
  19.   System.out.println("HOUR: " + calendar.get(Calendar.HOUR));
  20.   System.out.println("HOUR_OF_DAY: " + calendar.get(Calendar.HOUR_OF_DAY));
  21.   System.out.println("MINUTE: " + calendar.get(Calendar.MINUTE));
  22.   System.out.println("SECOND: " + calendar.get(Calendar.SECOND));
  23.   System.out.println("MILLISECOND: " + calendar.get(Calendar.MILLISECOND));
  24.   System.out.println("ZONE_OFFSET: " + (calendar.get(Calendar.ZONE_OFFSET)/(60*60*1000)));
  25.   System.out.println("DST_OFFSET: " + (calendar.get(Calendar.DST_OFFSET)/(60*60*1000)));
  26.   System.out.println("Current Time, with hour reset to 3");
  27.   calendar.clear(Calendar.HOUR_OF_DAY); // so doesn't override
  28.   calendar.set(Calendar.HOUR, 3);
  29.   System.out.println("ERA: " + calendar.get(Calendar.ERA));
  30.   System.out.println("YEAR: " + calendar.get(Calendar.YEAR));
  31.   System.out.println("MONTH: " + calendar.get(Calendar.MONTH));
  32.   System.out.println("WEEK_OF_YEAR: " + calendar.get(Calendar.WEEK_OF_YEAR));
  33.   System.out.println("WEEK_OF_MONTH: " + calendar.get(Calendar.WEEK_OF_MONTH));
  34.   System.out.println("DATE: " + calendar.get(Calendar.DATE));
  35.   System.out.println("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH));
  36.   System.out.println("DAY_OF_YEAR: " + calendar.get(Calendar.DAY_OF_YEAR));
  37.   System.out.println("DAY_OF_WEEK: " + calendar.get(Calendar.DAY_OF_WEEK));
  38.   System.out.println("DAY_OF_WEEK_IN_MONTH: " + calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));

posted @ 2012-09-19 09:51 japper 阅读(6339) | 评论 (0)编辑 收藏

Android SDK下载和更新失败的解决方法


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来编辑。

posted @ 2012-09-10 11:18 japper 阅读(339) | 评论 (0)编辑 收藏

免安装版Tomcat 6.0.35碰到的问题 :打开tomcat管理界面时,用户名和密码错误的设置方法

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相同。

posted @ 2012-09-05 14:22 japper 阅读(3825) | 评论 (0)编辑 收藏

通过 jconsole查看tomcat运行情况的配置方法

通过 jconsole查看tomcat运行情况的配置方法

——基于JDK1.5LinuxRedhat5.5)、Tomcat6

由于项目的原因,需要使用jconsoletomcat进行远程监控,结合网上的资料对配置方法进行了总结。

第一步、配置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"项设置远程连接端口,不要与其他应用冲突

sslauthenticate设置为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

【用户名、口令】为空,点击【连接】进入监控界面。

posted @ 2012-09-05 14:16 japper 阅读(3943) | 评论 (0)编辑 收藏

TOMCAT获取信息:JMXServiceURL 、JMXConnector 和MBeanServerConnection

  1. import java.lang.management.MemoryUsage;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Date;
  4. import java.util.Formatter;
  5. import java.util.HashMap;
  6. import java.util.Iterator;
  7. import java.util.Map;
  8. import java.util.Set;
  9. import javax.management.MBeanAttributeInfo;
  10. import javax.management.MBeanInfo;
  11. import javax.management.MBeanServerConnection;
  12. import javax.management.ObjectInstance;
  13. import javax.management.ObjectName;
  14. import javax.management.openmbean.CompositeDataSupport;
  15. import javax.management.remote.JMXConnector;
  16. import javax.management.remote.JMXConnectorFactory;
  17. import javax.management.remote.JMXServiceURL;
  18. public class test {
  19. /**
  20. * @param args
  21. */
  22. public static void main(String[] args) {
  23. try {
  24. String jmxURL = "service:jmx:rmi:///jndi/rmi://192.168.1.126:10090/jmxrmi";//tomcat jmx url
  25. JMXServiceURL serviceURL = new JMXServiceURL(jmxURL);
  26. Map map = new HashMap();
  27. String[] credentials = new String[] { "monitorRole", "QED" };
  28. map.put("jmx.remote.credentials", credentials);
  29. JMXConnector connector = JMXConnectorFactory.connect(serviceURL, map);
  30. MBeanServerConnection mbsc = connector.getMBeanServerConnection();
  31. //端口最好是动态取得
  32. ObjectName threadObjName = new ObjectName("Catalina:type=ThreadPool,name=http-8089");
  33. MBeanInfo mbInfo = mbsc.getMBeanInfo(threadObjName);
  34. String attrName = "currentThreadCount";//tomcat的线程数对应的属性值
  35. MBeanAttributeInfo[] mbAttributes = mbInfo.getAttributes();
  36. System.out.println("currentThreadCount:" + mbsc.getAttribute(threadObjName, attrName));
  37. //heap
  38. for (int j = 0; j < mbsc.getDomains().length; j++) {
  39. System.out.println("###########" + mbsc.getDomains()[j]);
  40. }
  41. Set MBeanset = mbsc.queryMBeans(null, null);
  42. System.out.println("MBeanset.size() : " + MBeanset.size());
  43. Iterator MBeansetIterator = MBeanset.iterator();
  44. while (MBeansetIterator.hasNext()) {
  45. ObjectInstance objectInstance = (ObjectInstance) MBeansetIterator.next();
  46. ObjectName objectName = objectInstance.getObjectName();
  47. String canonicalName = objectName.getCanonicalName();
  48. System.out.println("canonicalName : " + canonicalName);
  49. if (canonicalName.equals("Catalina:host=localhost,type=Cluster")) {
  50. // Get details of cluster MBeans
  51. System.out.println("Cluster MBeans Details:");
  52. System.out.println("=========================================");
  53. //getMBeansDetails(canonicalName);
  54. String canonicalKeyPropList = objectName.getCanonicalKeyPropertyListString();
  55. }
  56. }
  57. //------------------------- system ----------------------
  58. ObjectName runtimeObjName = new ObjectName("java.lang:type=Runtime");
  59. System.out.println("厂商:" + (String) mbsc.getAttribute(runtimeObjName, "VmVendor"));
  60. System.out.println("程序:" + (String) mbsc.getAttribute(runtimeObjName, "VmName"));
  61. System.out.println("版本:" + (String) mbsc.getAttribute(runtimeObjName, "VmVersion"));
  62. Date starttime = new Date((Long) mbsc.getAttribute(runtimeObjName, "StartTime"));
  63. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  64. System.out.println("启动时间:" + df.format(starttime));
  65. Long timespan = (Long) mbsc.getAttribute(runtimeObjName, "Uptime");
  66. System.out.println("连续工作时间:" + test.formatTimeSpan(timespan));
  67. //------------------------ JVM -------------------------
  68. //堆使用率
  69. ObjectName heapObjName = new ObjectName("java.lang:type=Memory");
  70. MemoryUsage heapMemoryUsage = MemoryUsage.from((CompositeDataSupport) mbsc.getAttribute(heapObjName,
  71. "HeapMemoryUsage"));
  72. long maxMemory = heapMemoryUsage.getMax();//堆最大
  73. long commitMemory = heapMemoryUsage.getCommitted();//堆当前分配
  74. long usedMemory = heapMemoryUsage.getUsed();
  75. System.out.println("heap:" + (double) usedMemory * 100 / commitMemory + "%");//堆使用率
  76. MemoryUsage nonheapMemoryUsage = MemoryUsage.from((CompositeDataSupport) mbsc.getAttribute(heapObjName,
  77. "NonHeapMemoryUsage"));
  78. long noncommitMemory = nonheapMemoryUsage.getCommitted();
  79. long nonusedMemory = heapMemoryUsage.getUsed();
  80. System.out.println("nonheap:" + (double) nonusedMemory * 100 / noncommitMemory + "%");
  81. ObjectName permObjName = new ObjectName("java.lang:type=MemoryPool,name=Perm Gen");
  82. MemoryUsage permGenUsage = MemoryUsage.from((CompositeDataSupport) mbsc.getAttribute(permObjName, "Usage"));
  83. long committed = permGenUsage.getCommitted();//持久堆大小
  84. long used = heapMemoryUsage.getUsed();//
  85. System.out.println("perm gen:" + (double) used * 100 / committed + "%");//持久堆使用率
  86. //-------------------- Session ---------------
  87. ObjectName managerObjName = new ObjectName("Catalina:type=Manager,*");
  88. Set<ObjectName> s = mbsc.queryNames(managerObjName, null);
  89. for (ObjectName obj : s) {
  90. System.out.println("应用名:" + obj.getKeyProperty("path"));
  91. ObjectName objname = new ObjectName(obj.getCanonicalName());
  92. System.out.println("最大会话数:" + mbsc.getAttribute(objname, "maxActiveSessions"));
  93. System.out.println("会话数:" + mbsc.getAttribute(objname, "activeSessions"));
  94. System.out.println("活动会话数:" + mbsc.getAttribute(objname, "sessionCounter"));
  95. }
  96. //----------------- Thread Pool ----------------
  97. ObjectName threadpoolObjName = new ObjectName("Catalina:type=ThreadPool,*");
  98. Set<ObjectName> s2 = mbsc.queryNames(threadpoolObjName, null);
  99. for (ObjectName obj : s2) {
  100. System.out.println("端口名:" + obj.getKeyProperty("name"));
  101. ObjectName objname = new ObjectName(obj.getCanonicalName());
  102. System.out.println("最大线程数:" + mbsc.getAttribute(objname, "maxThreads"));
  103. System.out.println("当前线程数:" + mbsc.getAttribute(objname, "currentThreadCount"));
  104. System.out.println("繁忙线程数:" + mbsc.getAttribute(objname, "currentThreadsBusy"));
  105. }
  106. } catch (Exception e) {
  107. e.printStackTrace();
  108. }
  109. }
  110. public static String formatTimeSpan(long span) {
  111. long minseconds = span % 1000;
  112. span = span / 1000;
  113. long seconds = span % 60;
  114. span = span / 60;
  115. long mins = span % 60;
  116. span = span / 60;
  117. long hours = span % 24;
  118. span = span / 24;
  119. long days = span;
  120. return (new Formatter()).format("%1$d天 %2$02d:%3$02d:%4$02d.%5$03d", days, hours, mins, seconds, minseconds)
  121. .toString();
  122. }
  123. }

  1. import java.util.Iterator;
  2. import java.util.Set;
  3. import javax.management.Attribute;
  4. import javax.management.MBeanInfo;
  5. import javax.management.MBeanServerConnection;
  6. import javax.management.MBeanServerInvocationHandler;
  7. import javax.management.ObjectInstance;
  8. import javax.management.ObjectName;
  9. import javax.management.remote.JMXConnector;
  10. import javax.management.remote.JMXConnectorFactory;
  11. import javax.management.remote.JMXServiceURL;
  12. public class Client {
  13. public static void main(String[] args) throws Exception {
  14. JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:9999/server");
  15. JMXConnector jmxc = JMXConnectorFactory.connect(url, null);
  16. MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
  17. ObjectName mbeanName = new ObjectName("chengang:name=HelloWorld");
  18. // 把所有Domain都打印出来
  19. System.out.println("Domains:---------------");
  20. String domains[] = mbsc.getDomains();
  21. for (int i = 0; i < domains.length; i++) {
  22. System.out.println("\tDomain[" + i + "] = " + domains[i]);
  23. }
  24. // MBean的总数
  25. System.out.println("MBean count = " + mbsc.getMBeanCount());
  26. // 对name属性的操作(属性名的第一个字母要大写)
  27. mbsc.setAttribute(mbeanName, new Attribute("Name", "PANDA"));// 设值
  28. System.out.println("Name = " + mbsc.getAttribute(mbeanName, "Name"));// 取值
  29. // 得到proxy代理后直接调用的方式
  30. HelloMBean proxy = (HelloMBean) MBeanServerInvocationHandler.newProxyInstance(mbsc, mbeanName, HelloMBean.class, false);
  31. proxy.printHello();
  32. proxy.printHello("Raymend");
  33. // 远程调用的方式
  34. mbsc.invoke(mbeanName, "printHello", null, null);
  35. mbsc.invoke(mbeanName, "printHello", new Object[] { "熊猫烧香" }, new String[] { String.class.getName() });
  36. // 得mbean的信息
  37. MBeanInfo info = mbsc.getMBeanInfo(mbeanName);
  38. System.out.println("Hello Class: " + info.getClassName());
  39. System.out.println("Hello Attriber:" + info.getAttributes()[0].getName());
  40. System.out.println("Hello Operation:" + info.getOperations()[0].getName());
  41. // 得到所有的MBean的ObjectName
  42. System.out.println("all ObjectName:---------------");
  43. Set set = mbsc.queryMBeans(null, null);
  44. for (Iterator it = set.iterator(); it.hasNext();) {
  45. ObjectInstance oi = (ObjectInstance) it.next();
  46. System.out.println("\t" + oi.getObjectName());
  47. }
  48. // 关闭MBeanServer连接
  49. jmxc.close();
  50. }
  51. }

posted @ 2012-09-05 14:14 japper 阅读(4983) | 评论 (0)编辑 收藏

LinkedHashMap和HashMap的比较使用

由于现在项目中用到了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时,得到的记录是排过序的。

posted @ 2012-09-05 14:12 japper 阅读(16454) | 评论 (5)编辑 收藏