zoukankan      html  css  js  c++  java
  • Java 类型转换

    类型转换

    public class CharToIntExample {
    	public static void main(String args[]) {
    		char c = 'a';
    		char c2 = '1';
    		int a = c;
    		int b = c2;
    		System.out.println(a);
    		System.out.println(b);
    		
    		int d=Character.getNumericValue(c2);  
    		System.out.println(d);  
    	}
    }
    
    import java.sql.Timestamp;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    public class DateToTimestampExample {
    	public static void main(String args[]) {
    		Date date = new Date();
    		Timestamp ts = new Timestamp(date.getTime());
    		System.out.println(ts);
    		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    		System.out.println(formatter.format(ts));
    	}
    }
    
    public class IntToStringExample1 {
    	public static void main(String args[]) {
    		int i = 200;
    		String s = String.format("%d", i);
    		System.out.println(s);
    	}
    }
    
    public class IntToStringExample2 {
    	public static void main(String args[]) {
    		int i = 200;
    		String s = Integer.toString(i);
    		System.out.println(i + 100);// 300 because + is binary plus operator
    		System.out.println(s + 100);// 200100 because + is string concatenation operator
    	}
    }
    
    import org.junit.Test;
    
    public class LongToStringExample {
    	@Test
    	public void test1() {
    		long i = 9993939399L;
    		String s = String.valueOf(i);
    		System.out.println(s);
    	}
    
    	@Test
    	public void test2() {
    		long i = 9993939399L;
    		String s = Long.toString(i);
    		System.out.println(s);
    	}
    }
    
    import org.junit.Test;
    
    public class ObjectToStringExample {
    	@Test
    	public void test() {
    		String s = "hello";
    		StringBuilder sb = new StringBuilder(s);
    		sb.reverse();
    		String rev = sb.toString();// converting StringBuilder to String
    		System.out.println("String is: " + s);
    		System.out.println("Reverse String is: " + rev);
    	}
    }
    
    import org.junit.Test;
    
    public class StringToFloatExample {
    	@Test
    	public void test() {
    		String s = "23.6";
    		float f = Float.parseFloat(s);
    		System.out.println(f);
    	}
    }
    
    public class StringToIntegerExample {
    	public static void main(String args[]) {
    		String s = "hello";
    		int i = Integer.parseInt(s);
    		System.out.println(i); // NumberFormatException
    	}
    }
    
    public class StringToLongExample {
    	public static void main(String args[]) {
    		String s = "9990449935";
    		long l = Long.parseLong(s);
    		System.out.println(l);
    	}
    }
    
    public class TestStringToInt {
    	public static void main(String[] args) {
    		int a = Integer.parseInt("123");
    		Integer integer = Integer.valueOf(123);
    		System.out.println(a + 100);
    		System.out.println(integer + 100);
    	}
    }
    
  • 相关阅读:
    echarts中label上下两行展示
    vue中去掉地址栏中的#
    vue中登录超时跳转到登录页面设置拦截器
    在table中,tbody没有充满整个table
    vant中dialog的使用
    水位波纹动画兼容ie8
    在vue中使用XLSX导出表格
    elementUI 选择开始结束日期加限制
    element table 合并同类项并输出后台返回数据
    将后台返回的月份201810,201809转换成正常的9月10月
  • 原文地址:https://www.cnblogs.com/hgnulb/p/10092088.html
Copyright © 2011-2022 走看看