JDk 8之前的日期时间的API

/**************************************************************************

package sss;

import org.junit.Test;

import java.util.Date;
/*
JDK 8 之前日期和时间的API测试

 */

public class sss {
/*
java.util.Date类
/–java.sql.Date类
1.两个构造器的使用。
>构造器一:Date():创建一个对应当前时间的Date对象。
>//构造器二:创建指定毫秒数的Date对象。

2.两个方法的使用。
     >toString():显示当前的年、月、日、时、分、秒
     >getTime():获取当前Data对象对应的毫秒数(时间戳)
3. java.sql.Date对应着数据库中的日期类型的变量。
   >如何实例化
   >如何将java.util.Date 对象转化为 java.sql.Date对象。
 */

@Test
public void test2(){

    //构造器一:Date():创建一个对应当前时间的Date对象。
    Date date1 = new Date();
    System.out.println(date1.toString());//Mon Mar 28 19:32:00 CST 2022
    System.out.println(date1.getTime());//1648467188430

    //构造器二:创建指定毫秒数的Date对象。
    Date date2 = new Date(1648467188430L);
    System.out.println(date2.toString());

    //创建java.sql.Date对象
    java.sql.Date date3 = new java.sql.Date(16484267188430L);//Mon Mar 28 19:33:08 CST 2022
    System.out.println(date3);//2492-05-13

    //如何将java.util.Date对象转换为java.sql.Date对象
    //情况一:

// Date date4 = new java.sql.Date(16484267188430L);
// java.sql.Date date5 = (java.sql.Date) date4;
//情况二:
Date date6 = new Date();
java.sql.Date date7 = new java.sql.Date(date6.getTime());
}

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

}
JDk 8之前的日期时间的API
1.System.类中的currenTimeMillis();
2.java.util.Date.和子类java.sql.Date
3.SimpleDateFormat
4.Calendar
//获取月份时:一月是0,二月是1…
//获取星期时:周日是1…

******************************************************************************/
public class Main
{
/

SimpleDateFormat的使用:SimpleDateFormat对日期Date类的格式化

1.格式化:日期--->字符串
2.解析:字符串-->日期

3.SimpleDateFormat的实例化:默认构造器
*/
public static void main(String[] args) {
//实例化SimpleDateFormat
SimpleDateFormat sdf  = new SimpleDateFormat();
//格式化日期
Date date =  new Date();
System.out.println(date);
String format = sdf.format(date);
System.out.println(format);
//解析:格式化的逆过程,字符串-->日期
String str = "2019-08-09";
 Date date1 = sdf.parse(str);
 System.out.println(date1);//异常格式不对
 //4.SimpleDateFormat的实例化:选择构造器
 SimpleDateFormat sdf1 = SimpleDateFormat("yyyy-MM-dd:mm:ss");
     String format1 = sdf.format(date);
    System.out.println(format1);
 //解析
 String str1 = "2019-08-09 11:48:27";
 Date date1 = sdf.parse(st1);
 System.out.println(st1);//正常输出。

/*
Caledar日历类(抽象类)的使用

*/
public static void main(String[] args) {
//1.实例化
//方式一:创建其子类(GregorianCaledar)对象
//方式二:调用其静态方法getInstance()
Calendar calendar = Calendar.getInstance();

//2.常用方法
//get()
 int day1  = calendar.get(Calendar.DAY_OF_MONTH);//当前月份的第几天
 System.out.println(day1);//15
//set()
calendar.set(Calendar.DAY_OF_MONTH,22);//把数值改成自己想要的数值
day1 = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println(day1);//22

//add()
calendar.add(Calendar.DAY_OF_MONTH,2);//把数值增加自己想要的数值
day1 = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println(day1);//24
//getTime():把日历类-->Date
Date date = calendar.getTime();
//setTime():Date--日历类
Date date2 = new Date();
calendar.setTime(date2);

}

}

}
/**************************************************************************
LocalDate,LocalTime,LocalDateTime 的使用
1.LocalDateTime相较于LocalTime与LocalDate,使用频率高

*******************************************************************************/
public class Main{
public static void main (String[] args){

    //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,20,2,20,30);
   
   
    //getXXX获取相关的属性
    System.out.println(localDateTime.getDayOfMonth);
    System.out.println(localDateTime.getDayOfWeek);
    System.out.println(localDateTime.getMonth);
    System.out.println(localDateTime.getMonthValue();
    System.out.println(localDateTime.getMinute);
    //withXXX设置相关的属性
    //体现不可变性
    LocalDate localDate1 = localDate.withDayOfMonth(2);
    System.out.println(localDate);
    System.out.println(localDate1);//localDate不变localDate1变为2
   
    //体现不可变性
    localDateTime localDateTime3 = localDateTime3.plusMonth(3)//minusDays减
    System.out.println(localDateTime);
    System.out.println(localDateTIme3);//localDateTime不变localDateTime1加3个月
}

}
/**************************************************************************
Instant的使用:类似于java.util.Date类。
*******************************************************************************/
public class Main{
public static void main (String[] args){
//本初子午线对应的标准时间
Instant instant = Instant.now();
System.out.println(instant);
OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));//时区加8
System.out.println(offsetDateTime);//中国时间
//获取毫秒数
long milli = instant.toEpochMilli();
System.out.println(milli);
通过给定的毫秒数计算时间
Instant instant1 = Instant.ofEpochMilli(65446456746L);
System.out.println(instant1);

    /*
    DateTimeFormatter:格式化或解析日期,时间
    类似于SimpleDateFormat
    */
    //自定义
   DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
   //格式化
   String str4 = formatter3.format(LocaDateTime.now());
   System.out.println(str4);//2020-02-22
  
   //解析
   TemporalAccessor accessor = format3.parse(2020-02-22);
}}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

晚风strong

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

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

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

打赏作者

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

抵扣说明:

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

余额充值