2.保留2位小数:
import java.text.DecimalFormat; DecimalFormat df=new DecimalFormat("0.00"); Double x = 83.3333333333; x=Double.parseDouble(df.format(x)); 3.group by 和 order by
ORDER BY 用于对数据按指定的列和方法排序。 select * from syscolumns order by id asc, colorder desc; 指示查询出的结果 按 id 正序排列, colOrder 逆序排列。 GROUP BY 用于汇总统计。 HAVING 用途和 WHERE类似,但用于对 GROUP BY 的结果进行过滤。 select id, count(id) from syscolumns group by id; 这条语句以 id 进行汇总,统计出每个不同的 id 出现的个数。 select id, count(id) from syscolumns group by id having count(1) > 10; 这条语句以 id 进行汇总,统计出每个不同的 id 出现的个数,但 having 后的条件指定了只显示 count(id) 大于 10 的数据。。 先Group by ,后 Order by 4.日期 获取当前时间:
1. SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd" + " " + "hh:mm:ss"); String datetime = tempDate.format(new java.util.Date()); 2. Calendar now=Calendar.getInstance(); String time=now.get(Calendar.YEAR)+"-"+(now.get(Calendar.MONTH)+1)+"-"+now.get(Calendar.DAY_OF_MONTH)+" "+now.get(Calendar.HOUR_OF_DAY)+":"+now.get(Calendar.MINUTE)+":"+now.get(Calendar.SECOND); 3.Date curDate= new Date(System.currentTimeMillis());
a.获取年、月、日:
String year=String.valueOf(c.get(Calendar.YEAR)); String month=String.valueOf(c.get(Calendar.MONTH)+1); String day=String.valueOf(c.get(Calendar.DAY_OF_MONTH));
b.Calendar和Date的转化
Calendar cal=Calendar.getInstance(); Date date=cal.getTime();
Date date=new Date(); Calendar cal=Calendar.getInstance(); cal.setTime(date);
c.计算一年中的第几星期
Calendar cal=Calendar.getInstance(); cal.set(Calendar.YEAR, 2006); cal.set(Calendar.MONTH,1); cal.set(Calendar.DAY_OF_MONTH, 3); int weekno=cal.get(Calendar.WEEK_OF_YEAR);
(2)计算一年中的第几星期是几号
SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd"); Calendar cal=Calendar.getInstance(); cal.set(Calendar.YEAR, 2006); cal.set(Calendar.WEEK_OF_YEAR, 1); cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); System.out.println(df.format(cal.getTime())); 输出: 2006-01-02 d:
1天内(注意为add ):
Calendar c=Calendar.getInstance(); c.add(Calendar.DAY_OF_MONTH, -1); //得到前一天的时间 startDateStr=sf.format(c.getTime()); endDateStr=sf.format(java.util.Calendar.getInstance().getTime()); 推荐使用这种方法获取当前时间,不推荐使用new Date() 3天内:
Calendar c=Calendar.getInstance(); c.add(Calendar.DAY_OF_MONTH, -3); //得到前3天的时间 startDateStr=sf.format(c.getTime()); 近1月:
Calendar c=Calendar.getInstance(); c.add(Calendar.MONTH, -1); startDateStr=sf.format(c.getTime()); 本月(注意为set ):
Calendar c=Calendar.getInstance(); c.set(Calendar.DATE,1); //把日期设为当月第一天 startDateStr=sf.format(c.getTime()); 上月:
Calendar c2=Calendar.getInstance(); c2.add(Calendar.MONTH,-1); c2.set(Calendar.DATE,1); startDateStr=sf.format(c2.getTime());
5.文本域input的不可编辑属性 disabled 和 readonly 区别 都可以实现都可以实现input的不可编辑,但有区别...
disable 属性 -- 表示已经失效的输入域 readonly 属性 -- 表示只读(只能看到,不能修改)的输入域(框)
<input type="checkbox" value="2" name="fav" disabled="disabled" /> <input type="text" name="notinput" readonly="readonly" />
/**是否闰年 */ public boolean isLeapYear(int year) { return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); } 具体日期操作: http://www.360doc.com/content/09/0820/17/236102_5085810.shtml 可将选中的代码拖动到QQ聊天窗口中复制 6.File协议 3个斜杠代表本地
7.java转义字符 \":双引号 8.substring(int beginIndex, int endIndex) 该子字符串从指定的 beginIndex 处开始,直到索引 endIndex - 1 处的字符。因此,该子字符串的长度为 endIndex-beginIndex。 9.什么时候用Thread.getContextClassLoader() 需要动态加载很多类和资源的时候 . 通常当你需要动态加载资源的时候 , 你至少有三个 ClassLoader 可以选择 : ² 系统类加载器或叫作应用类加载器 (system classloader or application classloader) ² 当前类加载器 ² 当前线程类加载器
10.instanceof 运算符
String s = "I AM an Object!"; boolean isObject = s instanceof Object; public double calculate(Bill bill) { if (bill instanceof PhoneBill) { //计算电话账单 } } 11、java环境配置: CLASSPATH .;%JAVA_HOME%\lib JAVA_HOME C:\Program Files\Java\jdk1.6.0_22 Path .;%JAVA_HOME%\bin
12.String.equals()对大小写敏感,而String.equalsIgnoreCase()忽略大小写
13.JDK1.5的新特性 “JDK1.5”(开发代号猛虎)的一个重要主题就是通过新增一些特性来简化开发 ,这些特性包括泛型,for-each循环,自动装包/拆包,枚举,可变参数, 静态导入 。使用这些特性有助于我们编写更加清晰,精悍,安全的代码。 自动装包/拆包 自动装包/拆包大大方便了基本类型数据和它们包装类地使用。
int a = 3; Collection c = new ArrayList(); c.add(a);//自动转换成Integer. Integer b = new Integer(2); c.add(b + 2); 枚举:
public enum Color { Red, White, Blue } 然后可以这样来使用Color myColor = Color.Red.
for (Color c : Color.values()) System.out.println(c); 带构造函数的枚举:
public enum Color { RED("红色"),BLUE("蓝色"),GREEN("绿色"); private final String value; public String getValue() { return value; } private Color(String name){ this.value=name; } }
System.out.println("Color.RED.name():"+Color.RED.name()); //RED System.out.println("Color.RED.toString():"+Color.RED.toString()); //RED System.out.println(Color.RED.getValue()); //红色 Color.RED.name() Color.RED.toString(); 得到的都是RED 1、Color枚举类是特殊的class,其枚举值(RED,BLUE...)是Color的类对象(类实例):Color c=Color.RED ;
而且这些枚举值都是public static final的,也就是我们经常所定义的常量,因此枚举类中的枚举值最好全部大写 。 2、即然枚举类是class,当然在枚举类型中有构造器,方法和数据域。但是,枚举类的构造器有很大的不同: (1) 构造函数只是在构造枚举值的时候被调用。 (2) 枚举构造函数只能私有private ,绝对不允许有public构造器。这样可以保证外部代码无法新构造枚举类的实例。因为我们知道枚举值是public static final的常量而已 但枚举类的方法和数据域可以允许外部访问。
可变参数: 当不能确定一个方法的入口参数的个数时,以往版本的Java中,通常的做法是将多个参数放在一个数组或者对象集合中作为参数来传递,1.5版本以前的写法是:
int sum(Integer[] numbers) { int nSum = 0; for(int i: numbers) nSum += i; return nSum; } 在别处调用该方法 而在1.5版本中可以写为:
public static int sum(Integer... number){ int nSum=0; for(int i : number){ nSum+=i; } return nSum; } 在别处调用该方法
System.out.println("sum():"+sum(12,13,14)); System.out.println("sum():"+sum(12,13)); 静态导入: 要使用静态成员(方法和变量)我们必须给出提供这个方法的类。使用静态导入可以使被导入类的所有静态变量和静态方法在当前类直接可见,使用这些静态成员无需再给出他们的类名。
import static java.lang.Math.*; ……. r = sin(PI * 2); //无需再写r = Math.sin(Math.PI);
14.o1.CompareTo(o2); 整形间比较会返回1、-1、0 如:
public static void main(String[] args) { //Integer num1; //num1 = new Integer("24"); //Integer num2; //num2 = new Integer("34"); Integer num2=12; //不可使用 int num2=12;因compareTo(T t)中的T是类,所以必须用包装类 Integer num1=11; System.out.println(num2.compareTo(num1)); //1 System.out.println("------------------"); System.out.println(num1.compareTo(num2)); //-1 } 字符串间比较:
"abcd" .compareTo( "adef" )== -2 "abc" .compareTo( "abcdef" )== -3 "abc" .compareTo( "abc" ) == 0
//需要进行排序的字符串 String[] array = new String[] { "lilei", "libai", "james", "poly", "wobfei" }; //使用简单的循环排序 for (int i = 0; i < array.length ; i++) { for (int j = i + 1; j < array.length-1; j++) { if (array[i].compareTo(array[j]) > 0) { String temp = array[i]; array[i] = array[j]; array[j] = temp; } } } 使用上面针对 String 的排序以后,字符串的内容将会是: james libai lilei poly wobfei 该例子来源(稍作了修改):http://wobfei./blog/743123
15.编码习惯 add、saveAdd edit、saveEdit
16、Math.round();
Math.round(m) = (int)Math.floor(a + 0.5f) Math.ceil(x):比x大的最小值。 jdk说得很明白了,
17、重写 关于重写,遵循以下的规则: (1)重写方法必须和被重写方法具有相同的参数列表, 返回类型必须和被重写方法的返回类型相同或者是返回类型的子类型 。 (2)重写方法的访问控制修饰符不能比被重写方法更严格(比如一个在父类中声明为public的方法重写成一个protected的方法)。
18.
class Test { void test(int i) { System.out.println("I am an int." + i); } void test(String s) { System.out.println("I am a string."); } public static void main(String args[]) { Test t = new Test(); char ch = 'y'; t.test(ch); } //结果为:I am an int.121 }
19.JDBC的主要任务是什么?(三个) 1、建立与数据库的连接。
20.TreeSet的构造函数 TreeSet(); TreeSet(Collection c);//构造一个包含指定 collection 元素的新 TreeSet,它按照其元素的自然顺序 进行排序。 TreeSet(Comparator comparator);//构造一个新的空 TreeSet,它根据指定比较器进行排序。
public class NameComparator implements Comparator<Name>{ public int compare(Name n1,Name n2){ if(n1.getName().compareTo(n2.getName())>0) return -1; if(n1.getName().compareTo(n2.getName())<0) return 1; return 0; } public static void main(String[] args) { Set<Name> set = new TreeSet<Name>(new NameComparator()); Name n1 = new Name("ray"); Name n2 = new Name("tom"); Name n3 = new Name("jame"); set.add(n1); set.add(n2); set.add(n3); Iterator it = set.iterator(); while(it.hasNext()){ Name s = (Name)it.next(); System.out.print(s.getName()+","); } System.out.println("一共有对象:"+set.size()+"个"); } }//打印结果是:tom,ray,jame,一共有对象:3个 客户端排序时因为java.util.Comparator<Type>接口提供了具体的排序方式,<Type>指定了被比较对象的类型,Comparator有个compare(Type x,Type y)的方法,用于比较两个对象的大小。
21.Object中的hashcode()和equals() 在Java中任何一个对象都具备equals(Object obj)和hashcode()这两个方法,因为他们是在Object类中定义的。
22.序列化 序列化的对象要实现Serializable接口,
Serializable接口没有需要实现的方法, implements Serializable只是为了标注该对象是可被序列化的, 然后 使用一个输出流(如:FileOutputStream)来构造一个ObjectOutputStream(对象流)对象 ,接着,使用ObjectOutputStream对象的writeObject(Object obj)方法就可以将参数为obj的对象写出(即保存其状态),要恢复的话则用输入流。 实现序列化(保存到一个文件)的步骤
23、计划任务: java.util.Timer timer.schedule(...); http://www./georgehill/archive/2005/06/09/5793.aspx
24.Split 在java.lang包中有String.split()方法,返回是一个数组
25.作用范围: this.context.setAttribute(MyConstants.CONTINENTS_KEY, continentlist);
<body> <% request.setAttribute("name","xiaoming") ;%> <input type="text" value="${name}"/> </body> 26.数组转为list
List stooges = Arrays.asList("Larry", "Moe", "Curly"); 此时stooges中有有三个元素。注意:此时的list不能进行add操作,否则会报 “java.lang.UnsupportedOperationException”,Arrays.asList()返回的是List,而且是一个定长的List,所以不能转换为ArrayList,只能转换为AbstractList
String[] arr = {"1", "2"}; List list = Arrays.asList(arr); 27、使用ResourceBundle读取properties文件 有时候有些简单的配置文件可以没必要使用xml,其实ResourceBundle类就已经做的很好的。它甚至可以搜索到classpath里的jar文件中一些properties文件。
ResourceBundle rb = ResourceBundle.getBundle("test"); String s = rb.getString("FirstKey"); System.out.println(s); 28、HttpClient学习
public class TestHttpClient { public static void main(String[] args) { String url = "http://images./HH/Images/HX/HKGA/HKGA-REG-1.jpg|http://images./HH/Images/HX/HKGA/HKGA-REG-2.jpg"; String[] urlArr = url.split("\\|"); String fileName = "D:\\httpClient"; HttpClient client = new HttpClient(); GetMethod method = new GetMethod(urlArr[1]); // 使用 GET 方法 ,如果服务器需要通过 HTTPS 连接,那只需要将下面 URL 中的 http 换成 https FileOutputStream output = null; try { client.executeMethod(method); File showFile = new File(fileName + ".jpg"); output = new FileOutputStream(showFile); output.write(method.getResponseBody()); //method.getResponseBody()为byte[]类型 output.flush(); } catch (HttpException e) { } catch (IOException e) { } finally { if (output != null) { try { output.close(); output = null; } catch (IOException e) { } } } } } http://www./Alpha/archive/2007/01/22/95216.html
抽象类中可以有抽象方法和非抽象方法 !
构造方法可以被重载,但不可以被重写
数组没有 length() 这个方法,有 length 的属性。 String 有有 length() 这个方法。
Scanner scanner = new Scanner(System.in); String temp = scanner.nextLine(); System.out.println("temp=="+temp); // 输入。。。
new String(name.getBytes("iso-8859-1"),"utf-8");
其他: java类成员变量在没有赋值的时候会赋以默认值,其它局部变量是不能够享受这种待遇的。 |
|