API-2
Math类
package day4;
public class MathDemo {
public static void main(String[] args) {
System.out.println(Math.PI);
System.out.println(Math.abs(100));
System.out.println(Math.abs(-1.0));
System.out.println(Math.sqrt(121));
System.out.println(Math.pow(100, 2));
System.out.println(2 << 2);
System.out.println(Math.max(15, 20));
System.out.println(Math.min(15.2, 20));
System.out.println(Math.ceil(5.2));//向上取整
System.out.println(Math.floor(1.6));//向下取整
System.out.println(Math.round(11.6));//四舍五入
System.out.println(Math.random());//0到1之间去随机数
}
}
运行结果:
Random类
package day4;
import java.util.Arrays;
import java.util.Random;
public class RandomDemo {
public static void main(String[] args) {
Random random = new Random();
System.out.println(random.nextBoolean());//随机产生一个true或false
System.out.println(random.nextInt());//在整数范围内随机产生一个数
System.out.println(random.nextInt(10));//产生一个0到9的随机数
System.out.println(random.nextInt(11) + 10);//产生一个10-20之间的随机数
//在新声明的数组b中随机赋初值
byte[] b = new byte[5];
random.nextBytes(b);
System.out.println(Arrays.toString(b));
}
}
运行结果:
System类
package day4;
import java.util.Arrays;
public class SystemDemo {
public static void main(String[] args) {
System.out.println(System.getenv());
System.out.println(System.getenv("path"));
//System.out.println(System.getProperties("java.runtime.version"));
//System.exit(0);//在图形化窗口界面中调用,退出虚拟机
System.out.println(System.currentTimeMillis());
long s = System.currentTimeMillis();//记录运行之前的初始时间
for (int i = 0; i < 1000; i++) {
System.out.println(i);
}
System.out.println(System.currentTimeMillis() - s);//测试该程序段的运行时间,单位毫秒
//将数组a复制到数组b
int[] a = {1,2,3,4,5};
int[] b = Arrays.copyOf(a, 8);
System.out.println(Arrays.toString(b));
int[] c = {1, 2, 3, 4, 5,6};
int[] d = new int[10];
System.arraycopy(c, 0, d, 2, c.length);//把数组c从第0个位置开始复制给数组d,让数组d从第2个位置开始存放。
System.out.println(Arrays.toString(d));
}
}
运行结果:
Date类
package day4;
import java.util.Date;
public class DateMemo {
public static void main(String[] args) {
Date date = new Date();
System.out.println(date);//获得当前的时间Thu Dec 17 21:04:10 CST 2020,CST表示时区
//Date date1 = new Date(1608210361964l);//利用已知时间戳时候默认值为long型,所以需要加l
System.out.println(date.getYear()+1900);//获得当前的年
System.out.println(date.getMonth()+1);//获得当前的月份
System.out.println(date.getDate());//获得当前天在本月中的天数
System.out.println(date.getDay());//获得当前天在本周中的天数,即星期几。星期日0
date.setTime(1608210361964l);//得到那一刻的毫秒值,与currentTimeMillis相同
System.out.println(date.getTime());
}
}
运行结果:
Calender 类
package day4;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class CalenderDemo {
public static void main(String[] args) {
//Calender 为抽象类,不可以创建对象,可以通过子类创建对象,或者使用Calendar.getInstance();方法
Calendar calendar = Calendar.getInstance();
System.out.println(calendar);
Calendar calendar1 = new GregorianCalendar();
System.out.println(calendar1.get(Calendar.YEAR));
System.out.println(calendar1.get(Calendar.MONTH) + 1);
System.out.println(calendar1.get(Calendar.DATE));
System.out.println(calendar1.get(Calendar.DAY_OF_MONTH));
System.out.println(calendar1.get(Calendar.DAY_OF_WEEK));
System.out.println(calendar1.get(Calendar.DAY_OF_WEEK_IN_MONTH));
calendar1.set(2021, 12, 25);
System.out.println(calendar1.getTime());
System.out.println(calendar1.getTimeInMillis());
}
}
运行结果:
SimpleFormat类
package day4;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class SimpleDateFormatDemo {
public static void main(String[] args) {
//将日期对象转换为指定格式的字符串
Date date = new Date();
System.out.println(date);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss E");//默认为系统设置时间
SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss E", Locale.CHINA);//中国的本地时间
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss E", Locale.US);//美国的本地时间
String str0 = simpleDateFormat.format(date);
System.out.println(str0);
String str = simpleDateFormat1.format(date);
System.out.println(str);
String str1 = simpleDateFormat2.format(date);
System.out.println(str1);
//将字符串转化为日期
String d = "2020-12-18";
SimpleDateFormat simpleDateFormat3 = new SimpleDateFormat("yyyy-MM-dd");//必须给出字符串的模式,否则在运行时会出现异常
Date date1 = null;
try {
date1 = simpleDateFormat3.parse(d);
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println(date1);
}
}
运行结果: