常用类(除去string和包装类)

Date类

  • 日期,时间处理类,jdk7及之前使用,有很大的诟病,比较麻烦

    Long millis = System.currentTimeMillis();//当前相对时间,从1970年首日零点开始计算(计算机诞生)

Calendar类

  • 日历类

其他常用类

  • String SimpleDateDteFormat("yyyy-MM-dd HH:mm:ss");//格式日期工具类字母不可乱写
  • Date() 无参,当前时间

jdk8,日期的处理方式

  • LocalDate类
  • LocalTime类
  • LocalDateTime类
    • 和string类似,不会修改原来的,只会产生新的
    • 不再常量区

Math类

  • round();double四舍五入

BigInteger类

  • 和string类似,不会修改原来的数据,只会产生新的
  • BigInteger(String str);

BigDecimal类

  • 定点数,精确值
  • 支持任意精度的定点数
  • 十进制表示
package com.atguigu.javase.date;
package com.atguigu.javase.date;

import static org.junit.Assert.*;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.junit.Test;

public class DateTest {
    
    @Test       //clendar类,日历类,包含诸多信息,保留数组中,可以通过方法来调用
    public void testName2() throws Exception {
        Calendar calendar = Calendar.getInstance();
        System.out.println(calendar);
        
        //calendar.getYear();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH); // 月份数据比实际小1
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        
        System.out.println(year);
        System.out.println(month);
        System.out.println(day);
        
        //calendar.setYear(2008);
        calendar.set(Calendar.YEAR, 2008);
        calendar.set(Calendar.MONTH, 7);
        calendar.set(Calendar.DAY_OF_MONTH, 10);
        
        System.out.println(calendar.getTime());
        
        calendar.add(Calendar.YEAR, 11);
        calendar.add(Calendar.MONTH, 4);
        calendar.add(Calendar.DAY_OF_MONTH, -500); // 向前走500天
        
        System.out.println(calendar.getTime());
        
        // 获取日历对象, 把它变成你的生日 , 推算你的百岁是哪天.
        //add ,set ,get,get方法需要传入之中的常量来寻找
    }
     
    @Test   //Date类和SimpleDateFormat类,Date类可以获取当前时间util,simple来格式化date类,根据提前传入的字符串,
    public void testName() throws Exception {
        long millis = System.currentTimeMillis(); // 当前时间毫秒数
        System.out.println(millis);
        
        Date date = new Date(); // 当前时间对象
        System.out.println(date);
        
        // 日期格式化器, y表示年, M表示月, d表示日, H表示小时, m表示分钟, s表示秒
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        
        String string = dateFormat.format(date); // 把日期对象格式化成相应的字符串
        System.out.println(string);
        
        String string2 = "1998-03-22 15:22:30";
        Date parse = dateFormat.parse(string2); // 把符合格式的日期字符串解析成日期对象, 字符串要严格匹配模式字符串
        System.out.println(parse);
        
        Date date2 = new Date(2008, 8, 10); // 这个构造器很烂, 简直没法用, 因为年会多1900, 月会多1
        System.out.println(date2);
        
        String format = dateFormat.format(millis); // 还可以格式化毫秒, 很方便
        System.out.println(format);
        
        // 写一个小时钟
    }
}

package com.atguigu.javase.date;

import org.junit.Test;

class Simple {

    @Override   //调用函数来体现gc函数的执行,执行打印,由虚拟机来调用
    protected void finalize() throws Throwable {
        System.out.println("我要死了.....");
    }
    
}

public class SystemTest {
    
    @Test       //测试gc回收器,
    public void testName2() throws Exception {
        Simple simple = new Simple();
        simple = null;
        System.gc(); // gc()方法不是马上执行, 而是交由另外的后台线程去执行.
    }
    
    @Test       //测试finlly和return以及exit()函数的作用效果和优先性
    public void test1() {
        System.out.println("begin");
        try {
            if (true) {
                //return;
                System.exit(0); // 终止JVM
            }
        } finally {
            System.out.println("finally");
        }
        System.out.println("end");
    }
    
    @Test   //测试currentTimeMillis()函数和System类
    public void testName() throws Exception {
        long currentTimeMillis = System.currentTimeMillis();
        System.out.println(currentTimeMillis);
    }
}   

猜你喜欢

转载自www.cnblogs.com/aishuijdemiaomiao/p/9978917.html