栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java笔记2.13

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java笔记2.13

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中的成员方法

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/975542.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号