posts - 48,  comments - 5,  trackbacks - 0

package com.cskj.marketSystem.common;

 

/**

 * @author weishaoxiang

 * @功能JAVA数据类型转换

 * @QQ:297187963

 * @Email: weishaoxiang@163.com

 * @Web:http://www.blogjava.net/tylz7758

 * @date 2010-10-01

 * @version 1.0

 */

public class JavaTypeChange {

    /* 自动类型转换,低级变量可以直接转换为高级变量。 */

    /**

     * @功能 change the byte type to the short type

     * @param byteValue

     *            单个的字节或者是数值

     * @return short

     * @说明 自动类型转换

     */

    public static short byteToShort( byte byteValue ){

       // Byte by = new Byte(byteValue);

       // return by.shortValue();

       return byteValue;

    }

   

    /**

     * @功能 change the byte type to the char type

     * @param byteValue

     *            单个的字节或者是ASCII码数值

     * @return char

     * @说明 将单个的字节转换成字符,只是数据类型上的变换,输入没有改变。

     * @说明 如果是ASCII码数值,将先转换成为byte类型

     */

    public static char byteToChar( byte byteValue ){

       return ( char ) byteValue;

    }

   

    /**

     * @功能 change the byte type to the int type

     * @param byteValue

     *            单个的字节或者是ASCII码数值

     * @return int

     * @说明 自动类型转换

     */

    public static int byteToInt( byte byteValue ){

       // Byte by = new Byte(byteValue);

       // return by.intValue();

       return byteValue;

    }

   

    /**

     * @功能 change the byte type to the long type

     * @param byteValue

     *            单个的字节或者是ASCII码数值

     * @return long

     * @说明 自动类型转换

     */

    public static long byteToLong( byte byteValue ){

       // Byte by = new Byte(byteValue);

       // return by.longValue();

       return byteValue;

    }

   

    /**

     * @功能 change the byte type to the double type

     * @param byteValue

     *            单个的字节或者是ASCII码数值

     * @return double

     * @说明 自动类型转换

     */

    public static double byteToDouble( byte byteValue ){

       // Byte by = new Byte(byteValue);

       // return by.doubleValue();

       return byteValue;

    }

   

    /**

     * @功能 change the byte type to the String type

     * @param byteValue

     *            单个的字节或者是ASCII码数值

     * @return String

     * @说明 输出时都会自动转换为int整形

     */

    public static String byteToString( byte byteValue ){

       // return String.valueOf(byteValue);

       Byte by = new Byte( byteValue );

       return by.toString( );

       // return Byte.toString(byteValue);

    }

   

    public static String byteToString( byte[] byteValue ){

       String str = "";

       for( int i = 0; i < byteValue.length; i++ ){

           str += String.valueOf(( char ) byteValue[i]);

       }

       return str;

    }

   

    /**

     * @功能 change the char type to the byte type

     * @param charValue

     *            单个的字符或者是ASCII码数值

     * @return byte

     * @说明 输出时都会自动转换为int整形

     */

    public static byte charToByte( char charValue ){

       return ( byte ) charValue;

    }

   

    /**

     * @功能 change the char type to the short type

     * @param charValue

     *            单个的字符或者是ASCII码数值

     * @return short

     * @说明

     */

    public static short charToShort( char charValue ){

       return ( short ) charValue;

    }

   

    /**

     * @功能 change the char type to the int type

     * @param charValue

     *            单个的字符或者是ASCII码数值

     * @return int

     * @说明 自动类型转换

     */

    public static int charToInt( char charValue ){

       return charValue;

    }

   

    /**

     * @功能 change the char type to the long type

     * @param charValue

     *            单个的字符或者是ASCII码数值

     * @return long

     * @说明 自动类型转换

     */

    public static long charToLong( char charValue ){

       return charValue;

    }

   

    /**

     * @功能 change the char type to the float type

     * @param charValue

     *            单个的字符或者是ASCII码数值

     * @return float

     * @说明 自动类型转换

     */

    public static float charToFloat( char charValue ){

       return charValue;

    }

   

    /**

     * @功能 change the char type to the double type

     * @param charValue

     *            单个的字符或者是ASCII码数值

     * @return double

     * @说明 自动类型转换

     */

    public static double charToDouble( char charValue ){

       return charValue;

    }

   

    /**

     * @功能 change the char type to the String type

     * @param charValue

     *            单个的字符或者是ASCII码数值

     * @return String

     * @说明

     */

    public static String charToString( char charValue ){

       return String.valueOf( charValue );

       // return Character.toString(charValue);

    }

   

    /**

     * @功能 change the short type to the byte type

     * @param shortValue

     *            短整形数值

     * @return byte

     * @说明 强制类型转换

     */

    public static byte shortToByte( short shortValue ){

       // Short sh = new Short(shortValue);

       // return sh.byteValue();

       return ( byte ) shortValue;

    }

   

    /**

     * @功能 change the short type to the char type

     * @param shortValue

     *            短整形数值

     * @return char

     * @说明 强制类型转换

     */

    public static char shortToChar( short shortValue ){

       return ( char ) shortValue;

    }

   

    /**

     * @功能 change the short type to the int type

     * @param shortValue

     *            短整形数值

     * @return int

     * @说明 自动类型转换

     */

    public static int shortToInt( short shortValue ){

       // Short sh = new Short(shortValue);

       // return sh.intValue();

       return shortValue;

    }

   

    /**

     * @功能 change the short type to the long type

     * @param shortValue

     *            短整形数值

     * @return long

     * @说明 自动类型转换

     */

    public static long shortToLong( short shortValue ){

       // Short sh = new Short(shortValue);

       // return sh.longValue();

       return shortValue;

    }

   

    /**

     * @功能 change the short type to the float type

     * @param shortValue

     *            短整形数值

     * @return float

     * @说明 自动类型转换

     */

    public static float shortToFloat( short shortValue ){

       // Short sh = new Short(shortValue);

       // return sh.floatValue();

       return shortValue;

    }

   

    /**

     * @功能 change the short type to the double type

     * @param shortValue

     *            短整形数值

     * @return double

     * @说明 自动类型转换

     */

    public static double shortToDouble( short shortValue ){

       // Short sh = new Short(shortValue);

       // return sh.doubleValue();

       return shortValue;

    }

   

    /**

     * @功能 change the short type to the String type

     * @param shortValue

     *            短整形数值

     * @return String

     * @说明

     */

    public static String shortToString( short shortValue ){

       return String.valueOf( shortValue );

       // Short sh = new Short(shortValue);

       // return sh.toString();

       // return Short.toString(shortValue);

    }

   

    public static byte intToByte( int intValue ){

       // Integer integer = new Integer(intValue);

       // return integer.byteValue();

       return ( byte ) intValue;

    }

   

    public static short intToShort( int intValue ){

       // Integer integer = new Integer(intValue);

       // return integer.shortValue();

       return ( short ) intValue;

    }

   

    public static char intToChar( int intValue ){

       return ( char ) intValue;

    }

   

    public static long intToLong( int intValue ){

       // Integer integer = new Integer(intValue);

       // return integer.longValue();

       return intValue;

    }

   

    public static float intToFloat( int intValue ){

       // Integer integer = new Integer(intValue);

       // return integer.floatValue();

       return intValue;

    }

   

    public static double intToDouble( int intValue ){

       // Integer integer = new Integer(intValue);

       // return integer.doubleValue();

       return intValue;

    }

   

    /**

     * @功能 change int type to the string type

     * @param intValue

     *            整型数据值

     * @return String

     * @说明 有三种转换的方法

     */

    public static String intToString( int intValue ){

       /* 第一种方法: */

       String intstr = String.valueOf( intValue );

       /* 第二种方法: */

       // String intstr = Integer.toString(value);

       /* 第三种方法: */

       // String intstr = "" + value;

       return intstr;

    }

   

    public static byte longToByte( long longValue ){

       return ( byte ) longValue;

    }

   

    public static short longToShort( long longValue ){

       return ( short ) longValue;

    }

   

    public static char longToChar( long longValue ){

       return ( char ) longValue;

    }

   

    public static int longToInt( long longValue ){

       return ( int ) longValue;

    }

   

    public static float longTofloat( long longValue ){

       return longValue;

    }

   

    public static double longTodouble( long longValue ){

       return longValue;

    }

   

    public static String longToString( long longValue ){

       return String.valueOf( longValue );

       // return Long.toString(longValue);

    }

   

    public static byte floatToByte( float floatValue ){

       return ( byte ) floatValue;

    }

   

    public static short floatToshort( float floatValue ){

       return ( short ) floatValue;

    }

   

    public static char floatToChar( float floatValue ){

       return ( char ) floatValue;

    }

   

    public static int floatToInt( float floatValue ){

       return ( int ) floatValue;

    }

   

    public static long floatTolong( float floatValue ){

       return ( long ) floatValue;

    }

   

    public static double floatToDouble( float floatValue ){

       return floatValue;

    }

   

    /**

     * @功能 change the float type to the string type

     * @param floatValue

     *            浮点型数据值

     * @return String

     * @说明 有三种转换的方法

     */

    public static String floatToString( float floatValue ){

       /* 第一种方法: */

       String floatstr = String.valueOf( floatValue );

       /* 第二种方法: */

       // String floatstr = Float.toString(floatValue);

       /* 第三种方法: */

       // String floatstr = "" + floatValue;

       return floatstr;

    }

   

    public static byte doubleToDouble( double doubleValue ){

       return ( byte ) doubleValue;

    }

   

    public static short doubleToShort( double doubleValue ){

       return ( short ) doubleValue;

    }

   

    public static char doubleToChar( double doubleValue ){

       return ( char ) doubleValue;

    }

   

    public static int doubleToInt( double doubleValue ){

       return ( int ) doubleValue;

    }

   

    public static long doubleToLong( double doubleValue ){

       return ( long ) doubleValue;

    }

   

    public static float doubleToFloat( double doubleValue ){

       return ( float ) doubleValue;

    }

   

    public static String doubleToString( double doubleValue ){

       return String.valueOf( doubleValue );

       // return Double.toString(doubleValue);

    }

   

    /**

     * @功能 change the string type to the byte type

     * @param string

     *            输入参数必须是 -128127之间的数值型字符串

     * @return byte 相应参数值的bype类型

     * @说明 这个方法基本上不用

     */

    public static byte stringToByte( String string ){

       Byte by = new Byte( string );

       return by.byteValue( );

       // return Byte.parseByte(string);

    }

   

    /**

     * @功能 change the string type to the byte[] type

     * @param string

     *            字符串

     * @return byte[]

     * @说明 将自动转换成ASCII码值

     */

    public static byte[] stringToByteArray( String string ){

       return string.getBytes( );

    }

   

    /**

     * @功能 change the string type to the short type

     * @param string

     *            输入参数必须是 -3276832767之间的数值型字符串

     * @return short

     * @说明 一般情况下都不用short类型

     */

    public static short stringToShort( String string ){

       return new Short( string ).shortValue( );

       // return Short.parseShort(string);

    }

   

    /**

     * @功能 change the string type to the char[] type

     * @param string

     *            字符串,可以是多个字符

     * @return char[] 字符数组

     * @说明 将字符串转换成字符数组后,可以取单个字符

     */

    public static char[] stringToChar( String string ){

       return string.toCharArray( );

    }

   

    /**

     * @功能 change the string type to the int type

     * @param intstr

     *            整型的字符串形式

     * @return int

     * @说明 有两种转换的方法

     */

    public static int stringToInt( String intstr ){

       /* 第一种方法: */

       int integer = Integer.valueOf( intstr ).intValue( );

       /* 第二种方法: */

       // int integer = Integer.parseInt(intstr);

       return integer;

    }

   

    /**

     * @功能 change the string type to the long type

     * @param intstr

     *            长整型的字符串形式

     * @return long

     * @说明 有两种转换的方法

     */

    public static long stringToLong( String longstr ){

       /* 第一种方法: */

       long longs = Long.valueOf( longstr ).longValue( );

       /* 第二种方法: */

       // long longs = Long.parseLong(longstr);

       return longs;

    }

   

    /**

     * @功能 change the string type to the float type

     * @param floatstr

     *            浮点型的字符串形式

     * @return float

     * @说明 有两种转换的方法

     */

    public static float stringToFloat( String floatstr ){

       /* 第一种方法: */

       float floats = Float.valueOf( floatstr ).floatValue( );

       /* 第二种方法: */

       // float floats = Float.parseFloat(floatstr);

       return floats;

    }

   

    /**

     * @功能 change the string type to the double type

     * @param intstr

     *            双精度类型数的字符串形式

     * @return double

     * @说明 有两种转换的方法

     */

    public static double stringToDouble( String doublestr ){

       /* 第一种方法: */

       double doubles = Double.valueOf( doublestr ).doubleValue( );

       /* 第二种方法: */

       // double doubles = Double.parseDouble(doublestr);

       return doubles;

    }

   

    /**

     * @功能 change the string type to the sqlDate type

     * @param dateStr

     *            时间的字符串形式,格式为:"yyyy-MM-dd"

     * @return Date

     * @说明

     */

    public static java.sql.Date stringToDate( String dateStr ){

       return java.sql.Date.valueOf( dateStr.trim( ) );

    }

   

    /**

     * @功能 change the sqlDate type to the string type

     * @param java.sql.Date

     *            时间类的对象

     * @return String 时间的字符串形式

     * @说明

     */

    public static String dateToString( java.sql.Date date ){

       return date.toString( );

    }

}

 

posted on 2010-10-12 12:54 逍湘 阅读(504) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航:
 

留言簿(2)

随笔档案(49)

文章档案(17)

最新随笔

积分与排名

  • 积分 - 25217
  • 排名 - 1511

最新评论

阅读排行榜