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的转化
(1) Calendar转化为Date
Calendar cal=Calendar.getInstance();Date date=cal.getTime();
(2) Date转化为Calendar
Date date=new Date();Calendar cal=Calendar.getInstance();cal.setTime(date);
c.计算一年中的第几星期
(1)计算某一天是一年中的第几星期
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 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协议
表示本地文件传输协议,File协议主要用于访问本地计算机中的文件 ,就如同在Windows资源管理器中打开文件一样。
应用:要使用File协议,基本的格式如下:file:///文件路径,比如要打开F:盘flash文件夹中的1.swf文件,那么可以在资源管理器或IE地址栏中键入:file:///f:/flash/1.swf并回车。
3个斜杠代表本地
7.java转义字符
\":双引号
\':单引号
\\:反斜线
8.substring(int beginIndex, int endIndex)
该子字符串从指定的 beginIndex 处开始,直到索引 endIndex - 1 处的字符。因此,该子字符串的长度为 endIndex-beginIndex。
示例:
"hamburger".substring(4, 8) returns "urge"
"smiles".substring(1, 5) returns "mile "
参数:
beginIndex - 起始索引(包括)。
endIndex - 结束索引(不包括) 。
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()忽略大小写
例如:"ABC".equals("abc")是false
"ABC".equalsIgnoreCase("abc")为ture
13.JDK1.5的新特性
“JDK1.5”(开发代号猛虎)的一个重要主题就是通过新增一些特性来简化开发 ,这些特性包括泛型,for-each循环,自动装包/拆包,枚举,可变参数, 静态导入 。使用这些特性有助于我们编写更加清晰,精悍,安全的代码。
自动装包/拆包
自动装包/拆包大大方便了基本类型数据和它们包装类地使用。
自动装包:基本类型自动转为包装类.(int >> Integer)
自动拆包:包装类自动转为基本类型.(Integer >> int)
在JDK1.5之前,我们总是对集合不能存放基本类型而耿耿于怀,现在自动转换机制解决了我们的问题。
这里Integer先自动转换为int进行加法运算,然后int再次转换为Integer.
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.
枚举类型还提供了两个有用的静态方法values()和valueOf(). 我们可以很方便地使用它们,例如
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()); //REDSystem.out.println("Color.RED.toString():"+Color.RED.toString()); //REDSystem.out.println(Color.RED.getValue()); //红色
Color.RED.name()
Color.RED.toString(); 得到的都是RED
可变参数:
当不能确定一个方法的入口参数的个数时,以往版本的Java中,通常的做法是将多个参数放在一个数组或者对象集合中作为参数来传递,1.5版本以前的写法是:
int sum(Integer[] numbers) { int nSum = 0; for(int i: numbers) nSum += i; return nSum; }
在别处调用该方法
sum(new Integer[] {12,13,20});
而在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.iteye.com/blog/743123
15.编码习惯
add、saveAdd
edit、saveEdit
16、Math.round();
Math.round(m) = (int)Math.floor(a + 0.5f)
Math.ceil(x):比x大的最小值。
Math.round(x):四舍五入。
Math.floor(x):比x小的最大值。
jdk说得很明白了,
-11.5+0.5=-11 Math.floor(-11)=-11
-11.3+0.5=-10.8 Math.floor(-10.8)= -11
-11.8+0.5=-11.3 Math.floor(-11.3)= -12
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、建立与数据库的连接。
2、向数据库发起查询请求。
3、处理数据库返回结果。
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类中定义的。
equals(Object obj)方法用来判断两个对象是否“相同”,如果“相同”则返回true,否则返回false。
hashcode()方法返回一个int数,在Object类中的默认实现是“将该对象的内部地址转换成一个整数返回”。
接下来有两个个关于这两个方法的重要规范(我只是抽取了最重要的两个,其实不止两个):
规范1: 若重写equals(Object obj)方法,有必要重写hashcode()方法,确保通过equals(Object obj)方法判断结果为true的两个对象具备相等的hashcode()返回值。说得简单点就是:“如果两个对象相同,那么他们的hashcode应该相等”。不过请注意:这个只是规范,如果你非要写一个类让equals(Object obj)返回true而hashcode()返回两个不相等的值,编译和运行都是不会报错的。不过这样违反了Java规范,程序也就埋下了BUG。
规范2: 如果equals(Object obj)返回false,即两个对象“不相同”,并不要求对这两个对象调用hashcode()方法得到两个不相同的数。说的简单点就是:“如果两个对象不相同,他们的hashcode可能相同”。
根据这两个规范,可以得到如下推论:
1、如果两个对象equals,Java运行时环境会认为他们的hashcode一定相等 。
2、如果两个对象不equals,他们的hashcode有可能 相等。
3、如果两个对象hashcode相等,他们不一定 equals。
4、如果两个对象hashcode不相等,他们一定不 equals。
http://www.iteye.com/topic/800929
22.序列化
实现序列化(保存到一个文件)的步骤
a)Make a FileOutputStream
FileOutputStream fs = new FileOutputStream("foo.ser");
b)Make a ObjectOutputStream
ObjectOutputStream os = new ObjectOutputStream(fs);
c)write the object
os.writeObject(myObject1);
o s.writeObject(myObject2);
os.writeObject(myObject3);
d) close the ObjectOutputStream
os.close();
23、计划任务:
java.util.Timer
timer.schedule(...);
http://www.blogjava.net/georgehill/archive/2005/06/09/5793.aspx
http://www.klstudio.com/post/187.html
24.Split
在java.lang包中有String.split()方法,返回是一个数组
我在应用中用到一些,给大家总结一下,仅供大家参考:
1、如果用“.”作为分隔的话,必须是如下写法:String.split("\\."),这样才能正确的分隔开,不能用String.split(".");
2、如果用“|”作为分隔的话,必须是如下写法:String.split("\\|"),这样才能正确的分隔开,不能用String.split("|");
“.”和“|”都是转义字符,必须得加"\\";
3、如果在一个字符串中有多个分隔符,可以用“|”作为连字符,比如:“acount=? and uu =? or n=?”,把三个都分隔出来,可以用String.split("and|or");
25.作用范围:
this.context.setAttribute(MyConstants.CONTINENTS_KEY, continentlist);
public static final String CONTINENTS_KEY="CONTINENTS";
jsp页面中可以直接使用 CONTINENTS(${CONTINENTS }) ,而无需context.getAttribute("");
<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
原因在于asList()方法返回的是某个数组的列表形式,返回的列表只是数组的另一个视图,而数组本身并没有消失,对列表的任何操作最终都反映在数组上. 所以不支持remove,add方法的
String[] arr = {"1", "2"};List list = Arrays.asList(arr);
27、使用ResourceBundle读取properties文件
有时候有些简单的配置文件可以没必要使用xml,其实ResourceBundle类就已经做的很好的。它甚至可以搜索到classpath里的jar文件中一些properties文件。
例如在jar文件中的根目录放置一个文件:test.properties,然后只要这个jar文件在classpath里。就可以使用这样的语句来获得一些属性:
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.gta-travel.com/HH/Images/HX/HKGA/HKGA-REG-1.jpg|http://images.gta-travel.com/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 换成 httpsFileOutputStream 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.blogjava.net/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类成员变量在没有赋值的时候会赋以默认值,其它局部变量是不能够享受这种待遇的。
联系客服