常用类

来源:互联网 发布:网络直播培训机构 编辑:程序博客网 时间:2024/05/16 08:21

基本数据类型包装类

byte             Byte

short           Short

int                Integer

long             Long

char             Character

boolean      Boolean

float            Float

double        Double

以Integer为例

code:

package cn.river.integer;public class IntegerDemo {public static void main(String[] args) {// TODO Auto-generated method stubmethod_5();}//Integer细节问题public static void method_5(){/*Integer i = new Integer("123");Integer ii = new Integer("123");System.out.println(i==ii);System.out.println(i.equals(ii));*///自动装箱,自动拆箱JDK1.5新特性//自动装箱,就是将基本数据类型,封装成对象/*Integer x = 1;//Integer x = new Integer(1);int y = 2;//自动拆箱,将封装后的对象,转成基本数据类型System.out.println(x+y);//x.intValue()+y*//*Integer x = 1;//byte范围内,JVM不在开辟新空间Integer y = 1;System.out.println(x==y);System.out.println(x.equals(y));Integer xx = 128;//byte范围外,JVM开辟新空间Integer yy = 128;System.out.println(xx==yy);System.out.println(xx.equals(yy));*/  System.out.println(Integer.toString(3)+4);  System.out.println(Integer.toString(122,2));//2表示基数}//Integer的进制转换public static void method_4(){System.out.println(Integer.toBinaryString(15));System.out.println(Integer.toHexString(15));System.out.println(Integer.toOctalString(15));}//Integer的范围public static void method_3(){System.out.println(Integer.MAX_VALUE);System.out.println(Integer.MIN_VALUE);}//Integer的构造方法public static void method_1(){Integer i = new Integer(12);Integer ii = new Integer("123");}//字符串转换成int类型public static void method_2(){Integer ii = new Integer("123");Integer iii = Integer.parseInt("34");System.out.println(iii);//public static int parseInt(String s, int radix)radix为解析时使用的基数Integer iiii = Integer.parseInt("1100110",2);System.out.println(iiii);}}

Math类

|-- 静态的常量 圆周率 PI  double

|-- static int abs()绝对值

|-- static int max(int a,int b)返回较大的数

|-- static double ceil(double d) ***** 返回大于或者等于指定数最小数

|-- static double floor(double d)***** 返回小于或者等于指定数最大数

|-- static double pow(int x,int y)幂运算 x的y次幂

|-- static int round(double d) 四舍五入

|-- static double random()伪随机数 0.0-1.0

|-- 新的产生随机数的方式,用的是 Random类  类中的方法nextInt(参数就是随机数的范围)

|--导包,导入的是包中的类 java.util.Random; 

package cn.river.integer;import java.util.Random;public class MathDemo {public static void main(String[] args) {double d = Math.PI;System.out.println(d);System.out.println(Math.E);method_2();}//利用Random类获取100以内的随机数public static void method_2(){Random r  = new Random();for(int y=0; y<100; y++){int x = r.nextInt(100);System.out.println(x);}}//伪随机数public static void method_1(){for (int i = 0; i < 100; i++) {double d = Math.random()*100+1;System.out.println((int)d);}}public static void method(){System.out.println(Math.abs(-44));System.out.println(Math.max(-44,55));System.out.println(Math.min(-44,20));System.out.println(Math.ceil(-4.4));System.out.println(Math.floor(-4.4));System.out.println(Math.pow(2,3));System.out.println(Math.round(3.58));}}

日期类

Date类

  |-- 构造方法  new Date()

  |-- 构造方法  new Date(long date)毫秒值,可以将毫秒值,转成日期对象*****

  |-- 有了日期对象,怎么转成毫秒值呢 long getTime() *****

  |-- boolean after(Date d)当前日期在指定日期之后嘛

  |-- boolean before(Date d)当前日期在指定日期之前嘛

  |-- boolean equals(Date d)当前日期和指定时期相等吗

  |-- int compareTo(Data d)

对日期进行格式化SimpleDateFormat *****

  |-- 对日期对象格式化步骤:

  |-- 建立SimpleDateFormat对象,在构造方法中,传递格式化的格式

  |-- 使用SimpleDateFormat类中的方法,format方法,对日期对象进行格式化,返回一个字符串

  |-- 打印字符串

code:

package cn.river.integer;import java.text.SimpleDateFormat;import java.util.Date;public class DateDemo {public static void main(String[] args) {method_3();}//对日期对象,进行友好的格式化操作public static void method_3(){//建立SimpleDateFormat对象,构造方法,传递模式SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时 mm分 ss秒");//建立日期对象Date d = new Date();//调用SimpleDateFormat中的format格式化,日期对象String s = sdf.format(d);System.out.println(s);}//Date类中的after before equalspublic static void method_1(){Date d = new Date();Date dd = new Date(1378266159534L);System.out.println(d.after(dd));System.out.println(d.before(dd));System.out.println(d.equals(dd));System.out.println(d.equals(d));}//Date类的构造方法public static void method(){Date d = new Date();System.out.println(d);//Wed Sep 04 11:37:36 CST 2013System.out.println(d.getTime());//1378265938145Date dd = new Date(1378266159534L);System.out.println(dd);System.out.println(dd.getTime());}}

本地风格化时期

static DateFormat getDateInstance(intstyle);

2013-03-03 2010-08-23相差多少天

数据的来源有可能是用户输入的,字符串

将字符串转成日期对象

日期对象转成毫秒值

两个日期毫秒值相减 /1000/60/60/24

package cn.river.integer;import java.text.DateFormat;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;public class DateDemo1 {public static void main(String[] args) throws Exception{// TODO Auto-generated method stubmethod();}public static void method() throws Exception{String s1 = "2013-03-03";String s2 = "2010-08-23";SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//DateFormat df = DateFormat.getInstance();Date d1 = sdf.parse(s1);Date d2 = sdf.parse(s2);long l1 = d1.getTime();long l2 = d2.getTime();System.out.println(Math.abs(l1-l2)/1000/60/60/24);}//本地风格化public static void method_1(){//返回DateFormat对象,指定日期风格DateFormat df = DateFormat.getDateInstance(DateFormat.LONG);//风格格式化日期对象Date d = new Date();String s = df.format(d);System.out.println(s);}}

Timer类

Timer定时器

  |--实现定时完成某些任务

  |--构造方法建立对象

  |--void schedule

package cn.river.integer;import java.util.*;public class TimerDemo {public static void main(String[] args) {Timer t = new Timer(true);// //t是main的守护线程,mian一死t跟着也死t.schedule(new TimerTask(){public void run(){System.out.println("haha");}},new Date(), 1000);/*while(true){int i=1;System.out.println(i++);}*/}}

Calendar 日历类

  |--static Calendar getInstance()  返回的是Calendar类对象

  |--int get(日历字段) 返回给定日历字段的值。

  |--add(日历规则,偏移量)

  |--setTime(Date d)改变当前日历类,对应的日期

  |--请你计算出这一年,是不是闰年

code:

package cn.river.integer;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;public class DateDemo2 {public static void main(String[] args)throws Exception {// TODO Auto-generated method stubmethod_2();}//计算一下闰年,2月29日public static void method_2()throws Exception{//将字符串格式的日期,转成日期对象SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");Date d = sdf.parse("2012-03-01 ");//建立日历类对象Calendar c = Calendar.getInstance();//调用setTime方法。将日历设定为,当前的日期对象的日期c.setTime(d);//让这一年的3月1日,向前偏移一天,如果得到29,润年c.add(Calendar.DAY_OF_MONTH, -1);System.out.println(c.get(Calendar.DAY_OF_MONTH));}//2013-08-03 产品保质期是15天,什么时候到期public static void method_1()throws Exception{//字符串转成日期对象SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");Date d = sdf.parse("2013-08-03 ");//返回Calendar类对象Calendar c = Calendar.getInstance();c.setTime(d);c.add(Calendar.DAY_OF_MONTH,15);System.out.println(c.get(Calendar.YEAR)+"年"+getMonth((c.get(Calendar.MONTH)+1))+"月"    +getday(c.get(Calendar.DAY_OF_MONTH))+"日  "+getWeek(c.get(Calendar.DAY_OF_WEEK)-1)+"  "+c.get(Calendar.HOUR_OF_DAY)+"点"+c.get(Calendar.MINUTE)+"分"+c.get(Calendar.SECOND)+"秒");}public static void method(){//static Calendar getInstance()Calendar c = Calendar.getInstance();//Calendar c = new Calendar();    //2013年9月3日 星期二 10点50分10秒System.out.println(c.get(Calendar.YEAR)+"年"+getMonth((c.get(Calendar.MONTH)+1))+"月"    +getday(c.get(Calendar.DAY_OF_MONTH))+"日  "+getWeek(c.get(Calendar.DAY_OF_WEEK)-1)+"  "+c.get(Calendar.HOUR_OF_DAY)+"点"+c.get(Calendar.MINUTE)+"分"+c.get(Calendar.SECOND)+"秒");}public static String getWeek(int week){String[] table = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六",};return table[week];}public static String getMonth(int month){if(month<10)return "0"+month;elsereturn ""+month;}public static String getday(int day){if(day<10)return "0"+day;elsereturn ""+day;}}

System类

  |--static Prorerties getProperties()

  |--static String getProperty(String key)

public class SystemDemo {         publicstatic void main(String[] args) {         //      System.out.println(System.getProperties());                   Stringname = System.getProperty("os.name");                   System.out.println(name);                   if(name.equals("Windows7"))                            System.exit(0);                   System.out.println("-------------");         }}

Runtime类

此类采用单例模式构建

  |--static Runtime getRuntime()

  |--Process exec()

public class RuntimeDemo {         publicstatic void main(String[] args)throws Exception {                   //通过Runtime中的静态方法getRuntime获取该类对象                                     Runtimer = Runtime.getRuntime();                   Processp  = r.exec("shutdown -a");                   //Thread.sleep(3000);                   //p.destroy();//杀掉exec这个方法,开启的程序         }}
原创粉丝点击