Java 日期、时间类,日期、时间的格式化

Java提供了Date、Calendar两个类用于处理日期、时间。

但Date的大部分构造器、方法已经过时,不在推荐使用,Calendar类又过于复杂,所以Java8推出了一套新的时间日期库。


1、Date类

Date常用的构造函数:

Date()     生成一个代表当前日期时间的Date对象,相当于Date(System.currentTimeMillis())

Date(long date)   指定时间戳,默认单位ms。距1970.1.1   00:00:00的毫秒数。

Date常用方法:

boolean  after(Date  date)    判断是否在该时间日期之后

boolean before(Date date)

long  getTime()    获取时间戳

void  setTime()   设置时间戳

少用Date,尽量用Calendar代替。


2、Calendar类

Calendar类常用方法:

Calendar calendar=Calendar.getInstance();     使用静态方法获取实例,默认为当前的时间日期
calendar.get(Calendar.YEAR)    获取指定字段的值,参数为预定义的常量,返回值均为int。月份比较特殊,0-11,0表示1月。
calendar.set(Calendar.YEAR,2020);   设置指定字段的值

calendar.set(2020,1,1); 同时设置年月日
calendar.set(2020,1,1,1,1,1);   同时设置年月日时分秒

calendar.add(Calendar.YEAR,2); 增加指定字段的值

calendar.add(Calendar.YEAR,-1); 可以为负数

参数、返回值均为int型。



Calendar类示例:

package test;

import java.util.Calendar;

//需要实现Cloneable接口
public class Test{
       public static void main(String[] args) {
           //使用静态方法获取实例,默认为当前的时间日期
           Calendar calendar=Calendar.getInstance();
           //获取年份,打YEAR选择即可
           System.out.println(calendar.get(Calendar.YEAR));
           //获取月份,月份从0-11,0表示1月
           System.out.println(calendar.get(Calendar.MONTH));
           //获取日
           System.out.println(calendar.get(Calendar.DATE));
           //获取时
           System.out.println(calendar.get(Calendar.HOUR));
           //设置指定字段的值
           calendar.set(Calendar.YEAR,2020);
           //同时设置年月日
           calendar.set(2020,1,1);
           //同时设置年月日时分秒
           calendar.set(2020,1,1,1,1,1);
           //增加指定字段的值,年份+1
           calendar.add(Calendar.YEAR,1);
           //可以为负数,年份-1
           calendar.add(Calendar.YEAR,-1);
       }
}

3、Java8新增的时间日期包

LocalDate类:代表当前时区的日期

LocalTime类:代表当前时区的时间

LocalDateTime:代表当前时区的日期时间

以上三个类都提供了静态方法now()获取当前的时间/日期/时间日期对象。

示例:

package test;

import java.time.LocalDateTime;

public class Test{
       public static void main(String[] args){
           //使用静态方法now()获取当前时间日期
           LocalDateTime ldt=LocalDateTime.now();
           //使用plusXxx(int x)增加指定字段的值
           ldt.plusDays(1);   //Day字段的值+1
           //使用minusXxx(int x)方法减少指定的字段的值
           ldt.minusHours(1);   //Day字段的值-1
       }
}

有三种方法可以格式化日期、时间。

1、使用DateFormat类

获取DateFormat实例:

DateFormat.getDateInstance()    只能格式化日期      2019年5月13日

DateFormat.getTimeInstance()    只能格式化时间   下午10:06:07

DateFormat.getDateTimeInstance()    格式化日期时间  2019年5月13日 下午10:06:07

以上方法均为静态方法。

以上方法均有2个重载方法:

指定显示样式,DateFormat类预定义的常量。

DateFormat.getDateInstance(int style)   指定日期显示样式

DateFormat.getTimeInstance(int style)   指定时间显示样式

DateFormat.getDateTimeInstance(int style,int style)    第一个参数指定Date显示样式,第二个参数指定Time显示样式

可用常量:

DateForm.SHORT        2019/5/13   下午10:16

DateForm.MEDIUM  2019年5月13日  下午10:17:06        缺省时默认值就是DateForm.MEDIUM

DateForm.LONG     这个不常用

DateForm.FULL  2019年5月13日星期一  中国标准时间 下午10:18:44

以上三个方法可再加一个参数指定国家:

DateFormat.getDateInstance(int style,Locale.CHINA)   指定日期显示样式

DateFormat.getTimeInstance(int style,Locale.CHINA)   指定时间显示样式

DateFormat.getDateTimeInstance(int style,int style,Locale.CHINA)    第一个参数指定Date显示样式,第二个参数指定Time显示样式

也可以使用其他国家。缺省第二个参数时,会使用默认值。第二个参数一般可以缺省。

DateFormat实例常用的方法:

String  format(Date date)   将Date对象转换为格式化的字符串,并返回字符串

 1 package test;
 2 
 3 import java.text.DateFormat;
 4 import java.text.ParseException;
 5 import java.util.Date;
 6 
 7 public class Test{
 8        public static void main(String[] args) throws ParseException {
 9            //使用静态方法获取实例。只能格式化日期
10            DateFormat df1=DateFormat.getDateInstance();
11            //只能格式化时间
12            DateFormat df2=DateFormat.getTimeInstance();
13            //格式化日期时间
14            DateFormat df3=DateFormat.getDateTimeInstance();
15            //要格式化的Date对象
16            Date date=new Date();
17            //使用format()格式化Date对象
18            System.out.println(df1.format(date));
19            System.out.println(df2.format(date));
20            System.out.println(df3.format(date));
21        }
22 }

2、使用SimpleDateForm类

SimpleDateForm类是DateForm类的子类。SimpleDateForm比DateForm更简单,功能更强大。

示例:

 1 package test;
 2 
 3 import java.text.ParseException;
 4 import java.text.SimpleDateFormat;
 5 import java.util.Date;
 6 
 7 public class Test{
 8        public static void main(String[] args) throws ParseException {
 9            //创建SimpleDateFormat对象,指定样式    2019-05-13 22:39:30
10            SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
11            //2019年的第133天。占位符是特定的
12            SimpleDateFormat sdf2=new SimpleDateFormat("y年的第D天");
13            //要格式化的Date对象
14            Date date=new Date();
15            //使用format()方法格式化Date对象为字符串,返回字符串
16            System.out.println(sdf1.format(date));
17            System.out.println(sdf2.format(date));
18        }
19 }

占位符:

G  "公元"
y  四位数年份
M  月
d  日
h  时 在上午或下午 (1~12)
H  时 在一天中 (0~23)
m  分
s  秒
S  毫秒


E  一周中的周几
D  一年中的第几天
w  一年中第几个星期
a  上午 / 下午 标记符
k   时(1~24)
K     时 在上午或下午 (0~11)

 

DateFormat只能使用特定的日期时间格式,SimpleDateFormat是自定义日期时间格式。

 

 


 

 

3、使用Java8新增的DateTimeFormatter类

DateTimeFormatter相当于DateFormat、SimpleDateFormat的合体,可以使用预定义的日期时间格式,也可以使用自定义的格式。但使用方式有些不同。

 1 package test;
 2 
 3 import java.text.ParseException;
 4 import java.time.LocalDateTime;
 5 import java.time.format.DateTimeFormatter;
 6 import java.time.format.FormatStyle;
 7 
 8 public class Test{
 9        public static void main(String[] args) throws ParseException {
10            //使用预定义的格式。和DateFormat不同,以下三个方法方法均没有重载方法,只能这样用。预定义的常量为FormatStyle类的SHORT、MEDIUM、LONG、FULL
11            DateTimeFormatter df1=DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT);   //只能格式化日期部分
12            DateTimeFormatter df2=DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT);    //只能格式化时间部分
13            DateTimeFormatter df3=DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT,FormatStyle.SHORT);   //格式化日期时间
14 
15            //使用自定义的格式
16            DateTimeFormatter df4=DateTimeFormatter.ofPattern("y-M-d H:m:s");
17 
18            //要格式化的时间日期对象,只能用LocalDateTime。只涉及到日期也可以用LocalDate类,只涉及时间也可以用LocalTime类
19            LocalDateTime ldt=LocalDateTime.now();
20            
21            //格式化,不能用Date类的实例作为参数
22            System.out.println(df1.format(ldt));
23            System.out.println(df2.format(ldt));
24            System.out.println(df3.format(ldt));
25            System.out.println(df4.format(ldt));
26        }
27 }
原文地址:https://www.cnblogs.com/chy18883701161/p/10854547.html