com.jfimagine.utils.commonutil
Class CommonUtil

java.lang.Object
  |
  +--com.jfimagine.utils.commonutil.CommonUtil

public class CommonUtil
extends java.lang.Object

Common utilities for data converting, datetime processing and others.

Version:
$Revision: 1.1 $
Author:
CookieMaker

Constructor Summary
CommonUtil()
           
 
Method Summary
static java.lang.String appendStr(java.lang.String str, boolean headChar, char ch, int len)
          Continuous add a letter at the begining/end of a String, untill the length of the String equals to len
static int b2i(boolean b)
          Convert a boolean value to an int one
static java.lang.String byteToHex(byte[] byteAry)
          Convert a byte array to a 2-chars hex string
static boolean chkValidInt(java.lang.Integer i)
          Check if an Integer object value has valid or invalid value
static boolean chkValidStr(java.lang.String s)
          Check if an String value has valid or invalid value
static java.lang.String d2s(java.util.Date d)
          Convert a date to String
static java.lang.String dt2s(java.util.Date d)
          Convert a datetime to String
static java.lang.String f2fs(double d)
          Format a double value to string
static java.lang.String f2fs(double d, int scale)
          Format a double value to string
static java.lang.String f2s(double d)
          Convert a double value to a string
static java.util.Date firstDayOfTheMonth(java.util.Date d)
          Get the first Day of a month which the date exists
static java.util.Date firstDayOfTheYear(java.util.Date d)
          Get the first Day of a year which the date exists
static java.util.Date firstDayOfThisMonth()
          Get the first Day of this month
static java.util.Date firstDayOfThisYear()
          Get the first day of this Year
static java.lang.String format(double d, int precision, int scale)
          Format a double value to string
static int getCharCount(java.lang.String inputStr, char ch)
          Find the number of a char that occurs in the string.
static java.util.Date getDate(java.util.Date curDate, int offset)
          Increase or decrease date by offset
static java.util.Date getDate(int year, int month, int day)
          Encode date from Year,Month and Day
static int getDay(java.util.Date d)
          Get the day part from a date
static int getHour(java.util.Date d)
          Get the hour part from a date
static int getMillisecond(java.util.Date d)
          Get the millisecond part from a date
static int getMinute(java.util.Date d)
          Get the minute part from a date
static int getMonth(java.util.Date d)
          Get the month part from a date
static int getSecond(java.util.Date d)
          Get the second part from a date
static java.lang.String getSystemProperty(java.lang.String inKey)
          Get a system property by a key.
static int getYear(java.util.Date d)
          Get the year part from a date
static byte[] hexToByte(java.lang.String hexStr)
          Convert a hex string to a byte array.
static java.lang.String hexToStr(java.lang.String hexStr)
          Convert a 2-chars hex string to a normal string
static boolean i2b(int i)
          Convert an int value to a boolean one
static java.lang.String i2s(int i)
          Convert an int value to String
static java.lang.String i2s(java.lang.Integer i)
          Convert an Integer object value to String
static boolean isURLFile(java.lang.String fileName)
          Check if a specified file name is a URL file name
static java.util.Date lastDayOfTheMonth(java.util.Date d)
          Get the last Day of a month which the date exists
static java.util.Date lastDayOfTheYear(java.util.Date d)
          Get the last day of a year which the date exists
static java.util.Date lastDayOfThisMonth()
          Get the last Day of this month
static java.util.Date lastDayOfThisYear()
          Get the last day of this Year
static char newLine()
          return a netLine() letter (not all platforms use '\n' for a newline)
static java.util.Date now_date()
          Get system's current date
static java.util.Date now_time()
          Get system's current time
static java.util.Date now()
          Get system's current datetime
static java.lang.String o2s(java.lang.Object o)
          Convert an object to a string
static java.util.Date s2d(java.lang.String s)
          Convert a string to a date value
static java.util.Date s2dt(java.lang.String s)
          Convert a string to a datetime value
static double s2f(java.lang.String s)
          Convert a String to a double value
static int s2i(java.lang.String s)
          Convert a String to an int value
static java.util.Date s2t(java.lang.String s)
          Convert a string to a time value
static java.lang.String strToHex(java.lang.String str)
          Convert a normal string to a a 2-chars hex string
static java.lang.String t2s(java.util.Date d)
          Convert a time to String
static java.lang.String validChnStr(java.lang.String str)
          Correct problems in Chinese letters
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CommonUtil

public CommonUtil()
Method Detail

isURLFile

public static boolean isURLFile(java.lang.String fileName)
Check if a specified file name is a URL file name

Parameters:
fileName - The file name to be checked.

newLine

public static char newLine()
return a netLine() letter (not all platforms use '\n' for a newline)

Returns:
A new Line letter

getCharCount

public static int getCharCount(java.lang.String inputStr,
                               char ch)
Find the number of a char that occurs in the string.

Parameters:
inputStr - An input string to be measured.
ch - A char to be found.
Returns:
The number of occurrence.

validChnStr

public static java.lang.String validChnStr(java.lang.String str)
Correct problems in Chinese letters

Parameters:
str - String to be corrected
Returns:
String corrected

appendStr

public static java.lang.String appendStr(java.lang.String str,
                                         boolean headChar,
                                         char ch,
                                         int len)
Continuous add a letter at the begining/end of a String, untill the length of the String equals to len

Parameters:
str - String to be expend
headChar - Adding chars at begining or end of a string
ch - Character to be added
Returns:
A new expended string

strToHex

public static java.lang.String strToHex(java.lang.String str)
Convert a normal string to a a 2-chars hex string

Parameters:
str - Original string.
Returns:
the hex string.

hexToStr

public static java.lang.String hexToStr(java.lang.String hexStr)
Convert a 2-chars hex string to a normal string

Parameters:
hexStr - Original hexstring.
Returns:
the normal string.

byteToHex

public static java.lang.String byteToHex(byte[] byteAry)
Convert a byte array to a 2-chars hex string

Parameters:
byteAry - the byte array
Returns:
the hex string.

hexToByte

public static byte[] hexToByte(java.lang.String hexStr)
Convert a hex string to a byte array.

Parameters:
hexStr - The hex string.
Returns:
the byte array.

now

public static java.util.Date now()
Get system's current datetime

Returns:
A datetime value

now_date

public static java.util.Date now_date()
Get system's current date

Returns:
A date value

now_time

public static java.util.Date now_time()
Get system's current time

Returns:
A time value

firstDayOfTheYear

public static java.util.Date firstDayOfTheYear(java.util.Date d)
Get the first Day of a year which the date exists

Parameters:
d - the date within a year
Returns:
A date value represents the first day of a year

lastDayOfTheYear

public static java.util.Date lastDayOfTheYear(java.util.Date d)
Get the last day of a year which the date exists

Parameters:
d - the date within a year
Returns:
A date value represents the last day of a year

firstDayOfThisYear

public static java.util.Date firstDayOfThisYear()
Get the first day of this Year

Returns:
A date value represents the first day of this year

lastDayOfThisYear

public static java.util.Date lastDayOfThisYear()
Get the last day of this Year

Returns:
A date value represents the last day of this year

firstDayOfTheMonth

public static java.util.Date firstDayOfTheMonth(java.util.Date d)
Get the first Day of a month which the date exists

Parameters:
d - the date within a month
Returns:
A date value represents the first day of a month

lastDayOfTheMonth

public static java.util.Date lastDayOfTheMonth(java.util.Date d)
Get the last Day of a month which the date exists

Parameters:
d - the date within a month
Returns:
A date value represents the last day of a month

firstDayOfThisMonth

public static java.util.Date firstDayOfThisMonth()
Get the first Day of this month

Returns:
A date value represents the first day of this month

lastDayOfThisMonth

public static java.util.Date lastDayOfThisMonth()
Get the last Day of this month

Returns:
A date value represents the last day of this month

getDate

public static java.util.Date getDate(int year,
                                     int month,
                                     int day)
Encode date from Year,Month and Day

Parameters:
year - the Year part of a date
month - the Month part of a date
day - the Day part of a date
Returns:
A date value encoded by the parameters

getDate

public static java.util.Date getDate(java.util.Date curDate,
                                     int offset)
Increase or decrease date by offset

Parameters:
curDate - the date value to be increased or decreased
offset - the number of days, be negative value if decrease.
Returns:
A date value adjusted

getYear

public static int getYear(java.util.Date d)
Get the year part from a date

Parameters:
d - A date value to be extracted
Returns:
the year part

getMonth

public static int getMonth(java.util.Date d)
Get the month part from a date

Parameters:
d - A date value to be extracted
Returns:
the month part

getDay

public static int getDay(java.util.Date d)
Get the day part from a date

Parameters:
d - A date value to be extracted
Returns:
the day part

getHour

public static int getHour(java.util.Date d)
Get the hour part from a date

Parameters:
d - A date value to be extracted
Returns:
the hour part

getMinute

public static int getMinute(java.util.Date d)
Get the minute part from a date

Parameters:
d - A date value to be extracted
Returns:
the minute part

getSecond

public static int getSecond(java.util.Date d)
Get the second part from a date

Parameters:
d - A date value to be extracted
Returns:
the second part

getMillisecond

public static int getMillisecond(java.util.Date d)
Get the millisecond part from a date

Parameters:
d - A date value to be extracted
Returns:
the millisecond part

d2s

public static java.lang.String d2s(java.util.Date d)
Convert a date to String

Parameters:
d - A date value to be converted
Returns:
a string formatted as 'YYYY-MM-DD', used to represent a date

t2s

public static java.lang.String t2s(java.util.Date d)
Convert a time to String

Parameters:
d - A time value to be converted
Returns:
a string formatted as 'hh:mm:ss', used to represent a time

dt2s

public static java.lang.String dt2s(java.util.Date d)
Convert a datetime to String

Parameters:
d - A datetime value to be converted
Returns:
a string formatted as 'YYYY-MM-DD hh:mm:ss', used to represent a datetime

s2d

public static java.util.Date s2d(java.lang.String s)
Convert a string to a date value

Parameters:
s - A string formatted as 'YYYY-MM-DD', represents a date value
Returns:
a Date value, null if invalid input

s2t

public static java.util.Date s2t(java.lang.String s)
Convert a string to a time value

Parameters:
s - A string formatted as 'hh:mm:ss', represents a time value
Returns:
a time value, null if invalid input

s2dt

public static java.util.Date s2dt(java.lang.String s)
Convert a string to a datetime value

Parameters:
s - A string formatted as 'YYYY-MM-DD hh:mm:ss', represents a datetime value
Returns:
a datetime value, null if invalid input

b2i

public static int b2i(boolean b)
Convert a boolean value to an int one

Parameters:
b - a boolean value to be converted
Returns:
a int value represents a boolean one, 1 if true, 0 if false

i2b

public static boolean i2b(int i)
Convert an int value to a boolean one

Parameters:
i - an int value to be converted
Returns:
a boolean value represents a int one, true if 1, false if 0

s2i

public static int s2i(java.lang.String s)
Convert a String to an int value

Parameters:
s - a string to be converted
Returns:
an int value converted, 0 if invalid

i2s

public static java.lang.String i2s(int i)
Convert an int value to String

Parameters:
i - an int value to be converted
Returns:
a string value converted

i2s

public static java.lang.String i2s(java.lang.Integer i)
Convert an Integer object value to String

Parameters:
i - an Integer object value to be converted
Returns:
a string value converted,null if invalid

s2f

public static double s2f(java.lang.String s)
Convert a String to a double value

Parameters:
s - a string to be converted
Returns:
a double value converted, 0 if invalid

f2s

public static java.lang.String f2s(double d)
Convert a double value to a string

Parameters:
d - a double value to be converted
Returns:
a string converted

o2s

public static java.lang.String o2s(java.lang.Object o)
Convert an object to a string

Parameters:
o - an object to be converted
Returns:
a string converted, null if invalid or unconvertible

format

public static java.lang.String format(double d,
                                      int precision,
                                      int scale)
                               throws java.lang.Exception
Format a double value to string

Parameters:
d - a double value to be formatted
precision - a precision for length of all digits
scale - digits count of the fraction partition
Returns:
a string formatted
java.lang.Exception

f2fs

public static java.lang.String f2fs(double d)
Format a double value to string

Parameters:
d - a double value to be formatted
Returns:
a string formatted as xxx.xx, use default precistion as 15 and default scale as 2

f2fs

public static java.lang.String f2fs(double d,
                                    int scale)
Format a double value to string

Parameters:
d - a double value to be formatted
scale - digits count of the fraction partition
Returns:
a string formatted as xxx.xx, default precistion=15

chkValidInt

public static boolean chkValidInt(java.lang.Integer i)
Check if an Integer object value has valid or invalid value

Parameters:
i - an Integer object to be checked
Returns:
a boolean result, false if null or zero value, true if not zero

chkValidStr

public static boolean chkValidStr(java.lang.String s)
Check if an String value has valid or invalid value

Parameters:
s - a String to be checked
Returns:
a boolean result, false if null or empty string, true if not empty

getSystemProperty

public static java.lang.String getSystemProperty(java.lang.String inKey)
Get a system property by a key.

Parameters:
inKey - A system property key.


Copyright 2004 ~ 2005 JingFei International Cooperation LTD. All Rights Reserved.