WEB开发网
开发学院WEB开发Jsp java初学者必读-经验总结(3) 阅读

java初学者必读-经验总结(3)

 2008-01-05 09:08:36 来源:WEB开发网   
核心提示:java初学者必读-经验总结(3)11:删除文件夹下的所有目录: /* * 删除一个目录下的所有文件 */ public static void delAllFile(String path) { File file = new File(path); if(!file.exists()) return; if(!fi

  java初学者必读-经验总结(3)

11:删除文件夹下的所有目录:
/*
* 删除一个目录下的所有文件
*/
public static void delAllFile(String path) {
File file = new File(path);
if(!file.exists()) return;
if(!file.isDirectory()) return;
String[] tempList = file.list();
File temp = null;
for(int i = 0; i < tempList.length; i++) {
if(path.endsWith(File.separator))
temp = new File(path + tempList[i]);
else
temp = new File(path + File.separator + tempList[i]);
if(temp.isFile()) temp.delete();
if(temp.isDirectory()) delAllFile(path + tempList[i]);
}
}
12:字符串转换成时间及时间相减:
1:) SimpleDateFormat formatter = new SimpleDateFormat ("yyyy.MM.dd");
//假定像2002.07.04的是合法日期其他都非法。
String str="2002.07.04";
ParsePosition pos = new ParsePosition(0);
Date dt=formatter.parse(str,pos);
if(dt!=null)
{
//是合法日期
}
else
{
//非法日期
}
2:)
两个日期相减
import java.util.*;
import java.text.*;
class a
{
public static void main(String[] args)
{
String s1 = "2003/08/15 17:15:30";
String s2 = "2002/09/14 14:18:37";
try{
SimpleDateFormat formatter = new SimpleDateFormat ("yyyy/MM/dd HH:mm:ss");

ParsePosition pos = new ParsePosition(0);
ParsePosition pos1 = new ParsePosition(0);
Date dt1=formatter.parse(s1,pos);
Date dt2=formatter.parse(s2,pos1);
System.out.PRintln("dt1="+dt1);
System.out.println("dt2="+dt2);
long l = dt1.getTime() - dt2.getTime();

System.out.println("Hello World!="+l);
}catch(Exception e){
System.out.println("exception"+e.toString());
}

}
}


3:)得到2个月后的日期:
import java.util.*;
import java.text.DateFormat;
public class test2
{
public static void main(String args[]) throws Exception
{
String date="2001/11/30";

DateFormat dateFormat =
DateFormat.getDateInstance(DateFormat.MEDIUM);
GregorianCalendar grc=new GregorianCalendar();
grc.setTime(new Date(date));


grc.add(GregorianCalendar.MONTH,2);
System.out.println("grc="+dateFormat.format(grc.getTime()));
}
}

13:jsp/servet重定向问题:
不改变url的:
1:)servlet:

Tags:java 初学者 必读

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接