JDK日期时间

package com.xt.java2;

import org.junit.Test;

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

import static java.util.Calendar.DAY_OF_MONTH;

/**
 * ClassName: DateTimeTest
 * Description:jdk8之前的日期时间的API测试
 *              System类中的CurrentTimeMills();
 *              java.util.Date和子类java.sql.Date
 *              SimpleDateFormat
 *              Calendar
 * date: 2022/4/19 10:03
 *
 * @author tongy
 * @since JDK 1.8
 */
public class DateTimeTest {
    /*
    SimpleDateFormat的使用:SimpleDateFormat对日期Date类的格式化和解析
    1.两个操作
    1.1 格式化: 日期-->字符串
    1.2 解析: 格式化的逆过程

    2.SimpleDateFormat的实例化
     */
    @Test
    public void testSimpleDateFormat() throws ParseException {
        //实例化,使用默认的构造器
        SimpleDateFormat sdf = new SimpleDateFormat();

        //格式化:日期-->字符串
        Date date = new Date();
        System.out.println(date);
        String s = sdf.format(date);
        System.out.println(s);
        //解析:字符串-->日期
        String str="2022/4/19 上午10:22";
        Date date1=sdf.parse(str);
        System.out.println(date1);
    }
    @Test
    public void testSimpleDateFormat1() throws ParseException {
        //按照指定的方式格式化和解析:调用带参的构造器
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        //格式化:日期-->字符串
        Date date = new Date();
        String str = sdf.format(date);
        System.out.println(str+"格式化");
        //解析:字符串-->日期 要求:字符串必须是SimpleDateFormat识别的格式(通过构造器参数体现)
        Date date1 = sdf.parse("2022-04-19 10:42:11");
        System.out.println(date1+"解析");
    }
    /*
    练习一:字符串"2022-04-29"转换为java.sql.Date
    练习二:”三天打鱼两天晒网“ 1990-01-01  xxxx-xx-xx打鱼还是晒网
          举例:2022-04-21?总天数
          总天数%5=1,2,3 打鱼    4,0 晒网
          总天数的计算:
          方式一:(date2.getTime()-date1.getTime())/(1000*60*60*24)+1
          方式二:1990-01-01 --> 2021-12-31 + 2022-01-01-->2022-04-21
     */
    @Test
    public void testExer() throws ParseException {
        String birth="1997-06-20";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf.parse(birth);
        java.sql.Date date1=new java.sql.Date(date.getTime());
        System.out.println(date1);
    }
    /*
    Calendar(抽象类)日历类的使用
     */
    @Test
    public void testCalendar(){
        //1.实例化
        //方式1:创建其子类(GregorianCalendar)的对象
        //方式2:调用它的静态方法
        Calendar calendar=Calendar.getInstance();
        System.out.println(calendar);

        //2.常用方法
        //get()
        int days = calendar.get(DAY_OF_MONTH);
        System.out.println(days);
        days = calendar.get(Calendar.DAY_OF_YEAR);
        System.out.println(days);
        //set()
        //Calendar可变性
        calendar.set(DAY_OF_MONTH,28);
        days=calendar.get(DAY_OF_MONTH);
        System.out.println(days);
        //add()
        calendar.add(DAY_OF_MONTH,-7);
        days=calendar.get(DAY_OF_MONTH);
        System.out.println(days);
        //getTime() -- 日历类-->Date
        Date date = calendar.getTime();
        System.out.println(date);
        //setTime() -- Date-->日历类
        Date date1=new Date();
        calendar.setTime(date1);
        days=calendar.get(DAY_OF_MONTH);
        System.out.println(days);
    }

}

package com.xt.java2;

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;

/**
 * ClassName: JDK8DateTimeTest
 * Description:jdk8 中日期时间的测试
 * date: 2022/4/21 17:33
 *
 * @author tongy
 * @since JDK 1.8
 */
public class JDK8DateTimeTest {
    @Test
    public void testDate(){
        //偏移量
        Date date=new Date(2022-1900,4-1,28);
        System.out.println(date);//Thu Apr 28 00:00:00 CST 2022
    }
    /*
    LocalTime、LocalDate、LocalDateTime的使用
    说明:
        1.LocalDateTime相较于LocalTime、LocalDate,使用频率要高一些
        2.类似于Calendar
     */
    @Test
    public void test1(){
        //获取当前的日期、时间、日期+时间
        LocalTime localTime = LocalTime.now();
        LocalDate localDate = LocalDate.now();
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localTime);
        System.out.println(localDate);
        System.out.println(localDateTime);

        //of():设置指定的年、月、日、时、分、秒是没有偏移量的
        LocalDateTime localDateTime1 = localDateTime.of(2022, 4, 28, 19, 00, 43);
        System.out.println(localDateTime1);

        //getXxx()获取相关的属性
        System.out.println(localDateTime.getDayOfMonth());
        System.out.println(localDateTime.getDayOfWeek());
        System.out.println(localDateTime.getMonth());
        System.out.println(localDateTime.getYear());

        //体现以上3个类的不可变性
        //withXxx():设置相关属性
        LocalDate localDate1 = localDate.withDayOfMonth(22);
        System.out.println(localDate);
        System.out.println(localDate1);
        LocalDateTime localDateTime2 = localDateTime.withHour(5);
        System.out.println(localDateTime);
        System.out.println(localDateTime2);
        //不可变性
        LocalDateTime plusMonths = localDateTime.plusMonths(3);
        System.out.println(plusMonths);
        LocalDateTime minusDays = localDateTime.minusDays(12);
        System.out.println(minusDays);
    }
    /*
    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);
        //toEpochMilli():获取自1970年1月1日0时0分0秒对应的毫秒数 -->Date类的getTime()
        long epochMilli = instant.toEpochMilli();
        System.out.println(epochMilli);

        //ofEpochMilli():通过给定的毫秒数,获取Instant实例  -->Date(Long millis)
        Instant ofEpochMilli = Instant.ofEpochMilli(1650593009205L);
        System.out.println(ofEpochMilli);
    }
    /*
    DateTimeFormatter:格式化或解析日期、时间
    类似于SimpleDateFormat
     */
    @Test
    public void test3(){
        //方式一:预定义的标准格式
        DateTimeFormatter formatter=DateTimeFormatter.ISO_LOCAL_DATE_TIME;
        //格式化:日期-->字符串
        LocalDateTime localDateTime=LocalDateTime.now();
        String format = formatter.format(localDateTime);
        System.out.println(localDateTime);
        System.out.println(format);

        //解析:字符串-->日期
        TemporalAccessor parse = formatter.parse("2022-04-22T11:22:15.2600813");
        System.out.println(parse);

        //方式二:本地化相关的格式:ofLocalizedDateTime
        DateTimeFormatter formatter1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
        String str = formatter1.format(localDateTime);
        System.out.println(str);  //2022/4/22 上午11:31
        //会报异常
//        DateTimeFormatter formatter2 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);
//        String str2 = formatter2.format(localDateTime);
//        System.out.println(str2);  //2022/4/22 上午11:31

        //(重点)方式三:自定义的格式,如:ofPattern("yyyy-MM-dd hh:mm:ss")
        DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
        //格式化
        String format1 = formatter2.format(LocalDateTime.now());
        System.out.println(format1);
        //解析
        TemporalAccessor accessor = formatter2.parse("2022-04-22 04:13:28");
        System.out.println(accessor);

    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值