黑马程序员——常用类

------<a href="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! -------

Math类概述和方法使用

Java.lang.Math类它是一个关于数学运算操作类。

它中的方法是static,使用时直接Math.方法名.

              // 1.约对值
		Math.abs(10);

		// 2.四舍五入
		Math.round(3.14);

		// 3.两个数的最大值与最小值
		Math.max(10, 20);
		Math.min(10, 20);

		// 4.开平方
		int a = (int) Math.sqrt(9);
		System.out.println(a);

		// 求某个数的n次方
		int b = (int) Math.pow(10, 3);
		System.out.println(b);

		// 求某个浮点数的向下值 向上值
		double c = 3.14;
		System.out.println(Math.floor(c)); //求出比c小的最大整数值
		System.out.println(Math.ceil(c));//求出比c大的最小整数值
		
		//随机数
		double d=Math.random(); //得到0-1之间的随机浮点数  不包含1

Random类的概述与方法使用

Random类 它是jdk1.5以后提供的一个可以获取随机数的类。

构造方法:

 new Random() 使用毫秒值做为种子

 new Random(long seed)使用指定值做为种子,每次得到的随机数是一样的。

成员方法

nextInt(); 返回一个int范围的随机数

nextInt(int n)返回一个0-n之间的随机数.,不包含n

 

在开发中获取随机数:

Random ran=new Random();

Int n=ran.nextInt(n);

System类中的垃圾回收的方法gc()的讲解

只需要大家了解:

System.gc()方法可以让垃圾回收执行。

它其实会去执行类中的finalize方法。

在开发中,我们一般情况下不需要使用gc();


System类中的exit()currentTimeMillis()的讲解

package cn.itcast.system;

/*
 * System类中的exit()方法.
 * 
 *  exit方法是关闭jvm.
 *  
 *  public static void exit(int status)
 *  	参数:status如果为0代表正常关闭,非0 代表异常关闭。
 */
public class SystemDemo2 {

	public static void main(String[] args) {

		for (int i = 0; i < 10; i++) {
			if (i == 5) {
				// return; //可以理解成只是让当前程序结束
				System.exit(0); //是让jvm停止。
			}
			System.out.println(i);
		}

	}
}

package cn.itcast.system;

/**
 * 
 * @author Administrator System类中的currentTimeMillis方法
 * 
 *         概念:基准时间:1970年1月1日 00:00:00.
 * 
 *         currentTimeMills方法得到的就是从基准时间开始到程序运行时的毫秒值。
 * 
 *         应用: 可以使用它进行程序效率测试
 * 
 */
public class SystemDemo3 {

	public static void main(String[] args) {

		// long l=System.currentTimeMillis();
		// System.out.println(l); //1427699551883
		// String s="abc";
		StringBuilder build = new StringBuilder("abc");
		long l1 = System.currentTimeMillis();
		for (int i = 0; i < 10000; i++) {
			// s+=i;
			build.append(i);
		}
		long l2 = System.currentTimeMillis();

		System.out.println(l2 - l1); // 7

	}
}

System类中的arraycopy()的讲解

package cn.itcast.system;

import java.util.Arrays;

/*
 * System类中的arraycopy方法
 * 	
 * public static void arraycopy(Object src,
                             int srcPos,
                             Object dest,
                             int destPos,
                             int length)
   功能:可以进行数组copy
 参数:
      src--->源数组          
      srcPos----->开始偏移量
      dest---->目标数组
      destPos--->目标数组偏移量
      lengt---->复制几个元素          
 */
public class SystemDemo4 {

	public static void main(String[] args) {
		
		int[] arr1={1,2,3,4};
		int[] arr2={5,6,7,8};
		
		System.arraycopy(arr1, 1, arr2, 2, 3);
		
		System.out.println(Arrays.toString(arr1)); //[1, 2, 3, 4]
		System.out.println(Arrays.toString(arr2));  //[5, 6, 2, 8]
		
	}
}

Date的概述和构造方法

Date类要求大家掌握,在就业班中会经常使用.

Java.util包下的Date

Date类它代表一个时间点,精确到毫秒.


package cn.itcast.date;

import java.util.Date;

/**
 * 
 * @author Administrator
 * 
 * Date类  java.util包下
 * 
 * Date类构造方法
 * 	
 * 	构造方法
 * 	new Date();  得到当前系统时间
 *  new Date(long val);  val代表的是从基准时间开始经过的毫秒值
 *
 */
public class DateDemo1 {

	public static void main(String[] args) {
		//无参数构造 
		
		// Date date=new Date();
		//
		// System.out.println(date); //Mon Mar 30 16:18:16 CST 2015
		//
		
		Date date=new Date(1000);
		
		System.out.println(date);
	}
}

Date中的日期与毫秒相互转换

package cn.itcast.date;

import java.util.Date;
/*
 * 关于Date对象与毫秒值的转换
 * 
 * 	成员方法
 * 	 long getTime()  它的作用就是获取Date对象描述的时间点的毫秒值。
 * 	 setTime(long val);它的作用是修改Date对象描述的时间点的毫秒值。
 */

public class DateDemo2 {

	public static void main(String[] args) {

		// Date date = new Date(); // 当前时间
		//
		// long l = System.currentTimeMillis(); //得到从基准时间到当前经过的毫秒值。
		
		
		//得到昨天这个时间点
		
		Date date=new Date(new Date().getTime()-24*60*60*1000);
		System.out.println(date);
	
	}
}

DateFormat实现日期和字符串的相互转换

DateFormat类它可以在Date----String之间做相互转换。

Date---àString  格式化

String-àDate   解析

 

DateFormat类在java.text包下.

在实际开发中对字符中与Date进行相互转换,一般情况下使用DateFormat类的子类

SimpleDateFormat

 

1. 怎样将一个Date对象格式化成指定效果的字符串

使用DateFormat类中提供的format方法。

 

public String format(Date date)

SimpleDateFormat类中也有这个方法。

 

问题:SimpleDateFormat它有无参数构造,它可以调用format方法对一个Date对象进行格式化,但是得到的不是我们想要的效果,怎样处理?

通知SimpleDateFormat它在做format操作时,得到字符串是我们指定的一个效果,就可以。

 

可以在创建SimpleDateFormat时指定一个模板  new SimpleDateFormat(String pattern)


package cn.itcast.dateformat;

import java.text.SimpleDateFormat;
import java.util.Date;

/*
 * DateFormat进行将一个Date对象格式化成指定效果的字符串
 * 
 * 将Date---->String    format方法
 */
public class DateFormatDemo2 {

	public static void main(String[] args) {

		// 1.创建一个Date对象.
		Date date = new Date();

		// 2.得到 2015-03-30 16时59分

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH时mm分");
		String s = sdf.format(date);
		System.out.println(s);
	}
}

将一个字符串分析成时间

DateFormat类中提供一个方法parse方法,它可以将一个字符串分析成Date对象

Public Date parse(String s);

注意:在使用SimpleDateFormat时,需要指定一个模板,它要与解析的字符串的格式一致才可以。

package cn.itcast.dateformat;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/*
 * DateFormat进行将一个字符串分析成时间
 * 
 *
 */
public class DateFormatDemo3 {

	public static void main(String[] args) throws ParseException{

		String s="2008年08月08日 08:08:08";
		
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
		
		Date date=sdf.parse(s);
		
		System.out.println(date);
	}
}

你在这个世界活了多少天

package cn.itcast.dateformat;

import java.text.ParseException;
import java.util.Date;

/*
 * 案例:计算你在这个世界上活了多少天
 * 
 * 要求:
 * 	从键盘输入你的出生年月日
 * 
 * 分析:
 * 	1.根据出生年月日得以一个Date对象birthday。
 *  2.得到birthday代表的毫秒值
 *  3.使用现在的时间毫秒值-birthday毫秒值  = 从出生到现在经过的毫秒。
 *  4.使用毫秒值/一天的毫秒=活了多少天.
 */
public class DateFormatDemo4 {

	public static void main(String[] args) throws ParseException {

		String s = "1981-10-08";
		// 1.根据出生年月日得以一个Date对象birthday。
		Date birthday = DateFormatUtils.StringToDate(s, "yyyy-MM-dd");
		// 2.得到birthday代表的毫秒值
		long l1 = birthday.getTime();
		// 3.使用现在的时间毫秒值-birthday毫秒值 = 从出生到现在经过的毫秒。
		long l = System.currentTimeMillis() - l1;
		// long l = new Date().getTime() - l1;
		// 4.使用毫秒值/一天的毫秒=活了多少天.

		System.out.println(l / (24 * 60 * 60 * 1000));
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值