java中的常用日期类_Java中常用日期处理类

前言

日期处理在Java中一块非常复杂的内容,包含日期的国际化,日期和时间的转换,日期的加减运算,日期的展示格式等问题。而且在一些面试中也可能会有问到,所以整理了一下这部分的内容。主要涉及以下四个类:

Date

Calendar

DateFormat

SimpleDateFormat

由于Date类算是比较常用的,所以这里只整理了后面的3个类。

Calendar 类

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

获取Calendar类的通用的对象

Calendar 提供了一个类方法 getInstance,以获得此类型的一个通用对象。

Calendar now = Calendar.getInstance();

获得并设置日历字段的方法

Calendar可以调用set等方法来设置日历字段的字段值,调用 get 、getTimeInMills、getTime等方法设置日历字段。

Calendar cal Calendar.getInstance();

cal.setTime(new Date());

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

int month = (cal.get(Calendar.MONTH))+1; //从0开始计算

System.out.println(cal.getTime());//输出 Wed Jul 03 12:49:38 CST 2019

System.out.println(year);//输出2019

System.out.println(month);//输出7

日历进行加减法

在Calendar对象的使用**add方法进行时间的加减操作**,其中第二个参数为正数表示“加”,负数表示“减”。

下面是Calendar类中add方法的定义:

/**

* Adds or subtracts the specified amount of time to the given calendar field,

* based on the calendar's rules. For example, to subtract 5 days from

* the current time of the calendar, you can achieve it by calling:

*

add(Calendar.DAY_OF_MONTH, -5).

*

* @param field the calendar field.

* @param amount the amount of date or time to be added to the field.

* @see #roll(int,int)

* @see #set(int,int)

*/

/**

* 翻译如下:

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

* 例如,要从日历的当前时间减去5天,可以通过调用:add(Calendar.DAY_OF_MONTH,-5)来实现它。

* @param字段日历字段。

* @param为要添加到字段的日期或时间量。

**/

abstract public void add(int field, int amount);

通过实例例子来使用一下add方法:

Calendar cal Calendar.getInstance();

cal.setTime(new Date());

System.out.println(cal.getTime());//输出 Wed Jul 03 12:49:38 CST 2019

cal.add(Calendar.YEAR,-1);//日期减1年

cal.add(Calendar.MONTH,3);//日期加3个月

cal.add(Calendar.DAY_OF_MONTH,10);//日期加10天

System.out.println(cal.getTime());//输出Sat Oct 13 12:55:14 CST 2018

System.out.println(cal.get(Calendar.YEAR));//2018

System.out.println(cal.get(Calendar.MONTH));//9

System.out.println(cal.get(Calendar.DAY_OF_MONTH));//13

DataFormat 类

DataFormat是对日期/时间格式化的抽象类,它可以格式化或解析日期或时间,即允许将时间对象进行相互的转换。

获取DataFormat对象实例

DataFormat是抽象类,不能创建该类的对象,需要使用静态方法 getDataInstance、getTimeInstance、getDataTimeInstance来得到日期时间格式对象

DataFormat df = DataFormat.getDataInstance();

//或者

DataFormat df1 = DataFormat.getDateInstance(DateFormat.LONG,LONG FRANCE);

日期格式化字符串

将日期、时间格式化为字符串,主要使用**format**方法。例如

DataFormat df = DataFormat.getDataInstance();

String dfStr = df.format(new Date());

字符串解析为日期

将字符串解析为日期,主要使用**parse**方法。 例如

DataFormat df = DataFormat.getDataInstance();

Date d1 = df.parse("2019-7-3")

SimpleDateFormat 类

SimpleDateFormat是DateFormat的子类,该类在包 java.text中,该类允许用户自定义的日期-时间格式的模式,并利用此模式来显示日期和时间

SimpleDateFormat 对象的创建

使用**new**关键字创建SimpleDateFormat类对象,例如

SimpleDateFormat fmt1 = new SimpleDateFormat();

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

SimpleDateFormat fmt3 = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");

格式化日期时间对象

SimpleDateFormat类提供**format**方法来格式化日期和时间对象,例如:

Date now = new Date();

String str1 = fm1.format(now);

解析字符串为日期对象

SimpleDateFormat类提供**parse**方法来解析字符串为日期和时间对象,返回Date对象。例如:

Date d1 = fmt1.parse("2019-07-03");

完整例子

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Date;

public class Calen {

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

// 使用不同的日期时间方式初始化对象

SimpleDateFormat fmt1 = new SimpleDateFormat();

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

SimpleDateFormat fmt3 = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒 E");

Date now = new Date();

// 利用format将时间日期对象用不同的形式显示出来

System.out.println(fmt1.format(now));

System.out.println(fmt2.format(now));

System.out.println(fmt3.format(now));

// 使用parse解析为时间日期对象

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

Date d1 = fmt4.parse("2019-07-03");// 这里的格式需要与上面的字符串格式相同

System.out.println(d1);

}

}

常用的模式字符串

在日期和时间模式字符串中,未加引号的字母 ‘A’ 到 ‘Z’ 和 ‘a’ 到 ‘z’ 被解释为模式字母,用来表示日期或时间字符串元素。

定义了以下模式字母(所有其他字符 ‘A’ 到 ‘Z’ 和 ‘a’ 到 ‘z’ 都被保留):

字母

日期或时间元素

Y

年Year

M

年中的月份

w

年中的周数

W

月份中的周数

D

年中的天数

d

月份中的天数

F

月份中的星期

E

星期中的天数

H

一天中的小时数(0~23)

k

一天中的小时数(1~24)

K

am/pm 中的小时数(0~11)

h

am/pm 中的小时数(1~12)

m

小时钟的分钟数

s

分钟中的秒数

S

毫秒数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值