java 常用的格式转化代码

String转换Timestamp

	private Timestamp ConvertToTime(String value){
		try {   
			Timestamp ts = new Timestamp(System.currentTimeMillis());   
            ts = Timestamp.valueOf(value);   
            return ts;  
        } catch (Exception e) {   
            return null;  
        } 
	}

Timestamp转换String

private String ConverToString(Timestamp value){
		
		Timestamp ts = new Timestamp(System.currentTimeMillis());  
        String tsStr = "";  
        DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");  
        try {  
            //方法一  
            tsStr = sdf.format(value);  
            System.out.println(tsStr);  
            return tsStr;
            //方法二  
           /* tsStr = ts.toString();  
            System.out.println(tsStr);  */
        } catch (Exception e) {  
            e.printStackTrace();  
            return null;  
        }  
	}

String转double

private double ConvertStrToDouble(String value){
		try{
			return Double.parseDouble(value);
		}catch(Exception ex){
			return 0;
		}
	 }

String转float,int ,BigDecimal

private float ConvertStrToFloat(String value){
		try{
			return Float.parseFloat(value);
		}catch(Exception ex){
			return 0;
		}
	}
	  
	private int  ConvertStrToInteger(String value){
		try{
			return  Integer.parseInt(value);
		}catch(Exception ex){
			return 0;
		}
	}
	
	private BigDecimal ConvertStrToBigDecimal(String value){
		try{
			BigDecimal bd = new BigDecimal(value);
			return  bd;
		}catch(Exception ex){
			return null;
		}
	}

猜你喜欢

转载自blog.csdn.net/qq_40216244/article/details/83117652