??xml version="1.0" encoding="utf-8" standalone="yes"?>久久97精品,国产精品91xxx,91国语精品自产拍http://www.aygfsteel.com/Collus/惜秦怀?/description>zh-cnWed, 02 Jul 2025 14:58:16 GMTWed, 02 Jul 2025 14:58:16 GMT60动态跟tJava代码的执行状况工?-BTracehttp://www.aygfsteel.com/Collus/articles/320789.html良帅良帅Thu, 13 May 2010 05:00:00 GMThttp://www.aygfsteel.com/Collus/articles/320789.htmlhttp://www.aygfsteel.com/Collus/comments/320789.htmlhttp://www.aygfsteel.com/Collus/articles/320789.html#Feedback0http://www.aygfsteel.com/Collus/comments/commentRss/320789.htmlhttp://www.aygfsteel.com/Collus/services/trackbacks/320789.htmlhttp://kenai.com/projects/btrace/pages/UserGuide?br /> 对于q行中的JavaE序Q尤其是Z问题的程序,会需要跟t其执行状况Q例如传入的参数是什么、执行了多少旉Q返回的对象是什么,抛出了什么异常,? l的做法只能是把E序改一遍,加上一堆logQ一个例子来展示下用BTrace的情况下Q怎么来跟t一个方法的执行旉Q?br />
@BTrace public class MethodResponseTime {
    
    @TLS 
private static long startTime;
    
    @OnMethod(clazz
="cd",method="Ҏ?/span>")
    
public static void onCall(){
        println(
"enter this method");
        startTime
=timeMillis();
    }
    
    @OnMethod(clazz
="cd",method="Ҏ?/span>",location=@Location(Kind.RETURN))
    
public static void onReturn(){
        println(
"method end!");
        println(strcat(
"Time taken ms",str(timeMillis()-startTime)));
    }
    
}
用btrace执行上面的代码,可以动态的监控L的目前运行的JavaE序中某cȝ某方法的执行旉Q执行上面代码的方式如下Qjdk 6+Q:
btrace [pid] MethodResponseTime.class

q有例如获取调用参数、调用者的对象实例以及q回值等请参看User Guide?br />
btraceZ保持JVMq行的安全性,因此做了很多的限Ӟ例如不能抛出异常、修改传入的参数的倹{修改返回值等Q基本是一个只ȝ动态分析代码运? 状况的工P但仍然是非常的有用,其实现机制是attach api + asm +  instrumentation?img src ="http://www.aygfsteel.com/Collus/aggbug/320789.html" width = "1" height = "1" />

良帅 2010-05-13 13:00 发表评论
]]>
StingUtilhttp://www.aygfsteel.com/Collus/articles/320748.html良帅良帅Wed, 12 May 2010 14:04:00 GMThttp://www.aygfsteel.com/Collus/articles/320748.htmlhttp://www.aygfsteel.com/Collus/comments/320748.htmlhttp://www.aygfsteel.com/Collus/articles/320748.html#Feedback0http://www.aygfsteel.com/Collus/comments/commentRss/320748.htmlhttp://www.aygfsteel.com/Collus/services/trackbacks/320748.html import  java.io.IOException; import  java.io...  阅读全文

良帅 2010-05-12 22:04 发表评论
]]>
FileUtilhttp://www.aygfsteel.com/Collus/articles/320747.html良帅良帅Wed, 12 May 2010 14:03:00 GMThttp://www.aygfsteel.com/Collus/articles/320747.htmlhttp://www.aygfsteel.com/Collus/comments/320747.htmlhttp://www.aygfsteel.com/Collus/articles/320747.html#Feedback0http://www.aygfsteel.com/Collus/comments/commentRss/320747.htmlhttp://www.aygfsteel.com/Collus/services/trackbacks/320747.html JAVA FileUtil

import  java.io. * ;

/**
* FileUtil. Simple file operation class.
*
@author  BeanSoft
*
*/
public   class  FileUtil {
    
/**
     * The buffer.
     
*/
    
protected   static   byte  buf[]  =   new   byte [ 1024 ];

    
/**
     * Read content from local file. FIXME How to judge UTF-8 and GBK, the
     * correct code should be: FileReader fr = new FileReader(new
     * InputStreamReader(fileName, "ENCODING")); Might let the user select the
     * encoding would be a better idea. While reading UTF-8 files, the content
     * is bad when saved out.
     *
     * 
@param  fileName -
     *            local file name to read
     * 
@return
     * 
@throws  Exception
     
*/
    
public   static  String readFileAsString(String fileName)  throws  Exception {
        String content 
=   new  String(readFileBinary(fileName));

        
return  content;
    }
    
/**
     * d文gq返回ؓl定字符集的字符?
     * 
@param  fileName
     * 
@param  encoding
     * 
@return
     * 
@throws  Exception
     
*/
    
public   static  String readFileAsString(String fileName, String encoding)  throws  Exception {
        String content 
=   new  String(readFileBinary(fileName), encoding);

        
return  content;
    }
    
/**
     * d文gq返回ؓl定字符集的字符?
     * 
@param  fileName
     * 
@param  encoding
     * 
@return
     * 
@throws  Exception
     
*/
    
public   static  String readFileAsString(InputStream in)  throws  Exception {
        String content 
=   new  String(readFileBinary(in));

        
return  content;
    }

    
/**
     * Read content from local file to binary byte array.
     *
     * 
@param  fileName -
     *            local file name to read
     * 
@return
     * 
@throws  Exception
     
*/
    
public   static   byte [] readFileBinary(String fileName)  throws  Exception {
        FileInputStream fin 
=   new  FileInputStream(fileName);
        
return  readFileBinary(fin);
    }
    
/**
     * 从输入流d数据Zq制字节数组.
     * 
@param  streamIn
     * 
@return
     * 
@throws  IOException
     
*/
    
public   static   byte [] readFileBinary(InputStream streamIn)  throws  IOException {
        BufferedInputStream in 
=   new  BufferedInputStream(streamIn);

        ByteArrayOutputStream out 
=   new  ByteArrayOutputStream( 10240 );

        
int  len;
        
while  ((len  =  in.read(buf))  >=   0 )
            out.write(buf, 
0 , len);
        in.close();

        
return  out.toByteArray();       
    }

    
/**
     * Write string content to local file.
     *
     * 
@param  fileName -
     *            local file name will write to
     * 
@param  content
     *            String text
     * 
@return  true if success
     * 
@throws  IOException
     
*/
    
public   static   boolean  writeFileString(String fileName, String content)
            
throws  IOException {
        FileWriter fout 
=   new  FileWriter(fileName);
        fout.write(content);
        fout.close();
        
return   true ;
    }

    
/**
     * Write string content to local file using given character encoding.
     *
     * 
@param  fileName -
     *            local file name will write to
     * 
@param  content
     *            String text
     * 
@param  encoding
     *            the encoding
     * 
@return  true if success
     * 
@throws  IOException
     
*/
    
public   static   boolean  writeFileString(String fileName, String content,
            String encoding) 
throws  IOException {
        OutputStreamWriter fout 
=   new  OutputStreamWriter( new  FileOutputStream(
                fileName), encoding);

        fout.write(content);
        fout.close();
        
return   true ;
    }

    
/**
     * Write binary byte array to local file.
     *
     * 
@param  fileName -
     *            local file name will write to
     * 
@param  content
     *            binary byte array
     * 
@return  true if success
     * 
@throws  IOException
     
*/
    
public   static   boolean  writeFileBinary(String fileName,  byte [] content)
            
throws  IOException {
        FileOutputStream fout 
=   new  FileOutputStream(fileName);
        fout.write(content);
        fout.close();
        
return   true ;
    }

    
/**
     * 查文件名是否合法.文g名字不能包含字符\/:*?"<>|
     *
     * 
@param  fileName文g?不包含\?br />     *  @return  boolean is valid file name
     
*/
    
public   static   boolean  isValidFileName(String fileName) {
        
boolean  isValid  =   true ;
        String errChar 
=   " \\/:*?\ " <>| " ; //
         if  (fileName  ==   null   ||  fileName.length()  ==   0 ) {
            isValid 
=   false ;
        } 
else  {
            
for  ( int  i  =   0 ; i  <  errChar.length(); i ++ ) {
                
if  (fileName.indexOf(errChar.charAt(i))  !=   - 1 ) {
                    isValid 
=   false ;
                    
break ;
                }
            }
        }
        
return  isValid;
    }

    
/**
     * 把非法文件名转换为合法文件名.
     *
     * 
@param  fileName
     * 
@return
     
*/
    
public   static  String replaceInvalidFileChars(String fileName) {
        StringBuffer out 
=   new  StringBuffer();

        
for  ( int  i  =   0 ; i  <  fileName.length(); i ++ ) {
            
char  ch  =  fileName.charAt(i);
            
//  Replace invlid chars: \\/:*?\"<>|
             switch  (ch) {
            
case   ' \\ ' :
            
case   ' / ' :
            
case   ' : ' :
            
case   ' * ' :
            
case   ' ? ' :
            
case   ' \" ' :
            
case   ' < ' :
            
case   ' > ' :
            
case   ' | ' :
                out.append(
' _ ' );
                
break ;
            
default :
                out.append(ch);
            }
        }

        
return  out.toString();
    }

    
/**
     * Convert a given file name to a URL(URI) string.
     *
     * 
@param  fileName -
     *            the file to parse
     * 
@return  - URL string
     
*/
    
public   static  String filePathToURL(String fileName) {
        String fileUrl 
=   new  File(fileName).toURI().toString();
        
return  fileUrl;
    }

    
/**
     * Write string content to local file.
     *
     * 
@param  fileName -
     *            local file name will write to
     * 
@param  content
     *            String text
     * 
@return  true if success
     * 
@throws  IOException
     
*/
    
public   static   boolean  appendFileString(String fileName, String content)
            
throws  IOException {
        OutputStreamWriter fout 
=   new  OutputStreamWriter( new  FileOutputStream(
                fileName, 
true ),  " GBK " );

        fout.write(content);
        fout.close();
        
return   true ;
    }
    
public   static   void  main(String[] args) {
        System.out.println(replaceInvalidFileChars(
" http://www.abc.com/ " ));
    }
}





良帅 2010-05-12 22:03 发表评论
]]>
Oracle 打开游标过最大数问题http://www.aygfsteel.com/Collus/articles/318562.html良帅良帅Fri, 16 Apr 2010 12:37:00 GMThttp://www.aygfsteel.com/Collus/articles/318562.htmlhttp://www.aygfsteel.com/Collus/comments/318562.htmlhttp://www.aygfsteel.com/Collus/articles/318562.html#Feedback0http://www.aygfsteel.com/Collus/comments/commentRss/318562.htmlhttp://www.aygfsteel.com/Collus/services/trackbacks/318562.html
实际上,q个错误的原因,主要q是代码问题引v的。?br /> ORA-01000: maximum open cursors exceededQ表C已l达C个进E打开的最大游标数?

   q样的错误很Ҏ出现在Java代码中的主要原因是:Java代码在执 行conn.createStatement()和conn.prepareStatement()的时候,实际上都是相当与在数据库中打开了一? cursor。尤其是Q如果你的createStatement和prepareStatement是在一个@环里面的话,׃非常Ҏ出现q个问题。因 为游标一直在不停的打开Q而且没有关闭?/span>

一般来_在写Java代码的时候,createStatement和prepareStatement都应该要攑֜循环外面Q而且使用了这? Statment后,及时关闭。最好是在执行了一ơexecuteQuery、executeUpdate{之后,如果不需要用结果集 QResultSetQ的数据Q就马上Statment关闭。?br />
对于出现ORA-01000错误q种情况Q单U的加大open_cursorsq不是好办法Q那只是L不治本。实际上Q代码中的隐患ƈ没有解除。?br /> 而且Q绝大部分情况下Qopen_cursors只需要设|一个比较小的|p够用了Q除非有非常特别的要求?

oracle 9i 默认的open_cursors=300  


一、看有问题的代码

 1 import java.sql.Connection;
 2 import java.sql.DriverManager;
 3 import java.sql.SQLException;
 4 import java.sql.Statement;
 5 
 6 public class Test {
 7   public Connection getConnection() {
 8     String url = "jdbc:oracle:thin:@localhost:1521:ora9i";
 9     String user = "scott";
10     String password = "tiger";
11     Connection con = null;
12     try {
13       Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
14       con = DriverManager.getConnection(url, user, password);
15     } catch (Exception e) {
16       e.printStackTrace();
17     }
18     return con;
19   }
20 
21   public static void main(String[] args) throws SQLException {
22     long a = 13819100000L;
23     long b = 13819100600L// 问题?/span>
24     Connection con = null;
25     Statement stmt = null;
26     Test insert = new Test();
27     try {
28       con = insert.getConnection();
29       for (long c = a; c <= b; c++) {
30         String sql = "insert into telepnum values(" + c + ")";
31         stmt = con.createStatement(); // q里是问题的所?/span>
32         stmt.executeUpdate(sql);
33       }
34       System.out.println("OK");
35     } catch (Exception e) {
36       e.printStackTrace();
37     } finally {
38       if (con != null) {
39         con.close();
40       }
41     }
42   }
43 }

二、分?/span>

在@环里面每ơ都 stmt = con.createStatement(); 而没有释放,q样每个都占用了一个服务器的游标资源,最后造成p|

三、解x?/span>

1、增加关闭语?
 
1   con = insert.getConnection();
2       for (long c = a; c <= b; c++) {
3         String sql = "insert into telepnum values(" + c + ")";
4         stmt = con.createStatement(); // q里是问题的所?/span>
5         stmt.executeUpdate(sql);
6         stmt.close(); // 用完了就关闭好了
7       }

2、将q句话移动到循环外面Q推荐用q个
1 con = insert.getConnection();
2       stmt = con.createStatement(); // Ud到这里,Statemet是可以重用的
3       for (long c = a; c <= b; c++) {
4         String sql = "insert into telepnum values(" + c + ")";
5         stmt.executeUpdate(sql);
6       }
7       stmt.close(); // 用完了就关闭好了

3、改装成扚w更新
1 con = insert.getConnection();
2       con.setAutoCommit(false);
3       stmt = con.createStatement(); // Ud到这里,Statemet是可以重用的
4       for (long c = a; c <= b; c++) {
5         String sql = "insert into telepnum values(" + c + ")";
6         stmt.addBatch(sql);
7       }
8       stmt.executeBatch();
9       con.commit();


四、ȝ

    鉴于上面的问题,在做基类的时候,在对数据q行DML操作的时候,量不要让基c返回Statement,而应该在基类直接q行关闭。在做查询的时候,? 以把statement留给E序员自p行手动关闭,关闭的方法ؓQ给ResultSet一个方法可以得到StatementQ然后再关闭 Statement。个U方法是比较妥当的?br />

良帅 2010-04-16 20:37 发表评论
]]>
JAVA 旉操作大全http://www.aygfsteel.com/Collus/articles/318152.html良帅良帅Tue, 13 Apr 2010 05:22:00 GMThttp://www.aygfsteel.com/Collus/articles/318152.htmlhttp://www.aygfsteel.com/Collus/comments/318152.htmlhttp://www.aygfsteel.com/Collus/articles/318152.html#Feedback0http://www.aygfsteel.com/Collus/comments/commentRss/318152.htmlhttp://www.aygfsteel.com/Collus/services/trackbacks/318152.html Java 旉操作大全

函数列表Q?br />


/**
   * 获取现在旉
   *
   * @return q回旉cd yyyy-MM-dd HH:mm:ss
   */

public static Date getNowDate() {
   Date currentTime = new Date();
   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
   String dateString = formatter.format(currentTime);
   ParsePosition pos = new ParsePosition(8);
   Date currentTime_2 = formatter.parse(dateString, pos);
   return currentTime_2;
}

/**
   * 获取现在旉
   *
   * @returnq回短时间格?yyyy-MM-dd
   */

public static Date getNowDateShort() {
   Date currentTime = new Date();
   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
   String dateString = formatter.format(currentTime);
   ParsePosition pos = new ParsePosition(8);
   Date currentTime_2 = formatter.parse(dateString, pos);
   return currentTime_2;
}

/**
   * 获取现在旉
   *
   * @returnq回字符串格?yyyy-MM-dd HH:mm:ss
   */

public static String getStringDate() {
   Date currentTime = new Date();
   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
   String dateString = formatter.format(currentTime);
   return dateString;
}

/**
   * 获取现在旉
   *
   * @return q回短时间字W串格式yyyy-MM-dd
   */

public static String getStringDateShort() {
   Date currentTime = new Date();
   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
   String dateString = formatter.format(currentTime);
   return dateString;
}

/**
   * 获取旉 时:?U?HH:mm:ss
   *
   * @return
   */

public static String getTimeShort() {
   SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
   Date currentTime = new Date();
   String dateString = formatter.format(currentTime);
   return dateString;
}

/**
   * 长旉格式字符串{换ؓ旉 yyyy-MM-dd HH:mm:ss
   *
   * @param strDate
   * @return
   */
public static Date strToDateLong(String strDate) {
   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
   ParsePosition pos = new ParsePosition(0);
   Date strtodate = formatter.parse(strDate, pos);
   return strtodate;
}

/**
   * 长旉格式旉转换为字W串 yyyy-MM-dd HH:mm:ss
   *
   * @param dateDate
   * @return
   */

public static String dateToStrLong(java.util.Date dateDate) {
   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
   String dateString = formatter.format(dateDate);
   return dateString;
}

/**
   * 短旉格式旉转换为字W串 yyyy-MM-dd
   *
   * @param dateDate
   * @param k
   * @return
   */
public static String dateToStr(java.util.Date dateDate) {
   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
   String dateString = formatter.format(dateDate);
   return dateString;
}

/**
   * 短旉格式字符串{换ؓ旉 yyyy-MM-dd
   *
   * @param strDate
   * @return
   */

public static Date strToDate(String strDate) {
   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
   ParsePosition pos = new ParsePosition(0);
   Date strtodate = formatter.parse(strDate, pos);
   return strtodate;
}

/**
   * 得到现在旉
   *
   * @return
   */

public static Date getNow() {
   Date currentTime = new Date();
   return currentTime;
}

/**
   * 提取一个月中的最后一?br />    *
   * @param day
   * @return
   */

public static Date getLastDate(long day) {
   Date date = new Date();
   long date_3_hm = date.getTime() - 3600000 * 34 * day;
   Date date_3_hm_date = new Date(date_3_hm);
   return date_3_hm_date;
}

/**
   * 得到现在旉
   *
   * @return 字符?yyyyMMdd HHmmss
   */

public static String getStringToday() {
   Date currentTime = new Date();
   SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd HHmmss");
   String dateString = formatter.format(currentTime);
   return dateString;
}

/**
   * 得到现在时
   */

public static String getHour() {
   Date currentTime = new Date();
   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
   String dateString = formatter.format(currentTime);
   String hour;
   hour = dateString.substring(11, 13);
   return hour;
}

/**
   * 得到现在分钟
   *
   * @return
   */

public static String getTime() {
   Date currentTime = new Date();
   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
   String dateString = formatter.format(currentTime);
   String min;
   min = dateString.substring(14, 16);
   return min;
}

/**
   * Ҏ用户传入的时间表C格式,q回当前旉的格?如果是yyyyMMddQ注意字母y不能大写?br />    *
   * @param sformat
   *             yyyyMMddhhmmss
   * @return
   */
public static String getUserDate(String sformat) {
   Date currentTime = new Date();
   SimpleDateFormat formatter = new SimpleDateFormat(sformat);
   String dateString = formatter.format(currentTime);
   return dateString;
}

/**
   * 二个时旉间的差?必须保证二个旉都是"HH:MM"的格式,q回字符型的分钟
   */

public static String getTwoHour(String st1, String st2) {
   String[] kk = null;
   String[] jj = null;
   kk = st1.split(":");
   jj = st2.split(":");
   if (Integer.parseInt(kk[0]) < Integer.parseInt(jj[0]))
    return "0";
   else {
    double y = Double.parseDouble(kk[0]) + Double.parseDouble(kk[1]) / 60;
    double u = Double.parseDouble(jj[0]) + Double.parseDouble(jj[1]) / 60;
    if ((y - u) > 0)
     return y - u + "";
    else
     return "0";
   }
}

/**
   * 得到二个日期间的间隔天数
   */

public static String getTwoDay(String sj1, String sj2) {
   SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
   long day = 0;
   try {
    java.util.Date date = myFormatter.parse(sj1);
    java.util.Date mydate = myFormatter.parse(sj2);
    day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
   } catch (Exception e) {
    return "";
   }
   return day + "";
}

/**
   * 旉前推或后推分?其中JJ表示分钟.
   */

public static String getPreTime(String sj1, String jj) {
   SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
   String mydate1 = "";
   try {
    Date date1 = format.parse(sj1);
    long Time = (date1.getTime() / 1000) + Integer.parseInt(jj) * 60;
    date1.setTime(Time * 1000);
    mydate1 = format.format(date1);
   } catch (Exception e) {
   }
   return mydate1;
}

/**
   * 得到一个时间g后或前移几天的时?nowdate为时?delay为前UL后g的天?br />    */

public static String getNextDay(String nowdate, String delay) {
   try{
   SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
   String mdate = "";
   Date d = strToDate(nowdate);
   long myTime = (d.getTime() / 1000) + Integer.parseInt(delay) * 24 * 60 * 60;
   d.setTime(myTime * 1000);
   mdate = format.format(d);
   return mdate;
   }catch(Exception e){
    return "";
   }
}

/**
   * 判断是否润年
   *
   * @param ddate
   * @return
   */

public static boolean isLeapYear(String ddate) {

   /**
    * 详细设计Q?1.?00整除是闰q_否则Q?2.不能?整除则不是闰q?3.能被4整除同时不能?00整除则是闰年
    * 3.能被4整除同时能被100整除则不是闰q?br />     */

   Date d = strToDate(ddate);
   GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
   gc.setTime(d);
   int year = gc.get(Calendar.YEAR);
   if ((year % 400) == 0)
    return true;
   else if ((year % 4) == 0) {
    if ((year % 100) == 0)
     return false;
    else
     return true;
   } else
    return false;
}

/**
   * q回国旉格式 26 Apr 2006
   *
   * @param str
   * @return
   */

public static String getEDate(String str) {
   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
   ParsePosition pos = new ParsePosition(0);
   Date strtodate = formatter.parse(str, pos);
   String j = strtodate.toString();
   String[] k = j.split(" ");
   return k[2] + k[1].toUpperCase() + k[5].substring(2, 4);
}

/**
   * 获取一个月的最后一?br />    *
   * @param dat
   * @return
   */
public static String getEndDateOfMonth(String dat) {// yyyy-MM-dd
   String str = dat.substring(0, 8);
   String month = dat.substring(5, 7);
   int mon = Integer.parseInt(month);
   if (mon == 1 || mon == 3 || mon == 5 || mon == 7 || mon == 8 || mon == 10 || mon == 12) {
    str += "31";
   } else if (mon == 4 || mon == 6 || mon == 9 || mon == 11) {
    str += "30";
   } else {
    if (isLeapYear(dat)) {
     str += "29";
    } else {
     str += "28";
    }
   }
   return str;
}

/**
   * 判断二个旉是否在同一个周
   *
   * @param date1
   * @param date2
   * @return
   */

public static boolean isSameWeekDates(Date date1, Date date2) {
   Calendar cal1 = Calendar.getInstance();
   Calendar cal2 = Calendar.getInstance();
   cal1.setTime(date1);
   cal2.setTime(date2);
   int subYear = cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR);
   if (0 == subYear) {
    if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
     return true;
   } else if (1 == subYear && 11 == cal2.get(Calendar.MONTH)) {
    // 如果12月的最后一周横跨来q第一周的话则最后一周即做来年的第一?br />     if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
     return true;
   } else if (-1 == subYear && 11 == cal1.get(Calendar.MONTH)) {
    if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
     return true;
   }
   return false;
}

/**
   * 产生周序?卛_到当前时间所在的q度是第几周
   *
   * @return
   */

public static String getSeqWeek() {
   Calendar c = Calendar.getInstance(Locale.CHINA);
   String week = Integer.toString(c.get(Calendar.WEEK_OF_YEAR));
   if (week.length() == 1)
    week = "0" + week;
   String year = Integer.toString(c.get(Calendar.YEAR));
   return year + week;
}

/**
   * 获得一个日期所在的周的星期几的日期Q如要找?002q??日所在周的星期一是几?br />    *
   * @param sdate
   * @param num
   * @return
   */
public static String getWeek(String sdate, String num) {
   // 再{换ؓ旉
   Date dd = VeDate.strToDate(sdate);
   Calendar c = Calendar.getInstance();
   c.setTime(dd);
   if (num.equals("1")) // q回星期一所在的日期
    c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
   else if (num.equals("2")) // q回星期二所在的日期
    c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
   else if (num.equals("3")) // q回星期三所在的日期
    c.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);
   else if (num.equals("4")) // q回星期四所在的日期
    c.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
   else if (num.equals("5")) // q回星期五所在的日期
    c.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
   else if (num.equals("6")) // q回星期六所在的日期
    c.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
   else if (num.equals("0")) // q回星期日所在的日期
    c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
   return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
}

/**
   * Ҏ一个日期,q回是星期几的字W串
   *
   * @param sdate
   * @return
   */

public static String getWeek(String sdate) {
   // 再{换ؓ旉
   Date date = VeDate.strToDate(sdate);
   Calendar c = Calendar.getInstance();
   c.setTime(date);
   // int hour=c.get(Calendar.DAY_OF_WEEK);
   // hour中存的就是星期几了,其范?1~7
   // 1=星期?7=星期六,其他cL
   return new SimpleDateFormat("EEEE").format(c.getTime());
}
public static String getWeekStr(String sdate){
   String str = "";
   str = VeDate.getWeek(sdate);
   if("1".equals(str)){
    str = "星期?;
   }else if("2".equals(str)){
    str = "星期一";
   }else if("3".equals(str)){
    str = "星期?;
   }else if("4".equals(str)){
    str = "星期?;
   }else if("5".equals(str)){
    str = "星期?;
   }else if("6".equals(str)){
    str = "星期?;
   }else if("7".equals(str)){
    str = "星期?;
   }
   return str;
}

/**
   * 两个旉之间的天?br />    *
   * @param date1
   * @param date2
   * @return
   */

public static long getDays(String date1, String date2) {
   if (date1 == null || date1.equals(""))
    return 0;
   if (date2 == null || date2.equals(""))
    return 0;
   // 转换为标准时?br />    SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
   java.util.Date date = null;
   java.util.Date mydate = null;
   try {
    date = myFormatter.parse(date1);
    mydate = myFormatter.parse(date2);
   } catch (Exception e) {
   }
   long day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
   return day;
}

/**
   * 形成如下的日?Q?Ҏ传入的一个时间返回一个结?星期?星期一 星期?星期?星期?星期?星期?下面是当月的各个旉
   * 此函数返回该日历W一行星期日所在的日期
   *
   * @param sdate
   * @return
   */
public static String getNowMonth(String sdate) {
   // 取该旉所在月的一?br />    sdate = sdate.substring(0, 8) + "01";

   // 得到q个月的1h星期?br />    Date date = VeDate.strToDate(sdate);
   Calendar c = Calendar.getInstance();
   c.setTime(date);
   int u = c.get(Calendar.DAY_OF_WEEK);
   String newday = VeDate.getNextDay(sdate, (1 - u) + "");
   return newday;
}

/**
   * 取得数据库主?生成格式为yyyymmddhhmmss+k位随机数
   *
   * @param k
   *             表示是取几位随机敎ͼ可以自己?br />    */

public static String getNo(int k) {

   return getUserDate("yyyyMMddhhmmss") + getRandom(k);
}

/**
   * q回一个随机数
   *
   * @param i
   * @return
   */

public static String getRandom(int i) {
   Random jjj = new Random();
   // int suiJiShu = jjj.nextInt(9);
   if (i == 0)
    return "";
   String jj = "";
   for (int k = 0; k < i; k++) {
    jj = jj + jjj.nextInt(9);
   }
   return jj;
}






良帅 2010-04-13 13:22 发表评论
]]>
վ֩ģ壺 ڳ| | | ƽɽ| Т| | Ӵ| | ɽ| | | | Ͻ| | ̨ɽ| | ʼ| | ̫| Դ| տ| | ʩ| ۲| | ˿| | ޳| | DZɽ| IJ| ɰ| ½| °Ͷ| | | | | | | |