实用类

1.包装类

public class BaoZhuangLei {
public static void main(String[] args) {
/*
* 装箱:把基本数据类型转换为对应的对象类型
* 作用:1.在需要使用对象类型的时候,转换成对应的对象类型(在集合里面)
* 2.在转换完成后,拥有对应的属性和方法
*/
//基本数据类型到基本数据类型的转换
//方法一
Integer integer = new Integer(12);//装箱,数据产生对应的属性和方法
System.out.println(integer);
int i = integer;//拆箱
System.out.println(i);
//方法二
Integer integer2 = new Integer("1233");
System.out.println(integer2);
int i2= integer2;
System.out.println(i2);
//方法三
Integer integer3=Integer.valueOf(18);//装箱
int i3 = integer3.intValue();//拆箱
System.out.println(i3);
//方法四
Integer integer4 = 5;//装箱
int i4 = integer4;//拆箱
System.out.println(i4);

//字符串到基本数据类型的转换
int i5 = Integer.parseInt("111");
System.out.println(i5);
float f = Float.parseFloat("222");
System.out.println(f);
double d = Double.parseDouble("333");
System.out.println(d);
boolean b = Boolean.parseBoolean("true");
System.out.println(b);
//基本数据类型转换为字符串
int i6=123;
String s = Integer.toString(i6);
System.out.println(s);

String s2 = 123+"";
System.out.println(s2);


}

}

 

2.日期类

public class DateLei {
public static void main(String[] args) {
//获取当前日期
Date now = new Date();
System.out.println(now.toString());
//指定对日期时间的规范
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = dateFormat.format(now);
System.out.println(time);
//转换成date类型
SimpleDateFormat dateFormat2 = new SimpleDateFormat("E");
String week = dateFormat2.format(now);
System.out.println(week);
//计算天数差
String day1 = "1994:10:03";
String day2 = "1993:01:06";
SimpleDateFormat format =new SimpleDateFormat("yyyy:MM:dd");

//将指定模式转换成Date类型
try{
Date date1 =format.parse(day1);
Date date2 =format.parse(day2);
//方法一
long time1=date1.getTime();
long time2 = date2.getTime();
double days = ((time1-time2)/(1000*60*60*24));
System.out.println(days);
//方法二
Calendar calendar = Calendar.getInstance();
calendar.setTime(date1);
int time3 = calendar.get(Calendar.DAY_OF_YEAR);
calendar.setTime(date2);
int time4 = calendar.get(Calendar.DAY_OF_YEAR);
int day3 = time4-time3;
System.out.println(day3);
//方法三
Calendar calendar1 = Calendar.getInstance();
calendar1.setTime(date1);
Calendar calendar2 = Calendar.getInstance();
calendar2.setTime(date2);
int dayss = calendar1.compareTo(calendar1);
System.out.println("相差"+dayss+"天。");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//判断今天周几
Calendar calendar = Calendar.getInstance();
int index = calendar.get(Calendar.DAY_OF_WEEK);
System.out.println(index);
switch (1) {
case 1:
System.out.println("周天,谁也别管我!!");
break;
case 2:
case 3:
case 4:
case 5:
case 6:
System.out.println("工作日,我要认真敲代码!");
break;
case 7:
System.out.println("周六,这周也要加班!");
break;
default:
break;
}
}
}

 

public class DateWork {

public static void main(String[] args) {
//获取当前日期时间
Date now = new Date();
System.out.println("规范前的日期为:"+now);
//指定对日期时间的规范模式
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh-mm-ss");
//按指定的模式将Date类型转成字符串类型
String time = dateFormat.format(now);
System.out.println("规范后的日期"+time);
SimpleDateFormat dateFormat2 = new SimpleDateFormat("E");
String week = dateFormat2.format(now);
System.out.println("今天是:"+week);

//计算两个时间的天数差
String day1 = "1994:10:04 02-21-57";//姚鑫生日(String)
String day2 = "1994:10:03 23-21-57";//华豪(String)
SimpleDateFormat format = new SimpleDateFormat("yyyy:MM:dd HH-mm-ss");
try {
//按指定的模式将字符串类型转成Date类型
Date date1 = format.parse(day1);//姚鑫生日(Date)
Date date2 = format.parse(day2);//华豪(Date)
//方法1
long time1 = date1.getTime();
long time2 = date2.getTime();
int days1 = (int) ((time1-time2)/(1000*60*60*24));
System.out.println("相差"+days1+"天。");
//方法2
Calendar calendar = Calendar.getInstance();
calendar.setTime(date1);
int time3 = calendar.get(Calendar.DAY_OF_YEAR);
calendar.setTime(date2);
int time4 = calendar.get(Calendar.DAY_OF_YEAR);
int days = time3-time4;
System.out.println("相差"+days+"天。");
//方法3
Calendar calendar1 = Calendar.getInstance();
calendar1.setTime(date1);
Calendar calendar2 = Calendar.getInstance();
calendar2.setTime(date2);
int dayss = calendar1.compareTo(calendar1);
System.out.println("相差"+dayss+"天。");
} catch (ParseException e) {
e.printStackTrace();
}
//判断今天周几
Calendar calendar = Calendar.getInstance();
int index = calendar.get(Calendar.DAY_OF_WEEK);
System.out.println(index);
switch (1) {
case 1:
System.out.println("周天,谁也别管我!!");
break;
case 2:
case 3:
case 4:
case 5:
case 6:
System.out.println("工作日,我要认真敲代码!");
break;
case 7:
System.out.println("周六,这周也要加班!");
break;
default:
break;
}
}

}

 

3.Math

public class MathLei {
public static void main(String[] args) {
//max min
int num1 = Math.max(20, 25);
int num2 = Math.min(20, 25);
System.out.println(num2);
System.out.println(num1);
//pow
double powNum = Math.pow(5, 3);
System.out.println(powNum);
//sprt
double sprt = Math.sqrt(16);
System.out.println(sprt);
//abs
int i = -6;
int abs = Math.abs(i);
System.out.println(abs);
//sin cos tan asin
double angle =(Math.PI)/2;
double sin = Math.sin(angle);
System.out.println(sin);
double cos = Math.cos(angle);
System.out.println(cos);
double tan = Math.tan(angle);
System.out.println(tan);
//random 返回一个0到1的随机数,不包括1
//返回一个1到10的随机数
int random =(int)( Math.random()*10+1);
System.out.println(random);
//产生0或者1的随机数
int check = Math.random()>=0.5?1:0;
System.out.println(check);

int random2 =(int) (Math.random()*6+5);
System.out.println(random2);

}

}

4.stringbuffer

public class StringBufferLei {
public static void main(String[] args) {
/*Scanner input = new Scanner(System.in);
StringBuffer stringBuffer= new StringBuffer();
String word;
String result2;
boolean go = true;
do{
System.out.println("请输入单词:");
word=input.next();
if(word.equals("end")){
go = false;
break;
}else{
stringBuffer.append(word+",");
}
}while(go);
System.out.println(stringBuffer);
String result =stringBuffer.toString();
String [] result2Array = result.split(",");
for(int i=0;i<result2Array.length;i++){
System.out.println(result2Array[i]);
}*/

//random
Random random=new Random();
int i = random.nextInt(5);
System.out.println(i);

Random r= new Random();
for(int i2 =0;i2<10;i2++){
double output = r.nextDouble()*9+1;
System.out.println(output+"\t");

}

}

}

 

posted @ 2016-11-03 14:16  造西  阅读(103)  评论(0编辑  收藏  举报