1、java的日期添加:
import java.util.Date ;date=new date();//取時間 Calendar calendar = new GregorianCalendar(); calendar.setTime(date); calendar.add(calendar.DATE,1);//把日期往后增加一天.整數往后推,負數往前移動 date=calendar.getTime(); //這個時間就是日期往后推一天的結果
2、String轉Date或Date轉String:
這種轉換要用到java.text.SimpleDateFormat類字符串轉換成日期類型:方法1:也是最簡單的方法 Date date=new Date("2008-04-14");方法2:SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");//小寫的mm表示的是分鐘String dstr="2008-4-24";java.util.Date date=sdf.parse(dstr);日期轉換成字符串:SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");java.util.Date date=new java.util.Date();String str=sdf.format(date);
3、設置session的過期時間:
1.在web.xml中的session-config配置session-timeout元素(WEB.XML文件中的元素)用來指定默認的會話超時時間間隔,以分鐘為單位。該元素值必須為整數。如果 session-timeout元素的值為零或負數,則表示會話將永遠不會超時。如:
//30分鐘<session-config> <session-timeout>30</session-timeout></session-config>
2.在程序中手動設置
java 代碼:session.setMaxInactiveInterval(30 * 60);//30分鐘
4、Java中的轉義字符問題:
s = URLDecoder.decode(s, "UTF-8");
5、JAVA中常用String類型轉換:
String a="1467000000";double aa=Double.parseDouble(a);//String轉DoubleString str = "123";int i=Integer.parseInt(str);//String轉IntInteger integer=Integer.valueOf(str);//Integer轉StringString s = String.valueOf(i);//Int轉StringString s = Integer.toString(integer);//Integer轉StringString s = "" + i;//Int轉String
6、日期格式數據處理:
//-----------------日期-------------------------Calendar calendar=Calendar.getInstance();int year=calendar.get(Calendar.YEAR);int month=calendar.get(Calendar.MONTH)+1;int day=calendar.get(Calendar.DATE);//獲取今天的日期字符串String today=java.text.DateFormat.getDateInstance().format(new java.util.Date());//獲取今天的日期new java.sql.Date(System.currentTimeMillis())
7、計算兩個日期變量之間的差值:
//計算兩個Date變量之間的差值Date date = new Date();try { new Thread().sleep(3000);} catch (InterruptedException e) { e.PRintStackTrace();}System.out.println((new Date().getTime() - date.getTime())/1000);//轉化為秒
8、JAVA中的字符串截取:
System.out.println("0123456789".substring(0,2));//結果:01
substring(arg1, arg2);arg1:截取字符起始位置,arg2:一共截取幾個字符;需要注意的是,字符串起始位置從0開始計算
9、JAVA中String對象,大小寫轉化:
String test="ABC34cccddee";System.out.println(test.toUpperCase());//小寫轉大寫String test="ABC34cccddee";System.out.println(test.toLowerCase());//小寫轉大寫
新聞熱點
疑難解答