700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Java编程学习-日期类(Date Calendar LocalDate...)

Java编程学习-日期类(Date Calendar LocalDate...)

时间:2023-05-04 10:36:19

相关推荐

Java编程学习-日期类(Date Calendar LocalDate...)

第一代日期类(java.util.Date)

1)Date:精确到毫秒,代表特定的瞬间;

2)SimpleDateFormat:格式和解析日期类SimpleDateFormat格式化和解析日期具体类。它允许进行格式化(日期转文本)、解析(文本转日期)和规范化;

Date类实现了Cloneable接口、Comparable接口、Serializable接口,继承Object类;

package com.pero.date_;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;/*** @author Pero* @version 1.0*/public class Date01 {public static void main(String[] args) throws ParseException {//该Date是java.util包下的//默认输出日期格式是国外方式的,通常要对该日期格式进行转换//1.获取当前时间Date d1 = new Date();System.out.println("当前日期为:"+d1);//2.创建一个SimpleDateFormat对象,可以指定格式//指定格式所使用的字母是规定好的,不能更改//G Era标志符 Text AD//y 年 Year 1996;96//M 年中的月份 Month July;jul;07//w 年中的周数 Number 27//D 年终的天数 Number 189//d 月份中的天数 Number 10//F 月份中的星期 Number 2//E 星期中的天数 Text Tuesday;Tue//a Am/Pm标记 Text PM//H 一天中的小时数(0~23) Number 0//k 一天中的小时数(1~24) Number 24//K am/pm中的小时数(0~11) Number 0//h am/pm中的小时数(1~12) Number 12//m 小时中的分钟数 Number 30//s 分钟中的秒数 Number 55//S 毫秒数 Number 978//z 时区 General time zone Pacific Standard Time;PST;GMT-08:00//Z 时区 RFC 822 time zone -0800SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss E");//3.format()方法,将格式化日期赋值给字符串String format = simpleDateFormat.format(d1);//4.输出格式化后的当前日期System.out.println("当前日期为:"+format);Date d2 = new Date(999999999); //通过指定毫秒数得到时间String date = simpleDateFormat.format(d2);System.out.println("通过毫秒数得到的时间为"+date);//parse()方法,可以把一个格式化的String转成对应的Date//直接输出日期都是使用国外格式,通过转换格式可以输出国内格式String s = "1969年12月20日 06:13:20 星期六";//在把String->Date,使用的格式是simpleDateFormat的格式,// 文本格式必须与SimpleDateFormat定义的格式完全相同,// 否则抛出转换异常throws ParseExceptionDate parse = simpleDateFormat.parse(s);System.out.println("parse="+parse);}}

第二代日期类(java.util.Calendar)

1)第二代日期类,主要是Calendar类(日历)。

public abstract class Calendar extends Object implements Serializable,Cloneable,

Comparable<Calendar>

2)Calendar 类是一个抽象类,它为特定瞬间与一组诸如YEAR、MONTH、DAY_OF_MONTH、HOUR等日历,字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法。

package com.pero.date_;import java.util.Calendar;/*** @author Pero* @version 1.0*/public class Calendar_ {public static void main(String[] args) {//Calendar 类是一个抽象类,构造器是protect的//可以通过getInstance()来获取实例//提供大量的方法和字段可使用//创建日历类对象Calendar calendar = Calendar.getInstance();System.out.println(calendar);//获取日历对象的某个日历字段System.out.println("年:"+calendar.get(Calendar.YEAR));//因为Calendar返回月份时是从0开始编号的,输出月份时需要+1System.out.println("月:"+(calendar.get(Calendar.MONTH)+1));System.out.println("日:"+calendar.get(Calendar.DAY_OF_MONTH));System.out.println("小时"+calendar.get(Calendar.HOUR));System.out.println("分钟:"+calendar.get(Calendar.MINUTE));System.out.println("秒:"+calendar.get(Calendar.SECOND));//Calendar没有专门的格式化方法,需要自己来组合显示System.out.println(calendar.get(Calendar.YEAR)+"年"+(calendar.get(Calendar.MONTH)+1)+"月"+calendar.get(Calendar.DAY_OF_MONTH)+"日"+"\t"+//如果显示24小时格式的将Calendar.HOUR->Calendar.HOUR_OF_DAYcalendar.get(Calendar.HOUR)+":"+calendar.get(Calendar.MINUTE)+":"+calendar.get(Calendar.SECOND));}}

第三代日期类(LocalDate、LocalTime、LocalDateTime)

★前两代日期类的不足

JDK1.0中包含了一个java.util.Date类,但是它的大多数方法已经在JDK1.1引入Calendar类之后被弃用了。而Calendar也存在的问题是:

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

2)偏移性:Date中的年份是从1900年开始的,二月份都是从0开始的。

3)格式化:格式化只对Date有用,Calendar则没有。

4)Date类和Calendar类也不是线程安全的;并且不能处理闰秒等问题(每隔两天多出一秒)。

第三代日期常用方法:

1)LocalDate(日期/年月日)、LocalTime(时间/时分秒)、LocalDateTime(日期时间/年月日时分秒)

LocalDate只包含日期,可以获取日期字段;

LocalTime只包含时间,可以获取时间字段;

LocalDateTime包含日期+时间,可以获取日期和时间段。

2)DateTimeFormatter格式日期类

类似于simpleDateFormat

DateTimeFormatter dtf = DateTimeFormatter.ofPatttern(格式);

String str = dtf.format(日期对象);

3)Instant 时间戳

类似于Date,提供了一系列和Date类转换的方式

Instant -> Date:

Date fate = Date.form(instant); //直接使用Date类的静态方法form()

Date -> Instant

Instant instant = date.toInstant(); //date对象使用Date类的方法toInstant()方法

4)第三代日期类更多方法

LocalDateTime类MonthDay类:检查重复事件是否闰年增加日期的某个部分使用plus()方法测试增加时间的某个部分使用minus()方法测试查看一年前和一年后的日期

package com.pero.date_;import java.text.DateFormat;import java.text.SimpleDateFormat;import java.time.Instant;import java.time.LocalDate;import java.time.LocalDateTime;import java.time.LocalTime;import java.time.format.DateTimeFormatter;import java.util.Date;/*** @author Pero* @version 1.0*/public class LocalDate_ {public static void main(String[] args) {//now()方法,返回表示当前日期时间的对象LocalDateTime localDateTime =LocalDateTime.now();//LocalDate.now();//LocalTime.now();System.out.println(localDateTime);System.out.println("年:"+localDateTime.getYear());System.out.println("月:"+localDateTime.getMonth());System.out.println("月:"+localDateTime.getMonthValue());System.out.println("日:"+localDateTime.getDayOfMonth());System.out.println("时:"+localDateTime.getHour());System.out.println("分:"+localDateTime.getMinute());System.out.println("秒:"+localDateTime.getSecond());LocalDate localDate = LocalDate.now();System.out.println(localDate);System.out.println(localDate.getYear());System.out.println(localDate.getMonthValue());System.out.println(localDate.getDayOfMonth());LocalTime localTime = LocalTime.now();System.out.println(localTime);System.out.println(localTime.getHour());System.out.println(localTime.getMinute());System.out.println(localTime.getSecond());//使用DateTimeFormatter 对象来进行格式化// DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");// String format = dateTimeFormatter.format(localDateTime);// System.out.println("格式化后的日期:"+format);//Instant 时间戳Instant instant = Instant.now(); //获取当前日期System.out.println(instant);//Instant -> Date:Date date = Date.from(instant);SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");String format1 = simpleDateFormat.format(date);System.out.println(format1);//Date -> Instant:Instant instant1 = date.toInstant();//提供plus()方法和minus()方法,可以对当前时间进行加或者减//查看800天后是什么时候,输出年月日时分秒LocalDateTime localDateTime1 = localDateTime.plusDays(800);DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");//定义输出格式String format = dateTimeFormatter.format(localDateTime1);//确认输出内容System.out.println("800天后的日期:"+format);//打印输出结果//查看123456分钟前是什么时间,输出年月日时分秒LocalDateTime localDateTime2 = localDateTime.minusMinutes(123456);System.out.println("123456分钟前的日期是:"+dateTimeFormatter.format(localDateTime2));}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。