------- android培训、java培训、期待与您交流! ----------
----------------------------------------java.util.Random(类)----------------------------------------
java.util.Random(类):此类的实例用于生成伪随机数流。
构造方法:
Random() : 创建一个新的随机数生成器。
Random(long seed) : 使用单个 long 种子创建一个新的随机数生成器。
成员方法:
public int nextInt():获取一个int范围内的随机数;
public int nextInt(int n):获取一个随机数,该数大于等于0 ,小于n;
public class Demo { public static void main(String[] args) { Random rdm1 = new Random(); Random rdm2 = new Random(10); for (int i = 0; i < 5; i++) { System.out.println("获取一个随机数(使用无参构造的对象):" + rdm1.nextInt()); } for (int i = 0; i < 5; i++) { System.out.println("获取一个随机数(使用种子构造的对象):" + rdm2.nextInt()); } for (int i = 0; i < 5; i++) { System.out.println("获取0(包含)--9(包含)之间的随机数:" + rdm1.nextInt(10)); } for (int i = 0; i < 5; i++) { System.out.println("获取1(包含)--10(包含)之间的随机数:"+ (rdm1.nextInt(10) + 1)); } } }
----------------------------------------java.math.BigInteger(类)----------------------------------------
java.math.BigInteger(类):大整数运算的类;构造方法:
public BigInteger(String val)
成员方法:
public BigInteger add(BigInteger val):加
public BigInteger subtract(BigInteger val):减
public BigInteger multiply(BigInteger val):乘
public BigInteger divide(BigInteger val):除
public BigInteger[] divideAndRemainder(BigInteger val):
public class Demo { public static void main(String[] args) { BigInteger big1 = new BigInteger("7489174389145435425324324324"); BigInteger big2 = new BigInteger("4294438972584324325253253243"); System.out.println("加:" + big1.add(big2)); System.out.println("减:" + big1.subtract(big2)); System.out.println("乘:" + big1.multiply(big2)); System.out.println("除:" + big1.divide(big2)); BigInteger[] result = big1.divideAndRemainder(big2); System.out.println("数组长度:" + result.length); System.out.println("除法结果:" + result[0]); System.out.println("取余结果:" + result[1]); } }
----------------------------------------java.math.BigDecimal(类) ----------------------------------------
java.math.BigDecimal(类) :浮点运算。
注意:在以后的开发中,凡是涉及到浮点运算的,不要使用"基本数据类型",而是使用BigDecimal类来运算。
构造方法:
public BigDecimal(String val):
成员方法:
public BigDecimal add(BigDecimal augend):加
public BigDecimal subtract(BigDecimal subtrahend):减
public BigDecimal multiply(BigDecimal multiplicand):乘
public BigDecimal divide(BigDecimal divisor):除法(不建议使用,当除不尽时,此方法会抛出异常)
public BigDecimal divide(BigDecimal divisor,int scale,int roundingMode):除法,指定精度,指定舍入模式;
public class Demo { public static void main(String[] args) { /* * System.out.println(0.09 + 0.01); * System.out.println(1.0 - 0.32); * System.out.println(1.015 * 100); * System.out.println(1.301 / 100); */ BigDecimal b1 = new BigDecimal("0.1"); BigDecimal b2 = new BigDecimal("0.03"); System.out.println("加:" + b1.add(b2)); System.out.println("减:" + b1.subtract(b2)); System.out.println("乘:" + b1.multiply(b2)); // System.out.println("除法:" + b1.divide(b2)); System.out.println("四舍五入除法:" + b1.divide(b2, 2, RoundingMode.HALF_UP)); } }
----------------------------------------java.util.Date(类)----------------------------------------
java.util.Date(类) :类 Date 表示特定的瞬间,精确到毫秒。
构造方法:
public Date():使用当前系统时间构造一个Date
public Date(long date):使用一个毫秒值构造一个Date
成员方法:
public long getTime():获取毫秒值
public void setTime(long time):设置毫秒值
public class Demo { public static void main(String[] args) { Date date1 = new Date(); System.out.println("date1 = " + date1.toString()); System.out.println("毫秒值:" + date1.getTime()); Date date2 = new Date(1000); System.out.println("date2 = " + date2); date1.setTime(1000); System.out.println("date1 = " + date1); } }
----------------------------------------SimpleDateFormat(类)----------------------------------------
DateFromat(抽象类):
|--SimpleDateFormat(类):
构造方法:
public SimpleDateFormat():用默认的模式和默认语言环境的日期格式符号构造 SimpleDateFormat。
public SimpleDateFormat(String pattern):用给定的模式和默认语言环境的日期格式符号构造 SimpleDateFormat。
成员方法:
public final String format(Date date):将一个Date转换为String
public Date parse(String source):将一个String转换为Date;注意:当前SimpleDateFormat的对象的格式一定要与参数source表示的格式一致,否则会抛出异常;
public class Demo { public static void main(String[] args) throws ParseException { // SimpleDateFormat sdf = new SimpleDateFormat(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 1.将Date转换为String Date date = new Date(); String str = sdf.format(date); System.out.println(str); // 2.将String转换为Date String s1 = "2015-09-14 0:0:0"; Date date2 = sdf.parse(s1); System.out.println("date2 = " + date2); } }
----------------------------------------java.util.Calendar(抽象类)----------------------------------------
java.util.Calendar(抽象类) :日历类
|--GregorianCalendar(类):
获取GregorianCalendar对象:
方式一:直接实例化;
方式二:Calendar的getInstance()方法;
成员方法:
public int get(int field):获取某个字段的值
public void add(int field,int amount):
public final void set(int year,int month,int date):
public class Demo { public static void main(String[] args) { Calendar c = Calendar.getInstance(); System.out.println(c); GregorianCalendar gc = new GregorianCalendar();// 使用当前系统时间 System.out.println(gc); int year = gc.get(Calendar.YEAR);// gc.get(1) int month = gc.get(Calendar.MONTH) + 1;// 月份内部是从0--11 int day = gc.get(Calendar.DAY_OF_MONTH); int hour = gc.get(Calendar.HOUR_OF_DAY);// 0--23 int minute = gc.get(Calendar.MINUTE); int second = gc.get(Calendar.SECOND); // 内部的周:日 一 二 三 四 五 六 // 1 2 3 4 5 6 7 int week = gc.get(Calendar.DAY_OF_WEEK); System.out.println(year + "年" + month + "月" + day + "日" + " " + hour + ":" + minute + ":" + second); System.out.println("week = " + week); System.out.println(getWeek(week)); } public static String getWeek(int week) { String[] weekArray = { "", "星期日", "星期一", "星期二", "星期三", "星期四", "星期五","星期六" }; return weekArray[week]; } }
Calendar的add()和set()方法
public void add(int field,int amount):为某个字段添加指定值
public final void set(int year,int month,int date):将当前日历替换为新的year,month,date
public class Demo2 { public static void main(String[] args) { Calendar cd = Calendar.getInstance(); // cd = new GregorianCalendar(1990,1 - 1,1); // 在当前日期上,添加一个月 /* * cd.add(Calendar.MONTH, 4); * System.out.println(cd); */ // 下个月的今天 // cd.add(Calendar.MONTH, 1); // 明年的今天 // cd.add(Calendar.YEAR, 1); // 去年的今天 // cd.add(Calendar.YEAR, -1); cd.set(2015, 1 - 1, 1); int week = cd.get(Calendar.DAY_OF_WEEK); System.out.println(Demo.getWeek(week)); } }
----------------------------------------Timer(类)定时器----------------------------------------
作用:
1.可以在指定的时间做指定的事情;
2.可以从指定的时间开始,每间隔指定的时间,做指定的事情;
Java中的定时器:
1.java.util.TimerTask(抽象类):定义任务;
1).自定义类,继承自TimerTask;
2).重写run()方法;
2.java.util.Timer(类):定义"定时器":
1).构造方法:Timer();
2).定时器方法:
public void schedule(TimerTask task, long delay):是在指定的delay延迟之后,启动task
public void schedule(TimerTask task,long delay,long period):在指定的delay延迟之后,开始,并每隔period时间重复的做task
class MyTimerTask extends TimerTask { private Timer timer; public MyTimerTask(Timer t) { this.timer = t; } @Override public void run() { for (int i = 0; i < 10; i++) { System.out.println("i = " + i); } // this.timer.cancel();//如果要指定"重复",就不能停止; } } public class Demo { public static void main(String[] args) { Timer timer = new Timer(); System.out.println("启动定时器,2秒后开始......"); // timer.schedule(new MyTimerTask(timer), 1000 * 2); timer.schedule(new MyTimerTask(timer), 1000 * 2, 1000); } }
----------------------------------------java.lang.Runtime类----------------------------------------
java.lang.Runtime类:每个 Java 应用程序都有"一个" Runtime 类实例
public class Demo { public static void main(String[] args) throws IOException { Runtime run = Runtime.getRuntime(); // 通过run方法运行应用程序 run.exec("notepad"); run.exec("calc"); } }