Math类
Math类不可以实例化,只能通过Math类名调用其中的静态方法
因为math类的构造是private访问权限
public class b {
public static void main(String[] args) {
// 返回绝对值
System.out.println(Math.abs(-99));
// 向上扩展(+1减去小数)
System.out.println(Math.ceil(10.9));
// 向下扩展(之间减去小数)
System.out.println(Math.floor(10.5));
// 四舍五入
System.out.println(Math.round(10.5));
// 返回较小值
System.out.println(Math.min(10, 1));
// 返回较大值
System.out.println(Math.max(10, 1));
// 返回10的2.1次幂(类型为double)
System.out.println(Math.pow(10, 2.1));
// 返回一个double类型正值,随机数0.0~0.9999
System.out.println(Math.random());
}
}
System类
System.currentTimeMillis()
System.exit(0);
根据currentTimeMillis()计算当前时间
import java.time.Year;
import java.util.Date;
import javax.management.monitor.MonitorNotification;
import java.text.*;
public class b {
public static void main(String[] args) {
// 当前时间距离1970的秒数,单位毫秒
// System.out.println(System.currentTimeMillis() / 1000 / 60 / 60 / 24 /
// 365+1970 + "年");
double sumday = System.currentTimeMillis() / 1000 / 60 / 60 / 24;// 总天数
// 减去闰年天数
int Year = 1970;
while (Year != 2023) {
if (Year % 4 == 0 && Year % 100 != 0 || Year % 400 == 0) {
sumday--;
}
Year++;
}
System.out.print((1970 + (int) sumday / 365) + "年");
// 计算月和日
int day = (int) sumday % 365;
int[] moonarry = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int i = 0;
while (day > moonarry[i]) {
day = day - moonarry[i];
i++;
}
System.out.print((i + 1) + "月");
// 不存在0日
day++;
System.out.print(day + "日");
// 取余,一天的秒数
long todaysecond = (System.currentTimeMillis() / 1000) % (24 * 60 * 60);
// 一天的秒数/一个小时的秒数
System.out.print(todaysecond / (60 * 60) + "时");
System.out.print(todaysecond / 60 % (60) + "分");
System.out.print(todaysecond % 60 + "秒");
System.out.println();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
Date date = new Date(System.currentTimeMillis());
System.out.println(formatter.format(date));
// return 0?
System.exit(0);
}
}
记得转时区域
Date date = new Date(1503544630000L); // 对应的北京时间是2017-08-24 11:17:10
SimpleDateFormat bjSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 北京
bjSdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai")); // 设置北京时区
SimpleDateFormat tokyoSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 东京
tokyoSdf.setTimeZone(TimeZone.getTimeZone("Asia/Tokyo")); // 设置东京时区
SimpleDateFormat londonSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 伦敦
londonSdf.setTimeZone(TimeZone.getTimeZone("Europe/London")); // 设置伦敦时区
System.out.println("毫秒数:" + date.getTime() + ", 北京时间:" + bjSdf.format(date));
System.out.println("毫秒数:" + date.getTime() + ", 东京时间:" + tokyoSdf.format(date));
System.out.println("毫秒数:" + date.getTime() + ", 伦敦时间:" + londonSdf.format(date));
object类
所有类都之间或者间接继承object类,它是所有类的超类
由于多态,所有类都可以由object类,达到多态的效果
public class b {
public static void main(String[] args) {
Object o1 = new a();
Object o2 = new c();
o1.toString();
o2.toString();
}
}
class a {
@Override
public String toString() {
System.out.println("a这里重写tostring");
return super.toString();
}
}
class c {
@Override
public String toString() {
System.out.println("c这里重写tostring");
return super.toString();
}
}
所以所有类都有tostring方法,也都包含object中的成员方法