好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

JAVA日期处理类详解

Date类

java.util.Date类表示特定的瞬间,精确到毫秒需要导包。注意,此时Date类中多个包中都存在,不要导错导。

构造方法

?

1

2

3

4

Date()

分配 Date 对象并初始化此对象,以表示分配它的时间(精确到毫秒)。

Date( long date)

分配 Date 对象并初始化此对象,以表示自从标准基准时间(称为[历元(epoch)],即 1970 年 1 月 1 日 00 : 00 : 00 GMT)经过date ms后的指定毫秒数。

简单来说:使用无参构造,可以自动设置当前系统时间的毫秒时刻;指定long类型的构造参数,可以自定义毫秒时刻。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

public class DateDemo {

     public static void main(String[] args) {

         //创建日期对象,把当前的时间转成日期对象

         System.out.println( new Date());

         //创建日期对象,把当前的毫秒值转成日期对象

         System.out.println( new Date(0L));

         //1970年1月1日经过100000000L以后的时间

         System.out.println( new Date(100000000L));

     }

}

//结果

18 15 : 56 : 43 CST 2021

Thu Jan 01 08 : 00 : 00 CST 1970

Fri Jan 02 11 : 46 : 40 CST 1970

Process finished with exit code 0

常用方法

Date类中的多数方法已经过时(被Calender类代替),常用的方法有:

?

1

2

3

4

5

6

long getTime()

返回自 1970 年 1 月 1 日 00 : 00 : 00 GMT 以来此 Date 对象表示的毫秒数。

void setTime( long time)

设置此 Date 对象,以表示 1970 年 1 月 1 日 00 : 00 : 00 GMT 以后 time 毫秒的时间点。

String toString()

把此Date对象转换为以下形式的 String: dow mon dd hh:mm:ss zzz yyyy 其中: dow 是一周中的某一天 (Sun, Mon, Tue, Wed, Thu, Fri, Sat)。

?

1

2

3

4

5

public static void main (String[]args) throws Exception {

     Date date = new Date();

     long time =date.getTime();

     System.out.println(time); //1594016487419

}

Calendar类

java.util.Calendar是日历类,在Date后出现,替换掉了许多Date的方法。该类将所有可能用到的时间信息封装为静态成员变量,方便获取。日历类就是方便获取各个时间属性的。

创建Calendar对象

Calendar为抽象类,由于语言敏感性,Calendar类在创建对象时并非直接创建,而是通过静态方法创建,返回子类GregorianCalendar对象,如下:

?

1

2

static Calendar getInstance()

使用默认时区和语言环境获得一个日历

所以:

?

1

2

//获取当前的日历对象

Calendar instance = Calendar.getInstance();

Calendar类中提供很多成员常量(静态的,由类名Calendar去调,都是int类型),代表给定的日历字段:

字段值 含义
YEAR
MONTH 月(从0开始,可以+1使用)
DAY_OF_MONTH 月中的天(几号)
HOUR 时(12小时制)
HOUR_OF_DAY 时(24小时制)
MINUTE
SECOND
DAY_OF_WEAK 周中的天(周几,周日为1,可以+1使用)

注意:1、西方星期的开始为周日,中国为周一。2、在Calendar类中,月份的表示是以0-11代表1-12月。

常用方法

?

1

2

int get( int field)

返回给定日历字段的值。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

//演示:

     public class DateDemo {

     public static void main(String[] args) {

         Calendar calendar = Calendar.getInstance();

         System.out.println(calendar);

         int year = calendar.get(Calendar.YEAR);

         int month = calendar.get(Calendar.MONTH);

         int day = calendar.get(Calendar.DAY_OF_MONTH);

         int hour = calendar.get(Calendar.HOUR_OF_DAY);

         int minute = calendar.get(Calendar.MINUTE);

         int second = calendar.get(Calendar.SECOND);

         System.out.println(year+ "年" +month+ "月" +day+ "日\t" +hour+ ":" +minute+ ":" +second);

     }

}

   //结果:

java.util.GregorianCalendar[time= 1629281702695 ,areFieldsSet= true ,areAllFieldsSet= true ,lenient= true ,zone=sun.util.calendar.ZoneInfo[id= "Asia/Shanghai" ,offset= 28800000 ,dstSavings= 0 ,useDaylight= false ,transitions= 19 ,lastRule= null ],firstDayOfWeek= 1 ,minimalDaysInFirstWeek= 1 ,ERA= 1 ,YEAR= 2021 ,MONTH= 7 ,WEEK_OF_YEAR= 34 ,WEEK_OF_MONTH= 3 ,DAY_OF_MONTH= 18 ,DAY_OF_YEAR= 230 ,DAY_OF_WEEK= 4 ,DAY_OF_WEEK_IN_MONTH= 3 ,AM_PM= 1 ,HOUR= 6 ,HOUR_OF_DAY= 18 ,MINUTE= 15 ,SECOND= 2 ,MILLISECOND= 695 ,ZONE_OFFSET= 28800000 ,DST_OFFSET= 0 ]

2021 年 7 月 18 日  18 : 15 : 2

Process finished with exit code 0

?

1

2

3

void set( int field, int value)

          将给定的日历字段设置为给定值。

//field:域,成员变量

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

public class DateDemo {

     public static void main(String[] args) {

         Calendar calendar = Calendar.getInstance();

         //设置年为4300年

        calendar.set(Calendar.YEAR, 4300 );

        //设置月为2月

         calendar.set(Calendar.MONTH, 2 );

         //设置日为2日

         calendar.set(Calendar.DAY_OF_MONTH, 2 );

         //设置小时为2时

         calendar.set(Calendar.HOUR_OF_DAY, 2 );

         //设置分钟为2分

         calendar.set(Calendar.MINUTE, 2 );

         //设置秒为2秒

         calendar.set(Calendar.SECOND, 2 );

         System.out.println(calendar);

         int year = calendar.get(Calendar.YEAR);

         int month = calendar.get(Calendar.MONTH);

         int day = calendar.get(Calendar.DAY_OF_MONTH);

         int hour = calendar.get(Calendar.HOUR_OF_DAY);

         int minute = calendar.get(Calendar.MINUTE);

         int second = calendar.get(Calendar.SECOND);

         System.out.println(year+ "年" +month+ "月" +day+ "日\t" +hour+ ":" +minute+ ":" +second);

     }

}

//结果

java.util.GregorianCalendar[time=?,areFieldsSet= false ,areAllFieldsSet= true ,lenient= true ,zone=sun.util.calendar.ZoneInfo[id= "Asia/Shanghai" ,offset= 28800000 ,dstSavings= 0 ,useDaylight= false ,transitions= 19 ,lastRule= null ],firstDayOfWeek= 1 ,minimalDaysInFirstWeek= 1 ,ERA= 1 ,YEAR= 4300 ,MONTH= 2 ,WEEK_OF_YEAR= 34 ,WEEK_OF_MONTH= 3 ,DAY_OF_MONTH= 2 ,DAY_OF_YEAR= 230 ,DAY_OF_WEEK= 4 ,DAY_OF_WEEK_IN_MONTH= 3 ,AM_PM= 1 ,HOUR= 6 ,HOUR_OF_DAY= 2 ,MINUTE= 2 ,SECOND= 2 ,MILLISECOND= 751 ,ZONE_OFFSET= 28800000 ,DST_OFFSET= 0 ]

4300 年 2 月 2 日   2 : 2 : 2

Process finished with exit code 0

?

1

2

abstract void add( int field, int amount)

根据日历的规则,为给定的日历字段添加或减去指定的时间量。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

public class DateDemo {

     public static void main(String[] args) {

         Calendar calendar = Calendar.getInstance();

         //将月份减去5个月(现在是八月)   ~!月是从零开始的

         calendar.add(Calendar.MONTH,- 5 );

         //将年减少2年(现在是2021年)

         calendar.add(Calendar.YEAR,- 2 );

         int year = calendar.get(Calendar.YEAR);

         int month = calendar.get(Calendar.MONTH);

         int day = calendar.get(Calendar.DAY_OF_MONTH);

         int hour = calendar.get(Calendar.HOUR_OF_DAY);

         int minute = calendar.get(Calendar.MINUTE);

         int second = calendar.get(Calendar.SECOND);

         System.out.println( "年:" +year);

         System.out.println( "月:" +month);

     }

}

 

年: 2019

月: 2

//月是从零开始的

Process finished with exit code 0

?

1

2

Date getTime()

返回一个表示此 Calendar 时间值(从历元至现在的毫秒偏移量)的 Date 对象。

?

1

2

3

4

5

6

7

8

9

public class DateDemo {

     public static void main(String[] args) {

         Calendar calendar = Calendar.getInstance();

         Date date = calendar.getTime();

         System.out.println(date);

     }

}

Wed Aug 18 23 : 21 : 51 CST 2021

Process finished with exit code 0

DataFormat类

java.text.DateFormat是日期/时间格式化子类的抽象类,我们通过这个类可以帮我们完成日期和文本之间的转换,也就是可以在Date对象与String对象之间进行来回转换。

格式化 :按照指定的格式,从Date对象转换为String对象。

解析 :按照指定的格式,从String对象转换为Date对象.

由于DateFormat为抽象类,不能直接使用,所以需要常用的子类java.text.SimpleDateFormat。这个类需要一个模式(格式)来指定格式化或解析的标准。

常用构造方法

?

1

2

3

4

5

SimpleDateFormat(String pattern)

用给定的模式和默认语言环境的日期格式符号构造

     //pattern代表日期时间的自定义格式 比如:"yyyy-MM-dd HH:mm:ss";  "yyyy/MM/dd HH:mm:ss";  "yyyy年MM月dd日 HH:mm:ss"

SimpleDateFormat()

用默认的模式和默认语言环境的日期格式符号构造 SimpleDateFormat。

pattern字符串格式规则

字母 日期或时间元素 表示 示例
y Year 1996; 95
M 年中的月份 Month July; 07
D 年中的天数 Number 189
d 月份中的天数 Number 10
m 小时中的分钟数 Number 30
s 分钟中的秒数 Number 55
S 毫秒数 Number 978
a Am/Pm标记 Text Pm
h am/pm中的小时数(1-12) Number 12
k 一天中的小时数(1-24) Number 24
E 星期中的天数 Text Tuesday;Tue

常用方法

?

1

2

public String format(Date date)  

     将Date对象格式化为字符串。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

public class FormatDemo {

     public static void main(String[] args) {

         Date now= new Date();

         // 指定 格式器化 的时间模式(是一个字符串,用特定字符表示不同的时间信息)

         // SimpleDateFormat sdf = new SimpleDateFormat();

         //将日期转为字符串

         SimpleDateFormat sdf = new SimpleDateFormat( "yyyy年MM月dd日 HH:mm:ss" );  

         //将日期转为字符串

         String format = sdf.format(now);

         System.out.println(format);

     }

}

//结果演示

//21-8-19 上午12:15

2021 年 08 月 19 日 00 : 14 : 12

Process finished with exit code 0

?

1

2

3

public Date parse(String source)

     将字符串解析为Date对象。

//!!!source这个字符串和SimpleFormat(String Pattern)里的Pattern这个字符串是同一个!!!!!!否则会报错!

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

public class FormatDemo {

     public static void main(String[] args) throws ParseException {

         Date now= new Date();

         // 指定 格式器化 的时间模式(是一个字符串,用特定字符表示不同的时间信息)

         SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );

         //将符合时间模式的日期字符串转为日期对象

         String timeStr= "2021-05-21 13:14:00" ;

         Date parse = sdf.parse(timeStr);

         System.out.println(parse);

     }

}

//演示

Fri May 21 13 : 14 : 00 CST 2021

Process finished with exit code 0

练习

计算出一个人已经出生了多少天

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

public static void main(String[]args) throws Exception {

     System.out.println( "请输入出生日期格式YYYY-MM-dd" );

     Scanner scanner = new Scanner(System.in);

     //获取出生日期,键盘输入

     String birthdayString= scanner.next();

     //将字符串日期,转成Date对象

     //创建SimpleDateFormat对象,写日期模式

     SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd" );

     //调用方法parse,字符串转成日期对象

     Date birthdayDate=sdf.parse(birthday String);

     //获取今天的日期对象

     Date todayDate = new Date();

     //将两个日期转成毫秒值,Date类的方法getTime

     long birthdaySecond =birthdayDate.getTime();

     long todaySecond =todayDate.getTime();

     long secone=todaySecond - birthdaySecond;

     if (secone< 0 ) {

         System.out.println( "还没出生呢" );

     } else {

         System.out.println(大概已经出生: "" +secone/ 1000 / 60 / 60 / 24 + "天" );

     }

     scanner.close();

}

总结

本篇文章就到这里了希望能给你带来帮助,也希望您能够多多关注的更多内容!

原文链接:https://blog.csdn.net/weixin_51975776/article/details/119792458

查看更多关于JAVA日期处理类详解的详细内容...

  阅读:15次