147-常用类与基础API-JDK8之前日期时间API的使用:Date、SimpleDateFormat、Calendar

文章详细介绍了Java中的日期时间处理,包括`Date`,`Calendar`,`LocalDateTime`,`Instant`等类的使用方法,以及`SimpleDateFormat`进行日期格式化的示例,并展示了如何在JUnit测试中操作和转换日期对象。
摘要由CSDN通过智能技术生成

package cd_one.code14.exer2;

import org.junit.Test;

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

public class DateTimeTest {
    @Test
    public void test(){
           Date date  = new Date();
        System.out.println(date);

        System.out.println("对应的毫秒数:"+date.getTime());

        Date date2 = new Date(1720303134781l);
        System.out.println(date2);
    }
    @Test
    public void test2(){
        java.sql.Date date1 = new java.sql.Date(1720303134781l);
        System.out.println(date1);//2024-07-07
        System.out.println(date1.getTime());
    }
    @Test
    public void test3() throws ParseException {
//        SimpleDateFormat类,用于时间日期的格式化和解析
        SimpleDateFormat sdf = new SimpleDateFormat();
       //格式化:日期--->字符串
        Date date1 = new Date();
        String strDate = sdf.format(date1);
        System.out.println(strDate);//2024/3/13 下午1:45

        //字符串--->日期
        Date date2 = sdf.parse("2024/3/13 下午1:45");
        System.out.println(date2);//Wed Mar 13 13:45:00 CST 2024
    }
}

 ————————————————————

package cd_one.code14.exer3;

import org.junit.Test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 *
 * 将控制台获取的年月日(比如:2022-12-13)的字符串数据,保存在数据库中
 */
public class DateTimeTest {
    @Test
    public void test2(){
        Calendar calendar = Calendar.getInstance();
        System.out.println(calendar.getClass());

    }
    @Test
    public void test1(){
        Calendar calendar = Calendar.getInstance();

        System.out.println(Calendar.DAY_OF_MONTH);//5
        System.out.println(Calendar.DAY_OF_YEAR);//6


        calendar.set(Calendar.DAY_OF_MONTH,23);
        System.out.println(calendar.get(Calendar.DAY_OF_MONTH));//23
//
        calendar.add(Calendar.DAY_OF_MONTH,3);
        System.out.println(calendar.get(Calendar.DAY_OF_MONTH));//26
        calendar.add(Calendar.DAY_OF_MONTH,-5);
        System.out.println(calendar.get(Calendar.DAY_OF_MONTH));//21

        Date date = calendar.getTime();//21
        System.out.println(date);//Thu Mar 21 19:33:11 CST 2024
        System.out.println(calendar.get(Calendar.DAY_OF_MONTH));//21

    }
    @Test
    public void test3() throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String pattern = "2022-12-13";
        Date date1 = sdf.parse(pattern);
        java.sql.Date date2 = new java.sql.Date(date1.getTime());
        System.out.println(date2);//2022-12-13

    }
    @Test
    public void test4(){
        System.out.println(LocalDate.now());//2024-03-13
        System.out.println(LocalDate.now());//2024-03-13
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDateTime);//2024-03-13T19:42:17.955574800

        System.out.println(LocalDate.of(2024, 3, 13));//2024-03-13
        LocalDateTime localDateTime1 = LocalDateTime.of(2022,11,5,11,23,45);//
        System.out.println(localDateTime1);//2022-11-05T11:23:45

        LocalDateTime localDateTime2 = LocalDateTime.now();
        System.out.println(localDateTime2.getDayOfMonth());//13
        LocalDateTime localDateTime3 = localDateTime2.withDayOfMonth(15);
        System.out.println(localDateTime3);//2024-03-15T19:48:56.394719400
    }

    @Test
    public void test5(){
        Instant instant = Instant.now();
        System.out.println(instant);//2024-03-13T11:53:41.533527600Z
        OffsetDateTime instant1 = instant.atOffset(ZoneOffset.ofHours(8));
        System.out.println(instant1);//2024-03-13T19:53:41.533527600+08:00
        Instant instant2 = Instant.ofEpochMilli(234325544L);
        System.out.println(instant2);//1970-01-03T17:05:25.544Z
        long millTime = instant.toEpochMilli();
        System.out.println(millTime);//1710330821533
        System.out.println(new Date().getTime());//1710330821544
    }
}
package cd_one.code14.exer3;

import cd_one.code6.Exer03.CircleTest;
import org.junit.Test;

import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Date;

/**
 *
 * 使用Calendar获取当前时间,把这个时间设置为你的生日,在获取你的百天(出生后100天)日期。
 */
public class Exer2 {
    @Test
    public void test(){
           Calendar calendar = Calendar.getInstance();
           Date date = calendar.getTime();
        System.out.println("你的生日为:" + date);

        calendar.add(Calendar.DAY_OF_YEAR,100);
        Date newDate = calendar.getTime();
        System.out.println("100天以后是:" + newDate);

        //你的生日为:Wed Mar 13 20:01:38 CST 2024
        //100天以后是:Fri Jun 21 20:01:38 CST 2024
    }
    @Test
    public void test2(){
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println("你的生日为:"+ localDateTime);
        LocalDateTime localDateTime1 = localDateTime.plusDays(100);
        System.out.println("100天以后是:" + localDateTime1);

//        你的生日为:2024-03-13T20:04:11.280317600
//        100天以后是:2024-06-21T20:04:11.280317600
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱吃炫迈的绮绮

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值