📄 tools.java
字号:
/*
* @(#)clsTools.java
*
* Copyright 2000-2001 Liu Xue. All Rights Reserved.
*
* This software is the proprietary information of Asiaitech iTechnology Limited.
* Use is subject to license terms.
*
*/
package org.lazybug.util;
import org.apache.oro.text.regex.*;
import java.util.*;
import java.text.SimpleDateFormat;
import java.io.File;
import java.io.IOException;
import java.io.FileInputStream;
import java.security.MessageDigest;
/*
* Author: Xue Liu
* Date: 2002/04/16
*/
public class Tools
{
public static final long MILLI_OF_WEEK = 7*24*60*60*1000;
public static final long MILLI_OF_DAY = 24*60*60*1000;
public static final long MILLI_OF_HOUR = 60*60*1000;
public static final long MILLI_OF_MINUTE = 60*1000;
public static final int SECOND_OF_WEEK = 7*24*60*60;
public static final int SECOND_OF_DAY = 24*60*60;
public static final int SECOND_OF_HOUR = 60*60;
public static final int SECOND_OF_MINUTE = 60;
/**
* 转换日期成为标准的整形表示的时间(采用默认的间隔符-)
* @param date 字符串格式的日期表现(yyyy-MM-dd)
* @return
*/
public static Random random = new Random();
public static final int getRandomInt( int p0 )
{
return random.nextInt(p0);
}
public static final int getDateInSeconds(String date)
{
return getDateInSeconds(date,"-");
}
/**
* 转换日期成为标准的整形表现的时间
* @param date 字符串格式的日期表现
* @param Separator 表现的间隔符
* @return
*/
public static final int getDateInSeconds(String date, String Separator)
{
return (int)(getDateInMillis(date,Separator)/1000);
}
/**
* 转换日期成为标准的长整形表现的时间
* @param date
* @param Separator
* @return
*/
public static final long getDateInMillis(String date, String Separator)
{
if( date == null || date.length() == 0 ){
return -1;
}
//YYYY-MM-DD日期格式必须满足这样的形式
String[] format = date.split(Separator);
Calendar time = Calendar.getInstance();
time.clear();
try
{
time.set( Integer.parseInt( format[0] ),
Integer.parseInt( format[1] ) - 1,
Integer.parseInt( format[2] ) );
}
catch(Exception e)
{
return 0;
}
return time.getTimeInMillis();
}
/**
* 得到精确到秒的时间数据
* @param KKMMSS 精确时间的字符串形式(默认:间隔)
* @return
*/
public static final int getTimeInSeconds(String KKMMSS)
{
return getTimeInSeconds(KKMMSS, ":");
}
/**
* 得到精确到秒的时间数据
* @param KKMMSS 精确时间的字符串形式
* @param Separator 时间的间隔符
* @return
*/
public static final int getTimeInSeconds(String KKMMSS, String Separator)
{
return (int)(getTimeInMillis(KKMMSS,Separator)/1000);
}
/**
* 将用于显示的时间字符串转换成精确到毫秒的时间数据
* @param KKMMSS
* @param Separator
* @return
*/
public static final long getTimeInMillis(String KKMMSS, String Separator)
{
//YYYY-MM-DD日期格式必须满足这样的形式
String[] format = KKMMSS.split(Separator);
long millis = 0;
switch(format.length)
{
case 1:
millis = Integer.parseInt(format[0])*60*60;
break;
case 2:
millis = Integer.parseInt(format[0])*60*60+Integer.parseInt(format[1])*60;
break;
case 3:
millis = Integer.parseInt(format[0])*60*60+Integer.parseInt(format[1])*60+Integer.parseInt(format[2]);
break;
}
return millis*1000;
}
/**
* 将标准的长整形的时间转换成指定格式的时间表现
* @param TimeFormat 可配的时间表现的格式(可以是yyyy年MM月dd日 HH:mm:ss)
* @param millis
* @return
*/
public static final String getFormatTime(String TimeFormat, long millis)
{
SimpleDateFormat sdf = new SimpleDateFormat(TimeFormat);
java.util.Calendar time = java.util.Calendar.getInstance();
time.clear();
time.setTimeInMillis(millis);
return sdf.format(time.getTime());
}
/**
* 将标准的整形的时间转换成指定格式的时间表现
* @param TimeFormat 可配的时间表现的格式(可以是yyyy年MM月dd日 HH:mm:ss)
* @param seconds
* @return
*/
public static final String getFormatTime(String TimeFormat, int seconds)
{
return getFormatTime(TimeFormat, ((long)seconds)*1000);
}
/**
* 转换精确时间为指定的格式(加入了时区的概念)
* @param TimeFormat 可配的时间表现的格式(可以是 HH:mm:ss)
* @param lt
* @return
*/
public static final String getSmartTime(int lt)
{
return getSmartTime("HH:mm:ss", lt);
}
public static final String getSmartTime(String TimeFormat, int lt)
{
int s = lt%SECOND_OF_DAY;
int hour = s/SECOND_OF_HOUR;
int minute = s%SECOND_OF_HOUR/SECOND_OF_MINUTE;
int seconds = s%SECOND_OF_MINUTE;
StringBuffer sb = new StringBuffer();
String[] formats = TimeFormat.split(":");
for(int i = 0; i < formats.length; i++ )
{
if( formats[i].equals("HH") )
{
sb.append(hour>9?""+hour:"0"+hour);
}
else if( formats[i].equals("mm") )
{
sb.append(minute>9?""+minute:"0"+minute);
}
else if( formats[i].equals("ss") )
{
sb.append(minute>9?""+minute:"0"+minute);
}
else
{
sb.append(formats[i]);
}
if( i + 1 < formats.length ) sb.append(":");
}
return sb.toString();
}
//**************************************************
public static final String replaceStr(String param_source,
String param_char,
String param_replace)
{
StringBuffer workStr = new StringBuffer(param_source.length()+1024);
int idx, i = 0;
idx = param_source.indexOf(param_char);
while (idx != -1)
{
//System.out.print(param_source.length());
workStr.append(param_source.substring(i, idx));
workStr.append(param_replace);
i = idx + param_char.length();
idx = param_source.indexOf(param_char, i);
//System.out.print(":"+param_source.length());
//System.out.println(" "+i+","+idx);
}
if( i != param_source.length() )
{
workStr.append(param_source.substring(i));
}
//System.out.println(workStr.toString());
return workStr.toString();
}
//**************************************************
public static final String charTrim(String param_source, String param_char)
{
// Start from the begining
int frontIdx=0, rearIdx=param_source.length();
while (param_source.substring(frontIdx,frontIdx+1).equals(param_char)) frontIdx++;
while (param_source.substring(rearIdx-1,rearIdx).equals(param_char)) rearIdx--;
return param_source.substring(frontIdx,rearIdx-1);
}
//**************************************************
public static final int generateHash(String magicStr)
{
int i = magicStr.length();
char ac[] = new char[i];
magicStr.getChars(0, i, ac, 0);
int j = 13;
for(int k = i - 1; k >= 0; k--) j = ac[k] + j * 29;
if(j < 0) j *= -1;
return j;
}
//**************************************************
public static final String generateKey(int magicInt)
{
return Integer.toString(magicInt & 0xffff ^ magicInt >> 16 & 0xffff, 16);
}
//binary translate
public static final String get4DBinary(int magicInt)
{
String str = Integer.toBinaryString(magicInt);
int size = str.length();
if( size < 4 )
{
for( int i = 4 - size; i > 0 ; i-- )
{
str = "0" + str;
}
}
else if( size > 4 )
{
str = str.substring(size - 4);
}
return str;
}
//**************************************************
public static final String getValidStr(String param_str)
{
return (getValidStr(param_str,""));
}
//**************************************************
public static final String getValidStr(String param_str, String param_default)
{
return (param_str != null ? (!param_str.trim().equals("") ? param_str.trim() : param_default) : param_default);
}
//**************************************************
public static final String getHexToDec(String HexStr)
{
if (HexStr.length() < 6)
{
for (int i=1;i<=(6-HexStr.length());i++)
{
HexStr = HexStr + "F";
}
}
return Integer.toString(getH2D(HexStr.substring(0,1))*16+getH2D(HexStr.substring(1,2)));
}
//**************************************************
public static final int getH2D(String Hex00)
{
int k = 0;
char[] buf = Hex00.toCharArray();
for( int i = 0; i < buf.length; i++ )
{
int n = getH2D(buf[i]);
int m = buf.length - i;
m = (0x10*(m-1)*n) + (m==1?n:0);
k += m;
//System.out.println(k + " = "+n+"*"+m);
}
return k;
}
public static final int getH2D(char Hex00)
{
if (Hex00 == 'A' || Hex00 == 'a' ) return 10;
else if (Hex00 == 'B' || Hex00 == 'b' ) return 11;
else if (Hex00 == 'C' || Hex00 == 'c' ) return 12;
else if (Hex00 == 'D' || Hex00 == 'd' ) return 13;
else if (Hex00 == 'E' || Hex00 == 'e' ) return 14;
else if (Hex00 == 'F' || Hex00 == 'f' ) return 15;
return Integer.parseInt(String.valueOf(Hex00) );
}
//**************************************************
public static long getSeed(String s1)
{
long seed = 0L;
for (int i=0;i<s1.length()-1;i++) {
seed += s1.charAt(i) * s1.charAt(i+1);
}
while (seed < 0x2710) {
seed <<= 2;
}
return seed;
}
/*
* Javascript Split function
*/
public static String[] split(String str, String s)
{
int count = 0;
int i = 0, bi = 0, ei = 0;
while(ei != -1)
{
ei = str.indexOf(s,bi);
bi = ei + 1;
count++;
}
ei = 0;
bi = 0;
String[] splits = new String[count];
while( ei != -1 )
{
ei = str.indexOf(s,bi);
if( ei == -1 )
{
splits[i++] = str.substring(bi);
}
else
{
splits[i++] = str.substring(bi, ei);
}
bi = ei + 1;
}
return splits;
}
/*
* Copy byte array to a file
*/
public static boolean copyByteArray(byte[] source, byte[] target)
{
if( source == null || target == null )
return false;
if( source.length > target.length )
{
for( int i = 0; i < target.length; i++ )
target[i] = source[i];
return true;
}
for( int i = 0; i < source.length; i++ )
target[i] = source[i];
return true;
}
/**
* 将 iso-8859-1 串转换成本地串
* @param s, 输入串
* @return 新的字符串
*/
public static final String toLocal(String source)
{
String target = getValidStr(source);
try
{
target = new String(source.getBytes(),"iso-8859-1");
}
catch(Exception e)
{
}
return target;
}
public static final void intToBytes(int s, byte[] buf, int offset, int length)
{
for (int i = length-1; i >= 0; i--)
{
//按位运算
if( s != 0 )
{
buf[offset+i] = (byte) (s & 0xFF);
s >>= 8;
}
else
{
buf[offset+i] = 0;
}
}
}
public static final void intToBytes(int s, byte[] buf)
{
for (int i = buf.length-1; i >= 0; i--)
{
buf[i] = (byte) (s & 0xFF);
s >>= 8;
if (s == 0) break;
}
}
/**
* 将整形数转换成为字节数
* @param s
* @return
*/
public static final byte[] intToBytes(int s, int size)
{
byte[] buf = new byte[size];
for (int i = size-1; i >= 0; i--)
{
buf[i] = (byte) (s & 0xFF);
s >>= 8;
if (s == 0) break;
}
return buf;
}
public static final byte[] intToBytes(int s)
{
return intToBytes(s, 4);
}
/**
* 将字节流转换成无符号的短整形
* @param b
* @return
*/
public static final short bytesToShort(byte[] b)
{
return (short)bytesToInt(b);
}
/**
* 将字节流转换成整形数
* @param b
* @return
*/
public static final int bytesToInt(byte[] b)
{
int s = 0, count = b.length;
for (int pos = 0; pos < b.length ; pos++)
{
int iTemp = b[pos]<0?0x100+b[pos]:b[pos];
s += iTemp << (8*(b.length-pos-1));
}
return s;
}
/**
*
* @param b数据缓存
* @param offset从数据的那个位置开始处理
* @param length处理数据的长度
* @return
*/
public static final int bytesToInt(byte[] b, int offset, int length)
{
int s = 0;
for( int i = 0; i < length; i++ )
{
int iTemp = b[offset+i]<0?0x0100+b[offset+i]:b[offset+i];
s += iTemp << (8*(length-i-1));
}
return s;
}
/**
*
* @param buffer
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -