潭州Java中级班(day_15)-Date,包装类,Math,Random
1.Date
package StringStringbufferStringbuilder; import java.text.SimpleDateFormat; import java.util.Date; public class demoDate { public static void main(String[] args) { Date date=new Date(); System.out.println(date); long lg=date.getTime(); //返回自1970年1月1日以来,由此 Date对象表示的00:00:00 GMT的毫秒 数 。 System.out.println("lg="+lg);//1537194482148 int it=date.getDate();//代表几号 System.out.println("it="+it);//17 int month=date.getMonth()+1;//8,从0开始 System.out.println("month="+month); /* * 格式化时间: * yyyy年MM月dd日 星期 hh:mm:ss */ /* * 创建格式化时间的对象 */ SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日 E hh:mm:ss"); //格式化时间,date变成字符串型 String time=sdf.format(date); System.out.println("time="+time); } }
2.包装类
* 基本数据类型 对应的包装类(字段,方法,构造方法)
* byte Byte
* short Short
* char Character
* int Integer
* long Long
* float Float
* double Double
* boolean Boolean
* 为什么要学基本数据类型对应的包装类呢。
* 让基本数据类型与String互转
*
* QQ账号都是数字,你们可能理解为从我们输入的账号获取的值就是整数。
* 其实不是,都是字符串,从文本框获取的值都是字符串。
*
*
*
* NumberFormatException:数字格式化异常。
*/
package StringStringbufferStringbuilder; /* * 基本数据类型 对应的包装类(字段,方法,构造方法) * byte Byte * short Short * char Character * int Integer * long Long * float Float * double Double * boolean Boolean * 为什么要学基本数据类型对应的包装类呢。 * 让基本数据类型与String互转 * * QQ账号都是数字,你们可能理解为从我们输入的账号获取的值就是整数。 * 其实不是,都是字符串,从文本框获取的值都是字符串。 * * * * NumberFormatException:数字格式化异常。 */ public class DemoIntegerCharacterDoubleFloatBooleanByteShort { public static void main(String[] args) { //test1(); test2(); } //自动装箱和自动拆箱 public static void test1() { Integer integer=new Integer(10);//会自动装箱,自己会new System.out.println("integer="+integer); int a=integer;//自动拆箱,自己会调用intValue()方法 System.out.println("a="+a); } public static void test2() { int a=Integer.compare(20, 20);//如果前面大于后面返回1,小于返回-1,等于返回0. System.out.println("a="+a); int b=Integer.compare(20, 30); System.out.println("b="+b); int c=Integer.compare(30, 20); System.out.println("c="+c); Integer integer=30; Integer integer2=30; //如果前面大于后面返回1,小于返回-1,等于返回0. int num1=integer.compareTo(integer2); System.out.println("num="+num1); int num2=integer.hashCode(); System.out.println("num2的哈希值:"+num2); //字符串数字变成int型的数字 String qq="3535"; int d=Integer.parseInt(qq); System.out.println("d="+d); } }
三.Math
package StringStringbufferStringbuilder; public class demoMath { public static void main(String[] args) { testFields(); //testMethod(); } public static void testMethod(){ double d=Math.abs(-1000.34);//变正数 System.out.println("d="+d); double du=Math.max(18, 10); System.out.println("du="+du); //返回值为 double值为正号,大于等于 0.0 ,小于 1.0 。 Random double d2=Math.random(); System.out.println("d2="+d2); //返回与参数最接近值的 double值,并且等于数学整数。 double d3=Math.rint(4.46); System.out.println("d3="+d3);//5.0 //返回参数中最接近的 long ,其中 long四舍五入为正无穷大。 long lg=Math.round(4.56); System.out.println("lg="+lg); double d4=Math.ceil(3.1456);//大于等于该值得整数 System.out.println("d4="+d4); double d5=3.1456787; System.out.println("d5="+d5); float ft=(float) d5; System.out.println("ft="+ft); } public static void testFields(){ double e=Math.E; System.out.println("e="+e); double π=Math.PI; System.out.println("π="+π); } }
四.Random
package StringStringbufferStringbuilder; import java.util.Random; /* * Random类的实例用于生成伪随机数的流。 */ public class demoRandom { public static void main(String[] args) { Random random=new Random(); int num=random.nextInt(); System.out.println("num="+num); //10以内的整数,不包括10 int num2=random.nextInt(10); System.out.println("num2="+num2); long num3=random.nextLong(); System.out.println("num3="+num3); double num4=random.nextGaussian(); System.out.println("num4="+num4); } }