JAVA常用函数4

public static  String toGBK(String str){ 
if(str==null) str=""
else 
try { 
str
=new String(str.getBytes("ISO-8859-1"),"GBK"); 
catch (UnsupportedEncodingException e) { 
System.out.println(
"转换字符串异常!"+e.getMessage()); 
e.printStackTrace(); 


return str; 




public static  String toUTF8(String str){ 
if(str==null) str=""
else 
try { 
str
=new String(str.getBytes("ISO-8859-1"),"UTF8"); 
catch (UnsupportedEncodingException e) { 
e.printStackTrace(); 

return str; 





提供一个随机类 :) 

/*
 * @author talent_marquis<��˺��>
 * Email: talent_marquis@163.com
 * Copyright (C) 2007 talent_marquis<��˺��>
 * All rights reserved.
 
*/
package com.dextrys.trilogy.util;

import java.util.Arrays;

import org.eclipse.swt.graphics.RGB;

public class RandomUtil
{

    
/**
     * 
@param args
     
*/
    
public static void main( String[] args )
    {
        
//System.out.println( getRandomNormalString( 8 ) );
        int[] test = getRandomIntWithoutReduplicate( 04039 );
        Arrays.sort( test );
        
forint i : test )
        {
            System.out.println( i );
        }
    }

    
/**
     * get a integer array filled with random integer without reduplicate [min, max)
     * 
@param min    the minimum value
     * 
@param max the maximum value
     * 
@param size the capacity of the array
     * 
@return a integer array filled with random integer without redupulicate
     
*/
    
public static int[] getRandomIntWithoutReduplicate( int min, int max, int size )
    {
        
int[] result = new int[size];
        
        
int arraySize = max - min;
        
int[] intArray = new int[arraySize];
        
// init intArray
        forint i = 0 ; i < intArray.length ; i++ )
        {
            intArray[i] 
= i + min;
        }
        
// get randome interger without reduplicate
        forint i = 0 ; i < size ; i++ )
        {
            
int c = getRandomInt( min, max - i );
            
int index = c - min;
            swap( intArray, index, arraySize 
- 1 - i );
            result[i] 
= intArray[ arraySize - 1 - i ];
        }
                
        
return result;
    }
    
    
private static void swap( int[] array, int x, int y )
    {
        
int temp = array[x];
        array[x] 
= array[y];
        array[y] 
= temp;
    }
    
    
/**
     * get a random Integer with the range [min, max)
     * 
@param min the minimum value
     * 
@param max the maximum value
     * 
@return the random Integer value
     
*/
    
public static int getRandomInt( int min, int max )
    {
        
// include min, exclude max
        int result = min + new Double( Math.random() * ( max - min ) ).intValue();

        
return result;
    }

    
/**
     * get a random double with the range [min, max)
     * 
@param min the minimum value
     * 
@param max the maximum value
     * 
@return the random double value
     
*/
    
public static double getRandomDouble( double min, double max )
    {
        
// include min, exclude max
        double result = min + ( Math.random() * ( max - min ) );
        
return result;
    }

    
/**
     * 
     * 
@return a random char with the range ASCII 33(!) to ASCII 126(~)
     
*/
    
public static char getRandomChar()
    {
        
// from ASCII code 33 to ASCII code 126
        int firstChar = 33// "!"
        int lastChar = 126// "~"
        char result = ( char ) ( getRandomInt( firstChar, lastChar + 1 ) );
        
return result;
    }
    
    
/**
     * 
     * 
@return a random rgb color
     
*/
    
public static RGB getRandomRGB()
    {
        
int red = getRandomInt(0,256);
        
int green = getRandomInt(0,256);
        
int blue = getRandomInt(0,256);
        
        
return new RGB( red, green, blue );
    }
    
    
/**
     * 
     * 
@return a random char with the range [0-9],[a-z],[A-Z]
     
*/
    
public static char getRandomNormalChar()
    {
        
// include 0-9,a-z,A-Z
        int number = getRandomInt( 062 );
        
int zeroChar = 48;
        
int nineChar = 57;
        
int aChar = 97;
        
int zChar = 122;
        
int AChar = 65;
        
int ZChar = 90;
        
        
char result;
        
        
if( number < 10 )
        {
            result 
=  ( char ) ( getRandomInt( zeroChar, nineChar + 1 ) );
            
return result;

        }
        
else if( number >= 10 && number < 36 )
        {
            result 
= ( char ) ( getRandomInt( AChar, ZChar + 1 ) );
            
return result;
        }
        
else if( number >= 36 && number < 62 )
        {
            result 
=  ( char ) ( getRandomInt( aChar, zChar + 1 ) );
            
return result;
        }
        
else
        {
            
return 0;
        }
    }

    
/**
     * 
     * 
@param length the length of the String
     * 
@return a String filled with random char
     
*/
    
public static String getRandomString( int length )
    {
        
// include ASCII code from 33 to 126
        StringBuffer result = new StringBuffer();
        
forint i = 0; i < length; i++ )
        {
            result.append( getRandomChar() );
        }
        
return result.toString();
    }
    
    
/**
     *     
     * 
@param length the length of the String
     * 
@return a String filled with normal random char
     
*/
    
public static String getRandomNormalString( int length )
    {
        
// include 0-9,a-z,A-Z
        StringBuffer result = new StringBuffer();
        
forint i = 0; i < length; i++)
        {
            result.append( getRandomNormalChar() );
        }
        
return result.toString();
    }
}

/**
 * 计算传入值是否星期六
 * 回车:true  or  false
 
*/

import java.util.*;

public class Week6 {
    
public boolean checkWeek6(String str){
        
boolean flag=false;
        
int week6=0;
        str.replace(
'/','-');
        Calendar cal
=Calendar.getInstance();
        cal.setTime(java.sql.Date.valueOf(str.substring(
0,10)));
        week6
=cal.get(Calendar.DAY_OF_WEEK);
        
        
if(week6==7){
            flag
=true;
        }
        
        
return flag;
    }
}

/**
 * 取得服务器当前的各种具体时间
 * 回车:日期时间
 
*/

import java.util.*;

public class GetNowDate{
    Calendar  calendar 
= null;
  
    
public GetNowDate(){
        calendar 
= Calendar.getInstance();
        calendar.setTime(
new Date());
    }
  
    
public int getYear(){
        
return calendar.get(Calendar.YEAR);
    }
  
    
public int getMonth(){
        
return 1 + calendar.get(Calendar.MONTH);
    }
  
    
public int getDay(){
        
return calendar.get(Calendar.DAY_OF_MONTH);
    }
  
    
public int getHour(){
        
return calendar.get(Calendar.HOUR_OF_DAY);
    }
    
    
public int getMinute(){
        
return calendar.get(Calendar.MINUTE);
    }
  
    
public int getSecond(){
        
return calendar.get(Calendar.SECOND);
    }
    
    
public String getDate(){
        
return getMonth()+"/"+getDay()+"/"+getYear();
    }
  
    
public String getTime(){
        
return getHour()+":"+getMinute()+":"+getSecond();
    }
  
    
public String getDate2(){
        String yyyy
="0000", mm="00", dd="00";
        yyyy 
= yyyy + getYear();
        mm   
= mm   + getMonth();
        dd   
= dd + getDay();
        yyyy 
= yyyy.substring(yyyy.length()-4);
        mm   
= mm.substring(mm.length()-2);
        dd   
= dd.substring(dd.length()-2);
        
return yyyy + "/" + mm + "/" +  dd;
    }
  
    
public String getTime2(){
        String hh
="00", mm="00", ss="00";
        hh 
= hh + getHour();
        mm 
= mm + getMinute();
        ss 
= ss + getSecond();
        hh 
= hh.substring(hh.length()-2, hh.length());
        mm 
= mm.substring(mm.length()-2, mm.length());
        ss 
= ss.substring(ss.length()-2, ss.length());
        
return hh + ":" + mm + ":" + ss;
    }
}

/**
 * Big5字与Unicode的互换
 * 转换后的正常字型
 
*/

import java.io.*;

public class MyUtil{
    
public static String big5ToUnicode(String s){
        
try{
            
return new String(s.getBytes("ISO8859_1"), "Big5");
        }
        
catch (UnsupportedEncodingException uee){
            
return s;
        }
    }

    
public static String UnicodeTobig5(String s){
        
try{
            
return new String(s.getBytes("Big5"), "ISO8859_1");
        }
        
catch (UnsupportedEncodingException uee){
            
return s;
        }
    }

    
public static String toHexString(String s){
        String str
="";
        
for (int i=0; i<s.length(); i++){
            
int ch=(int)s.charAt(i);
            String s4
="0000"+Integer.toHexString(ch);
            str
=str+s4.substring(s4.length()-4)+" ";
        }
        
return str;
    }
}
posted @ 2009-10-09 23:08  angushine  阅读(300)  评论(0编辑  收藏  举报