首页 > 技术文章 > 33 Date类

flypigggg 2021-03-29 22:06 原文

Date类

java.util.Date类

--- java.sql.Date类

1.两个构造器的使用

>构造器一:Date():创建一个对应当前时间的Date对象
>
>构造器二://创建指定毫秒数的Date对象

2.两个方法的使用

>`toString()`: 显示当前的年、月、日、时、分、秒
>
>`getTime()`: 获取当前Date对象对应的毫秒数(时间戳)
//构造器一:Date():创建一个对应当前时间的Date对象
Date date1 = new Date();
System.out.println(date1.toString());//Sun Mar 28 13:13:27 CST 2021

//返回当前时间与1970年1月1日0时0分0秒之间以毫秒为单位的时间差
System.out.println(date1.getTime())//1616908981009
   
//构造器二://创建指定毫秒数的Date对象
Date date2 = new Date(1616908981009l);
System.out.println(date2.toString());

java.sql.Date对应着数据库中的日期类型的变量

如何实例化

java.sql.Date date3 = new java.sql.Date(1616908981009l);
System.out.println(date3.toString());//2021-03-28

如何将java.util.Date对象转换为java.sql.Date对象

Date date4 = new Date();
java.sql.Date date5 = new java.sql.Date(date4.getTime());

 

SimpleDateFormat

允许进行格式化:日期-->字符串

SimpleDateFormat sdf = new SimpleDateFormat();
//格式化
Date date = new Date();

String format = sdf.format(date);
System.out.println(format);//21-3-29 下午8:16

//按照指定的方式进行格式化
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String format1 = sdf1.format(date);
System.out.println(format1);//2021-03-29 08:16:07

 

解析:字符串--->日期

String str = "21-3-29 下午8:05";
Date date1 = sdf.parse(str);
System.out.println(date1);//Mon Mar 29 20:05:00 CST 2021

//要求字符串必须符合SimpleDateFormat识别的格式,通过构造器参数体现
Date date2 = sdf1.parse("2020-03-29 08:16:07");
System.out.println(date2);//Sun Mar 29 08:16:07 CST 2020

 

 

SimpleDateFormat()默认的模式和语言环境创建对象

public simpleDateFormat(String pattern):该构造方法可以用参数pattern指定的格式创建一个对象,该对象调用: public string format(Date date):方法格式化时间对象date解析[ public Date parse(String source):从给定字符串的开始解析文本,以生成一个日期。

 

日历类

  public void testCalendar(){
        //1.实例化
        //方式一:创建其子类(GregorianCalendar)的对象
        //方式二:调用其静态方法getInstance()
        Calendar calendar = Calendar.getInstance();

        //2.常用方法
        //get()
        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,22);
       days = calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println(days);

        //add()
       calendar.add(Calendar.DAY_OF_MONTH,3);
       days = calendar.get(Calendar.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(Calendar.DAY_OF_MONTH);
        System.out.println(days);
    }

 

 

获取月份时:一月是0,二月是1,12月是11

获取星期时:周日是1,周二是2.。。。周六是7

 

可变性:像日期和时间这样的类应该是不可变的

偏移性:Date中的年份是从1900年开始的,而月份都从0开始

格式化:格式化中只对Date有用,Calendar则不行

此外 它们也不是线程安全的,不能处理闰秒等

 

 

LocalDate、LocalTime、LocalDateTime

说明

LocalDate相较于LocalTime、LocalDateTime 使用频率要高

public class DateTime {
@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, 3, 29, 10, 29, 56);
System.out.println(localDateTime1);

//getXxx()
System.out.println(localDateTime.getDayOfYear());
System.out.println(localDateTime.getDayOfMonth());
System.out.println(localDateTime.getDayOfWeek());
System.out.println(localDateTime.getMonthValue());
System.out.println(localDateTime.getMinute());

//体现不可变性
//with() 设置相关的属性
LocalDate localDate1 = localDate.withDayOfMonth(22);
System.out.println(localDate);//2021-03-29
System.out.println(localDate1);//2021-03-22

LocalDateTime localDateTime2 = localDateTime.withHour(4);
System.out.println(localDateTime);
System.out.println(localDateTime2);

//不可变性
LocalDateTime localDateTime3 = localDateTime.plusMonths(3);
System.out.println(localDateTime);//2021-03-29T22:01:36.935
System.out.println(localDateTime3);//2021-06-29T22:01:36.935

LocalDateTime localDateTime4 = localDateTime.minusDays(6);
System.out.println(localDateTime);//2021-03-29T22:02:46.941
System.out.println(localDateTime4);//2021-03-23T22:02:46.941
}

 

 

Instant类

时间线上的一个瞬时点,可能被用来记录应用程序中的事件时间戳

类似于java.util.Date类

//now():获取本初子午线对应的标准时间
Instant instant = Instant.now();
System.out.println(instant);

//添加时间的偏移量
OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
System.out.println(offsetDateTime);

//toEpochMill():获取自1970年1月1日0时0分0秒(utc)开始的毫秒数
long milli = instant.toEpochMilli();
System.out.println(milli);

//ofEpochMilli():通过给定的毫秒数,获取Instant实例--->Date类的getTime()
Instant instant1 = Instant.ofEpochMilli(15518919819884874L);

 

 

DateTimeFormatter 类

格式化或解析日期、时间

类似于SimpleDateFormat

预定义的标准格式。如:iSo_LoCAL_DATE_TIME;ISo_LOCAL_DATE;ISo_LOCAL_TIME

DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
 //格式化
LocalDateTime localDateTime = LocalDateTime.now();
String str1 = formatter.format(localDateTime);
System.out.println(str1);//2021-03-30T19:11:58.535

//解析
TemporalAccessor parse = formatter.parse("2021-03-30T19:11:58.535");

 

 

本地化相关的格式。如:ofLocalizedDateTime(FormatStyle.LONG)

DateTimeFormatter formatter1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
//格式化
String str2 = formatter1.format(localDateTime);
System.out.println(str2);//21-3-30 下午7:17

DateTimeFormatter formatter2 = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
//格式化
String str3 = formatter2.format(localDateTime);
System.out.println(str3);//2021年3月30日 星期二

 

自定义的格式。如:ofPattern("yyyy-MM-dd hh:mm:ss ")

DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
//格式化
String str4 = formatter3.format(LocalDateTime.now());
System.out.println(str4);//2021-03-30 07:29:10

//解析
TemporalAccessor accessor = formatter3.parse("2021-03-30 07:29:10");
System.out.println(accessor);

推荐阅读