常用JAVA工具類

2021-09-01 14:00:47 字數 3713 閱讀 3683

獲取訪問者ip

/*** 獲取訪問者ip

}獲取使用者瀏覽器型別

/*** 獲取使用者瀏覽器型別

獲取時間差

/*** 獲取相差n月的月份的第一天或最後一天,格式:yyyymmdd

** @param diff

* 月份差值

* @param type

* ('first':該月第一天,"last":該月最後一天)

* @return yyyymmdd

*/public static string getdaybydiff( int diff, string type) else

return new ******dateformat( "yyyy-mm").format(calendar.gettime()) + "-"

+ day;

}/**

* 獲取 -n或+n個年的年份yyyy

** @param diff

* @return

*/public static string getyearbydiff( int diff)

/*** 獲取 -n或+n個月的日期,格式:yyyy -mm

** @param diff

* 月份差值

* @return yyyy- mm

*/public static string getmonthbydiff( int diff)

獲取前兩個月的時間

/*** 獲取前兩個月的時間

** @param upmonthtime

* (格式:2011 -09)

* @return

*/public static string getupthreetime(string upmonthtime) else if ( "02".equals(upmonthtime.subsequence(5, 7))) else if (querymonth > 2 && querymonth < 12) else

return upquerydatestring;

}擷取、合併陣列

/*** 擷取子陣列

** @param array

* @param startindexinclusive

* @param endindexexclusive

* @return

*/public static byte subarray( byte array, int startindexinclusive,

int endindexexclusive)

if (startindexinclusive < 0)

if (endindexexclusive > array. length)

int newsize = endindexexclusive - startindexinclusive;

if (newsize <= 0)

byte subarray = new byte[newsize];

system. arraycopy(array, startindexinclusive, subarray, 0, newsize);

return subarray;

}/**

* 合併byte陣列

** @param array1

* @param array2

* @return

*/public static byte addall( byte array1, byte... array2) else if (array2 == null)

byte joinedarray = new byte[array1. length + array2. length];

system. arraycopy(array1, 0, joinedarray, 0, array1. length);

system. arraycopy(array2, 0, joinedarray, array1.length, array2.length);

return joinedarray;

}時間格式

/*** 根據指定的日期格式生成日期時間串

** @param format

* 日期格式,如yyyymmdd

* @return datetimestring

*/public static string formatdatetime(date date, string format)

/*** 將給定的日期格式化為"yyyy -mm -dd hh:mm:ss"

** @param date

* 日期

* @return 格式為"yyyy- mm- dd hh:mm:ss"的string型別的時間串

*/public static string formatdatetime(date date)

/*** 根據指定的格式返回當前日期時間

** @param format

* 指定的日期格式

* @return string型別的時間

*/public static string formatdatetime(string format)

/*** 返回當前標準的日期時間

** @return 格式為"yyyy- mm- dd hh:mm:ss"的當前時間

*/public static string formatdatetime()

轉碼/**

* 編碼轉換iso8859 -1 -> utf -8

** @param source

* @return

* @throws unsupportedencodingexception

*/public static string isotoutf8(string source)

throws unsupportedencodingexception

/*** 編碼轉換gb2312 -> iso8859 -1

** @param source

* @return

* @throws unsupportedencodingexception

*/public static string gb2312toiso(string source)

throws unsupportedencodingexception

/*** 編碼轉換iso8859 -1 -> gb2312

** @param source

* @return

* @throws unsupportedencodingexception

*/public static string isotogb2312(string source)

throws unsupportedencodingexception

Java工具類 Java檔案工具類

public class fileutils 讀取檔案並作為byte返回 param file 目標檔案 return throws ioexception public static byte readfileasbytes file file throws ioexception 讀取檔案並作為...

JAVA常用的util工具類

前言 日常開發中,我們需要封裝些基礎的方法來簡化我們的開發 本文是對一些常用的基礎方法進行總結,方便日後使用。更新.wanghaifeng 判斷是否空 param obj return public static boolean checknull object obj else if obj in...

常用的java程式設計工具類

物件轉位元組 序列化 param obj return public static byte objecttobyte object obj catch exception e finally catch ioexception e return bytes 位元組轉物件 反序列化 param by...