日期时间类

java.util.Date类、java.sql.Date类

1.两个构造器的使用

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

2.两个方法的使用

tostring():显示当前的年、月、日、时、分、秒
getTime():获取当前Date对象对应的毫秒数。(时间戳)

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

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

public void test2(){
    //构造器一: Date():创建一个对应当前时间的Date对象
    Date date1 = new Date();
    system.out.println(date1.toString());//Sat Feb 16 16:35:31 GMT+08:00 2019
    system.out.print1n(date1.getTime());//1550306204104
    //构造器二:创建指定毫秒数的Date对象
    Date date2 = newDate(155030620410L);
    system.out.println(date2.toString());
    //创建java.sql.Date对象
    java.sql.Date date3 = new java.sql.Date(35235325345L);
    System.out.println(date3);//1971-02-13
    //如何将java.util.Date对象转换为java.sqL.Date对象
    //情况一:
    //Date date4 = new java.sqL.Date( 2343243242323L);
    //java.sqL.Date date5 = (java.sqL.Date) date4;
    //情况二:
    Date date6 = new Date();
    java.sql.Date date7 = new java.sql.date(date6.getTime());
}

SimpleDateFormat类

SimpleDateFormat的使用: SimpleDateFormat对日期Date类的格式化和解析
1.两个操作:
1.1格式化:日期--->字符串
1.2解析:格式化的逆过程,字符串--->日期
2.SimpleDateFormat的实例化

@Test
public void testSimpleDateFormat() throws ParseException {
    //实例化SimpLeDateFormat
    SimpleDateFormat sdf = new simpleDateFormat();
    //格式化:日期--->字符串
    Date date = new Date();
    System.out.println(date);
    
    String format = sdf.format(date);
    System.out.println(format);
    
    //解析:格式化的逆过程,字符串--->日期
    String str = "19-12-18 上午11:43";
    Date date1 = sdf.parse(str);
    System.out.println(date1);
    
    //************按照指定的方式格式化和解析:调用带参的构造器*****************
    //SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyy.MMMM.dd GGG hh:mm aaa")
    SimpleDateFormat sdf1 = new SimpleDateFormat(pattern:"yyyy-MM-dd hh:mm:ss");
    //格式化
    String format1 = sdf1.format(date);
    System.out.println(format1);//2019-02-18 11:48:27
    //解析:要求字符串必须是符合SimpleDateFormat识别的格式(通过构造器参数体现),否则,抛异常
    Date date2 = sdf1.parse(source: "2020-02-18 11:48:27");
    System.out.print1n(date2);
}

Calendar日历类

Calendar日历类(抽象类)的使用

@Test
public void testcalendar(){
    //1.实例化
    //方式一:创建其子类(GregorianCalendar)的对象
    //方式二:调用其静态方法getInstance()
    Calendar calendar = Calendar.getInstance();
//    System.out.println(calendar.getclass());
    //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,amount:-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.print1n(days);
}

LocaLDate、LocalTime、LocalDateTime类

LocaLDate、LocalTime、LocalDateTime的使用

说明:
1.LocaLDateTime相较于LocalDate、LocalTime,使用频率要高

@Test
public void test1(){
    //now():获取当前的日期、时间、日期+时间
    LocalDate localDate = LocalDate.now();
    LocalTime localTime = LocalTime.now();
    LocalDateTime localDateTime = LocalDateTime.now();
    System.out.print1n(localDate);
    System.out.print1n(localTime) ;
    System.out.println(localDateTime) ;
    //of():设置指定的年、月、日、时、分、秒。没有偏移量
    LocalDateTime localDateTime1 = LocalDateTime.of(year:2020,month:10,dayOfMonth,6,hour,13,minute:23,second:43);
    System.out.print1n( localDateTime1);
    // 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(22);
    System.out.println(localDate);
    System.out.println(localDate1);
    
    LocalDateTime localDateTime2 = localDateTime.withHour(4);
    System.out.print1n(localDateTime);
    System.out.println( localDateTime2);
    //不可变性
    LocalDateTime localDateTime3 = localDateTime.plusMonths(3 );
    System.out.println(localDateTime);
    System.out.print1n(localDateTime3);
    LocalDateTime localDateTime4 = localDateTime.minusDays(6);
    System.out.println( localDateTime);
    System.out.println( localDateTime4);
}

Instant

Instant的使用
类似于java.util.Date类

@Test
public void test2(){
    //now():获取本初子午线对应的标准时间
    Instant instant = Instant.now();
    System.out.print1n(instant);//2019-02-18T07:29:41.719Z
    //添加时间的偏移量
    OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
    System.out.println(offsetDateTime);//2019-02-18T15:32:50.611+08:00
    //toEpochMilli():获取自1970年1月1日0时0分秒(UTC)开始的毫秒数 --->Date类的getTime()
    long milli = instant.toEpochMilli();
    System.out.println(milli);
	//ofEpochMilli():通过给定的毫秒数,获取Instant实例-->Date(Long millis)
    Instant instant1 = Instant.ofEpochMilli(1550475314878L);
    System.out.println(instant1);
}

DateTimeFormatter

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(localDateTime);
System.out.println(str1);//2019-02-18T15:42:18.797

//解析:字符串-->日期
TemporalAccessor parse = formatter.parse(text:"2019-02-18T15:42:18.797");
System.out.print1n(parse);

方式二:

//本地化相关的格式.如:ofLocalizedDateTime()
//FormatStyle.LONG/FormatStyLe.MEDIUM/FormatStyle.SHORT:适用于LcoalDateTime
DateTimeFormatter formatter1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);
//格式化
String str2 = formatter1.format(localDateTime);
System.out.println(str2);//2019年2月18日下午03时47分16秒
//本地化相关的格式。如:ofLocalizedDate()
//FormatStyle.FULL/FormatStyLe.LONG/FormatStyLe.MEDIUM/FormatStyle.SHORT:适用于LocalDate
DateTimeFormatter formatter2 = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUN);
//格式化
String str3 = formatter2.format(LocalDate.now());
System.out.println(str3);//2019-2-18

重点:方式三:自定义的格式。如: 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);//2019-02-18 03:52:09

//解析
TemporalAccessor accessor = formatter3.parse(text: "2019-02-18 03:52:09");
System.out.println(accessor);
posted @   xiaopiao  阅读(55)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· Windows编程----内核对象竟然如此简单?
点击右上角即可分享
微信分享提示
主题色彩