一、一些常用类
1.包装类:Byte,Short,Integer,Long,Float,Double,Character,Boolean
2.数组工具类:Arrays
(1)二分法查找: binarySearch
(2)排序: sort
(3)比较: equals
(4)转为字符串: toString
(5)填充: fill
(6)拷贝一部分: copyOfRange
3.系统类:System,Runtime
System:包含一些和系统相关的功能,例如销毁垃圾,退出虚拟机,获取当前时间,查看默认参数,设置默认输入输出流.
Runtime:可以用来执行一些操作命令,和在cmd.exe中执行命令一样.
注意: Runtime类是一个单例设计模式的类, 可以用getRuntime()方法获取对象.
4.时间类:Date,Calendar
Date:代表一个时间,可以用SimpleDateFormat类格式化成我们需要的格式,通常用来显示一个时间.
Calendar:用来操作时间,get,set,add.
5.计时器:Timer
可以在后台开启一条线程,设置指定时间后执行某个任务
二、简单示例
//示例一
public class Demo {
public static void main(String[] args) {
//JDK5之后,基本数据类型与包装类之间有了自动转换的功能
int i = new Integer(100);
Integer integer = 100;
System.out.println(integer / 2);//结果为:50
// 小于就返回负数, 等于就返回0, 大于就返回正数
System.out.println(integer.compareTo(i));
// 将int类型的1转换为new Integer(1), 然后打印new Integer(1).toString()
System.out.println(1);
// 缓冲区大小为1个字节, -128到127,超出范围结果就为false了
Integer integer1 = 128;
Integer integer2 = 128;
System.out.println(integer1 == integer2);//结果为false
String s = "123";
System.out.println(Integer.parseInt(s) + 1);//124
//转二进制,八进制,十六进制
System.out.println(Integer.toBinaryString(100));//1100100
System.out.println(Integer.toOctalString(100));//144
System.out.println(Integer.toHexString(100));//64
}
}
//示例二
public class Demo {
public static void main(String[] args) {
//如果有很大的数long也盛不下则用这个.
BigInteger x = new BigInteger("45678901234567890123890123456789067677");
BigInteger y = new BigInteger("34567890123456789012345678901234567890");
System.out.println(x.add(y));//加
System.out.println(x.subtract(y));//减
System.out.println(x.multiply(y));//乘
System.out.println(x.divide(y));//除
//求最大最小值的,其余类似.
System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.MIN_VALUE);
System.out.println(Long.MAX_VALUE);
System.out.println(Long.MIN_VALUE);
}
}
//示例三
public class Demo {
public static void main(String[] args) {
//确定使用指定基数的特定数字的字符表示形式。
System.out.println(Character.forDigit(9, 16));//结果为9
System.out.println(Character.forDigit(10, 16));//结果为a
System.out.println(Character.forDigit(11, 16));//结果为b
System.out.println(Character.isUpperCase('a'));//判断是大写字母么
System.out.println(Character.isLowerCase('a'));
System.out.println(Character.isLetter('1'));//判断是字母么,这里结果为false
System.out.println(Character.isWhitespace(' '));
System.out.println(Character.isWhitespace('\t'));//判断是空格么
}
}
//示例四
public class Demo {
public static void main(String[] args) throws Exception {
//假设我qq目录为g:\Tencent\QQ\Bin\QQ.exe
Process p = Runtime.getRuntime().exec("\"g:\\Tencent\\QQ\\Bin\\QQ.exe\"");
Thread.sleep(3000);
p.destroy();//结果是qq启动,3秒后被销毁.
}
}
//示例五
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateDemo {
public static void main(String[] args) throws ParseException {
demo1();
demo2();
demo3();
demo4();
}
private static void demo4() {
Date date = new Date(1000); // 1970年1月1日0时 + 100毫秒后的时间
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String s = sdf.format(date);
System.out.println(s);
}
private static void demo3() {
Date date = new Date();
long time = date.getTime(); // 1970年1月1日0时到创建这个Date的时间经历了多少毫秒
System.out.println(time);
}
private static void demo2() throws ParseException {
String s = "2012-08-06 15:09:20";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = sdf.parse(s); // 将字符串转回Date对象
System.out.println(date);
}
private static void demo1() {
Date date = new Date(); // 创建日期对象, 代表当前时间
// 创建格式化工具
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String s = sdf.format(date);// 将日期格式化成字符串
System.out.println(s);
}
}
//示例四,判断是否是闰年
import java.util.Calendar;
public class Demo {
public static void main(String[] args) throws Exception {
System.out.println(isLeapYear(2012));//true
System.out.println(isLeapYear(2008));//true
System.out.println(isLeapYear(2100));//false
System.out.println(isLeapYear(2000));//true
System.out.println(isLeapYear(2013));//false
}
// (year % 4 == 0 && year % 100 != 0) || year % 400 == 0 判断是否闰年
public static boolean isLeapYear(int year) {
Calendar c = Calendar.getInstance();// 获取日历对象
c.set(year, 2, 1); // 将日历设置到指定年份的3月1号
c.add(Calendar.DATE, -1); // 向前翻1天
return c.get(Calendar.DATE) == 29; // 看那天是否是29
}
}
//示例六
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class Demo {
public static void main(String[] args) throws Exception {
Timer t = new Timer(); // 创建计时器, 新线程
t.schedule(new MyTask(), 5000); // 给计时器安排任务, 5000豪秒后执行,仅执行一次
//t.schedule(new MyTask(), 5000, 500); // 5000毫秒后执行第一次, 每隔500毫秒再次执行
while (true) { //显示时间,为了更好的测试
System.out.println(new Date());
Thread.sleep(1000);
}
}
}
class MyTask extends TimerTask {
public void run() {
System.out.println("Do something!!!");
}
}