java时间的API

import org.junit.Test;

import java.util.Date;

/**
 * JDK8之前日期和时间的API测试
 * @author: qyx
 * @date: 2022-07-27 17:48
 * @desc:
 */
public class DateTimeTest {

    //System.currentTimeMillis()
    @Test
    public void test1(){
        long time = System.currentTimeMillis();
        //返回当前时间与1970年1月1日0时0分之间的毫秒数时间差
        //称为时间戳
        System.out.println(time);
    }

    /*
    java.util.Date(父类)
            java.sql.Date(子类)
        1.两个构造器的使用
            构造器一:Date()
            构造器二:创建指定毫秒数的Date对象
        2.两个方法的使用
            toString():显示当前的年月日时分秒
            getTime():获取当前Date对象对应的毫秒数。(时间戳)

        3.java.sql.Date对应着数据库中的日期数据类型的变量
            如何实例化
     */
    @Test
    public void test2(){
        //构造器一:Date()
        Date date1 = new Date();
        System.out.println(date1.toString());//Wed Jul 27 18:47:41 CST 2022
        System.out.println(date1.getTime());//1658918861842

        //构造器二:创建指定毫秒数的Date对象
        Date date2 = new Date(1658918861842L);
        System.out.println(date2.toString());
        //创建sql的date对象
        java.sql.Date date3 = new java.sql.Date(546546546456456l);
        System.out.println(date3);
        //如何将util的Date对象转化成sql的Date对象
        //情况一:
//        Date date4 = new java.sql.Date(3425435345l);
//        java.sql.Date date5 = (java.sql.Date)date4;
        //情况二:
        //毫秒数是相同的
        Date date6 = new Date();
        java.sql.Date date7 = new java.sql.Date(date6.getTime());
    }
}







import org.junit.Test;

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

/**
 * jdk 8之前的日期时间的API测试
 * 1.System类中currentTimeMillis();
 * 2.java.util.Date,子类java.sql.Date
 * 3.SimpleDateFormat
 * 4.Calendar
 * @author: qyx
 * @date: 2022-07-29 18:03
 * @desc:
 */
public class DateTimeTest {

    /*
    SimpleDateFormat的使用:对日期类Date的格式化和解析
    1.两个操作
    1.1格式化:日期 --> 字符串
    1.2解析:格式化的逆过程,字符串 --> 日期
     */
    @Test
    public void testSimpleDateFormat() throws ParseException {
        //实例化SimpleDateFormat:使用默认的构造器
        SimpleDateFormat sdf = new SimpleDateFormat();
        Date date = new Date(0l);
        String format = sdf.format(date);
        System.out.println(format);

        //解析:字符串 -->日期
        String str = "70-1-1 上午8:00";
        Date date1 = sdf.parse(str);
        System.out.println(date1);

        //按照指定的方式进行格式化,调用带参数的构造器
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        //格式化
        String format1 = sdf2.format(date);
        System.out.println(format1);
        //解析
        Date parse = sdf2.parse("1996-10-15 11:11:11");
        System.out.println(parse);

    }

    /*
    练习一:字符串”2020-09-08“转换为java.sql.Date
     */
    public void test() throws ParseException {
        String str = "2020-09-08";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf.parse(str);
        java.sql.Date date1 = new java.sql.Date(date.getTime());

    }

    /*
    Calendar日历类(抽象类)的使用
     */
    @Test
    public void testCalendar(){
        //1.实例化
        //方式一:创建子类的对象
        //方式二:调用静态方法getInstance()
        Calendar calendar = Calendar.getInstance();
        System.out.println(calendar.getClass());

        //2.常用方法
        //get()    获取时间     月份从0开始    星期从周日1开始
        int days = calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println(days);
        System.out.println(calendar.get(Calendar.DAY_OF_YEAR));
        //set()    设定时间
        calendar.set(Calendar.DAY_OF_MONTH,20);
        days = calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println(days);
        //add()    增加或减去时间
        calendar.add(Calendar.DAY_OF_MONTH,-3);
        System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
        //getTime()   calendar ---> date
        Date date = calendar.getTime();
        //setTime()   date ---> calendar
        calendar.setTime(date);

        
    }
}







import org.junit.Test;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.time.temporal.TemporalAccessor;
import java.util.Date;

/**
 * jdka8中的日期时间API测试
 * @author: qyx
 * @date: 2022-07-30 11:45
 * @desc:
 */
public class JDK8DateTimeTest {

    @Test
    public void testDate(){
        //偏移量
        Date date1 = new Date(2020 - 1900,9 - 1,8);
        System.out.println(date1);
    }

    /*
    LocalDate,LocalTime,LocalDateTime的使用
     */
    @Test
    public void test1(){
        //now()
        LocalDate localDate = LocalDate.now();
        LocalTime localTime = LocalTime.now();
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDate);
        System.out.println(localTime);
        System.out.println(localDateTime);
        //of():设置指定的年月日时分秒,没有偏移量
        LocalDateTime localDateTime1 = LocalDateTime.of(2020, 10, 6, 13, 23, 43);
        System.out.println(localDateTime1);

        //getXxx()获取相关的属性
        System.out.println(localDateTime1.getDayOfMonth());
        System.out.println(localDateTime1.getDayOfWeek());
        System.out.println(localDateTime1.getDayOfMonth());
        System.out.println(localDateTime1.getMonthValue());
        System.out.println(localDateTime1.getMinute());

        //withXxx()设置(体现不可变性)
        LocalDateTime localDateTime2 = localDateTime1.withDayOfMonth(22);
        LocalDateTime localDateTime3 = localDateTime1.withHour(4);
        System.out.println(localDateTime1);
        System.out.println(localDateTime2);
        System.out.println(localDateTime3);
        
        //plus
        LocalDateTime localDateTime4 = localDateTime1.plusMonths(3);
        System.out.println(localDateTime4);

        //minus
        LocalDateTime localDateTime5 = localDateTime1.minusDays(3);
        System.out.println(localDateTime5);
    }

    /*
    Instant的使用
     */
    @Test
    public void test2(){
        //now():获取本初子午线对应的标准时间
        Instant instant = Instant.now();
        System.out.println(instant);
        //添加时间的偏移量
        OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
        System.out.println(offsetDateTime);

        //获取瞬时点对应的毫秒数    类似于Date的get()
        long milli = instant.toEpochMilli();
        System.out.println(milli);

        //通过给定的毫秒数,获取Instant实例   类似于 new Date(long)
        Instant instant1 = Instant.ofEpochMilli(1659177424982l);
        System.out.println(instant1);
    }

    /*
    DateTimeFormatter:格式化或解析日期,时间
    类似于SimpleDateFormat
     */
    @Test
    public void testDateTimeFormatter(){
        //方式一:预定义的标准格式
        DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
        //格式化:日期-->字符串
        LocalDateTime localDateTime = LocalDateTime.now();
        String str1 = formatter.format(localDateTime);
        System.out.println(localDateTime);
        System.out.println(str1);

        //解析:字符串 -->日期
        TemporalAccessor parse = formatter.parse(str1);
        System.out.println(parse);

        //方式二:本地化相关的格式
        DateTimeFormatter formatter1 = DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG);
        //格式化
        String str2 = formatter1.format(localDateTime);
        System.out.println(str2);

        //方式三:自定义格式(重要)
        DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
        LocalDateTime now = LocalDateTime.now();
        //格式化
        String format = formatter2.format(now);
        System.out.println(format);
        //解析
        TemporalAccessor parse1 = formatter2.parse("2022-07-30 08:19:28");
        System.out.println(parse1);

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值