黑马程序员——常用类介绍

------- android培训java培训、期待与您交流! ----------

 

常用类介绍

 

Math

1.      提供数学运算的运算方法

2.      常用方法

a)      public static 参数类型 abs(参数)

作用: 求参数的绝对值, 参数可以是int, float, double, long

b)      public static doubleceil(double d)

得到大于等于当前值的最小值

public static double floor(double d)

得到小于等于当前的最大值

c)      public static 参数类型 max(参数1, 参数2)

得到两个参数的最大值

public static 参数类型 min(参数1, 参数2)

得到两个参数的最小值

这里的参数可以是int, double, float, long

d)      public static double pow(doublea, double b)

作用: 返回ab次幂的值

e)      public static double random()

作用: 返回一个0~1之间的随机数, 包含0不包含1

f)       public static int round(floatf)

public static long round(double d)

作用: 返回参数四舍五入后的值

g)      public static doublesqrt(double l)

作用: 返回参数的开平方根的值

代码示例

package com.dateformat;

public class MathTest {

	public static void main(String[] args) {
		method1(); // 打印结果: 10
		method2(); // 打印结果: 5.0
		method3(); // 打印结果: 5.0 4.0
		method4(); // 打印结果: 8.0
		method5(); // 打印结果: 4.0
		method6(); // 打印结果: 6, 5

	}

	// Math.round方法
	private static void method6() {
		double a = 5.9;
		double b = 5.3;
		System.out.println(Math.round(a) + ", " + Math.round(b));
	}

	// Math.sqrt方法
	private static void method5() {
		int a = 16;
		System.out.println(Math.sqrt(a));
	}

	// Math.pow方法
	private static void method4() {
		int a = 2;
		int b = 3;
		System.out.println(Math.pow(a, b));
	}

	// Math.ceil方法和Math.floor方法
	private static void method3() {
		double a = 4.5;
		System.out.println(Math.ceil(a) + "  " + Math.floor(a));
	}

	// Math.max方法
	private static void method2() {
		double a = 2.0;
		double b = 5.0;
		System.out.println(Math.max(a, b));
	}

	// Math.abs方法
	private static void method1() {
		int a = -10;
		System.out.println(Math.abs(a));
	}
}


 

Date

1.      Date是程序开发中操作和得到日期的一个类

2.      Date构造方法

a)      public Date()

返回当前系统日期和时间

b)      public Date(long l)

返回又1970/1/1 0:00:00 经过指定毫秒数的日期和时间

3.      常用方法法

a)      public long getTime()

作用: 得到1970/1/1 0:00:00当前系统时间所经过的毫秒数

b)      public void setTime(long l)

作用: 根据指定经过的毫秒数设置当前系统时间

 

代码示例

package com.dateformat;

import java.util.Date;

public class DateTest {

	public static void main(String[] args) {
		// 创建一个Date对象
		Date date1 = new Date();
		// 打印Date对象
		System.out.println(date1); // 打印结果: Sat Apr 20 11:18:04 CST 2013

		// 创建一个指定毫秒数的Date对象
		Date date2 = new Date(10000);
		System.out.println(date2); // 打印结果: Thu Jan 01 08:00:10 CST 1970

		// 获得当前时间的毫秒值
		long times1 = new Date().getTime();
		System.out.println(times1); // 打印结果: 1366428071549

		// 设置Date对象当前经过的毫秒值
		date1.setTime(10000);
		System.out.println(date1); // 打印结果: Thu Jan 01 08:00:10 CST 1970

	}

}


 

Calendar

1.      这个抽象类是一个日历类, 也是用来操作日期和时间的

2.      常用方法

a)      public static Calendar()

作用: 获得一个Calendar类型的对象, 就是获得它的子类对象

b)      public int get(int i)

作用: 获得指定参数在Calendar中对应的值

注意: 这里的参数是Calendar类中定义的, 比如参数是Calendar.YEAR就可以画的当前系统时间里的年份, Calendar.MONTH可以画的当前系统时间里的月份, 这里需要注意的是月份是从0开始的, 而不是咱们平时指的1, 所以在应用是要将月份加上1才是当前真正的月份

c)      set方法

1.      public void set(int feld, intvalue) 它有一些重载的方法

作用: 对指定字段的值进行设置, 比如Calendar.getInstance().set(Calendar.YEAR, 2014)就是将当前系统年份设定为2014

2.      public void set(int year, intmonth, int date)

作用: 对年月日进行重新设置

3.      public void set(int year, intmonth, int date, int hour, intminute, int second)

作用: 对年月日,小时, 分钟,秒进行重新设置

d)      getTimeInMillis,setTimeInMillis方法

1.      public long getTimeInMillis()

获得当前Calendar所经过的毫秒值

2.      public void set(long l)

calendar设置新的毫秒值

e)      public void add(int field, intvalue)

作用: Calendar指定的字段设置+value的操作, 比如Calendar.getInstance().add(Calendar.Year, 1); 就是将当前系统的年份加上1

f)       DeteCalendar相互转换

1.      Date转换为Calendar

public void setTime(Date date)

作用: Date表示的时间设置给Calendar

2.      Calendar转换为Date

public Date getTime()

作用: 将当前Calendar对象表示的时间转换成Date对象

代码示例

package com.dateformat;

import java.util.Date;
import java.util.Calendar;

public class CalendarTest {

	public static void main(String[] args) {
		// 创建一个Calendar对象
		Calendar cal = Calendar.getInstance();
		// 分别获得年月日时间和星期
		int year = cal.get(Calendar.YEAR);
		int month = cal.get(Calendar.MONTH) + 1;
		int date = cal.get(Calendar.DATE);
		int hour = cal.get(Calendar.HOUR);
		int minute = cal.get(Calendar.MINUTE);
		int scond = cal.get(Calendar.SECOND);
		int day = cal.get(Calendar.DAY_OF_WEEK) - 1;
		System.out.println(year + "年" + month + "月" + date + "日 " + hour + "时"
				+ minute + "分" + scond + "秒" + "星期" + day);
		// 打印结果: 2013年4月20日 11时34分3秒星期6

		// 得到今天是这个月的第几天
		int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH);
		System.out.println("今天是" + dayOfMonth + "号"); // 打印结果: 今天是20号
		// 得到今天是在这个月的第几个星期
		int dayOfWeekInMonth = cal.get(Calendar.DAY_OF_WEEK_IN_MONTH);
		System.out.println("现在是这个月的第" + dayOfWeekInMonth + "个星期"); // 打印结果:
																	// 现在是这个月的第3个星期
		// 得到今天是一年中的第几天
		int dayOfYear = cal.get(Calendar.DAY_OF_YEAR);
		System.out.println("今天是今年的第" + dayOfYear + "天"); // 打印结果: 今天是今年的第110天

		// 设置Calendar对象的时间, 并转换为Date对象
		Date dt = cal.getTime();
		System.out.println(dt); // 打印结果: Sat Apr 20 11:45:27 CST 2013

		// 设置新的毫秒数给dt
		dt.setTime(100000);
		// 将Date表示的时间设置给Calendar对象
		cal.setTime(dt);
		// 获得Calendar对象当前经过的毫秒值
		System.out.println(cal.getTimeInMillis()); // 打印结果: 100000

		// 重新设置Calendar对象毫秒值
		cal.setTimeInMillis(Calendar.getInstance().getTimeInMillis());
		System.out.println(cal.getTimeInMillis()); // 打印结果: 1366429841894
	}

}


 

DateFormatSimpleDateFormat

1.      这两个方法的作用是对Date表示的时间和日期进行格式化

2.      DateFormat常用方法

a)      public static DateFormatgetDateInstance()

作用: 获得DateFormat的一个实例对象, 获得日期格式化器

b)      public static DateFormatgetDateTimeInstance()

作用: 获得DateFormat的一个实例对象, 获得日期/时间格式化器

c)      public String format(Date date)

作用: 格式化指定日期或者日期/时间并返回字符串

3.      SimpleDateFormat

a)      这个类可以在构造对象的时候指定一个想要的日期或者日期/时间的格式

b)      public SimpleDateFormat(“yyyyMMdd”)

作用: 返回一个具有指定格式的格式化器

4.      String类型的日期和时间转换为Date

public Date parse(String s) throws ParseException

作用: 将一个日期时间的字符串转换为Date类型的日期时间, 必须指定格式

 

代码示例

package com.dateformat;

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

public class DateFTest {

	public static void main(String[] args) throws ParseException {
		// 创建一个Date对象
		Date date = new Date();
		// 创建一个日期的DateFormat对象
		DateFormat df = DateFormat.getDateInstance();
		// 创建一个日期/时间的DateFormat对象
		DateFormat dtf = DateFormat.getDateTimeInstance();

		// 获得当前时间的日期格式化的字符串
		String dat = df.format(date);
		// 获得当前时间的日期/时间格式化的的字符串
		String datt = dtf.format(date);

		System.out.println(dat); // 打印结果: 2013-4-20

		System.out.println(datt); // 打印结果: 2013-4-20 9:58:39

		// 创建一个SimpleDateFormat对象并制定日期格式
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
		// 调用format方法将Date按照指定格式转换为字符串
		String datf = sdf.format(date);
		System.out.println(datf); // 打印结果: 2013年04月20日
		// 创建一个SimpleDateFormat对象并制定日期/时间格式
		SimpleDateFormat sdft = new SimpleDateFormat("yyyy年MM月dd日hh时mm分ss秒");
		String dattf = sdft.format(date);
		System.out.println(dattf); // 打印结果: 2013年04月20日10时14分34秒

		// 这里的各时期不指定格式会出报ParseException异常
		SimpleDateFormat s = new SimpleDateFormat();
		Date date1 = sdft.parse(dattf); // s.parse(dattf);
		System.out.println(date1);
	}
}


 

System

这个类提供了一些重要方法, 类中方法全是静态的

1.      public static void exit(int i)

作用: 停止当前JVM

2.      public static void gc()

作用: 是一个垃圾回收器, 调用它的时候Object中的finzlized方法会被调用

3.      public static longcurrentTimeMillis()

作用: 获得1970/1/1 0:00:00到当前为止经过的毫秒数

4.      public static PropertiesgetProperties()

作用: 获得当前系统的一些信息, PropertiesMap的子类

5.      public static StringgetProperty(String key)

作用: 获得系统中指定key的属性(value)

6.      public static voidsetProperty(String key, String value)

作用: 设置keyvalue在系统中

 

代码示例:

package com.dateformat;

public class SystemTest {

	public static void main(String[] args) {
		// 给系统设置一个Properties
		String key = "RenYimin";
		String value = "ThinkPad";
		System.setProperty(key, value);
		// 获得key值的助兴
		System.out.println(System.getProperty(key)); // 打印结果: ThinkPad
		// 获得当前的毫秒值
		long l = System.currentTimeMillis();
		System.out.println(l); // 打印结果: 1366430653451
		// 拷贝数组
		int[] a = { 2, 4, 5, 6, 3, 2 };
		int[] b = new int[a.length];
		System.arraycopy(a, 0, b, 0, a.length);

		StringBuilder sb = new StringBuilder();
		sb.append("{");
		// 打印数组b的元素
		for (int i : b) {
			sb.append(i + ",");
		}
		sb.deleteCharAt(sb.length() - 1);
		sb.append("}");
		System.out.println(sb); // 打印结果: {2,4,5,6,3,2}
	}

}


 

Runtime

1.       public static Runtime getRuntime()

作用: 返回当前运行的Java程序的Runtime对象

2.       public static Process exec(String command)

作用: 返回一个Process对象, 可以执行一个进程, commad是某个进程的路径与名称

注意: Process是一个进程, 提供destroy()来销毁进程

3.       public void exit(int status)

作用: 停止JVM, System.exit()就是执行了这个方法

 

代码示例

package com.itcast;

import java.io.IOException;

public class RuntimeTest {

	public static void main(String[] args) throws IOException,
			InterruptedException {
		// 获得Runtime对象
		Runtime run = Runtime.getRuntime();
		// 设置一个执行重启电脑的进程
		Process proc = run.exec("shutdown -g");
		// 在25秒的时候将进程销毁
		for (int i = 0; i < 25; i++) {
			Thread.sleep(1000);
			System.out.print(i);
		}
		proc.destroy(); // 注意: 经过多次试验, 这个销毁的方法可能不会销毁进程
	}
}


 

Properties(了解)

这个类是Map的实现类, keyvalue都是String

1.      getProperty(String key)方法

作用: 获得keyvalue

2.      list方法将Properties对象中内容写入到文件中

3.      load方法将文件中的内容读到Properties对象中

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值