Java基础知识点总结--从细节再看Java(四)Java中与日期相关的类Date,SimpleDateFormat,Calendar

一、Date类

Date类可以让我们创建瞬间的时间,它可通过方法来设定时间,可表示任意时间。但Date中大部分方法已经过时,我们只需关注几个方法即可,其余的显示时间的格式我们通过SimpleDateFormat类来实现,后文将会提到该类。

  • 构造函数(主要关心两个)

Date() :创建当前系统时间,有其固定的格式。

public class DateDemo {

	public static void main(String[] args) {

		Date date=new Date();
		
		System.out.println(date);
		//output: Wed Jul 25 00:17:24 CST 2018

		System.out.println(date.toLocaleString());
            //output: 2018-7-25 0:17:24
	}

}

从代码中我们可以看出,Date创建对象输出的默认格式为:Wed Jul 25 00:17:24 CST 2018,其中CST为时区。而Date的toLocaleString()方法可以将date的格式进行转换,方便我们查看,不过这种方式已过时,可作为了解。

Date(long date):根据参数创建指定时间,而这个时间是根据距离1970-1-1 00:00:00的毫秒数,即date参数为0时,时间为1970-1-1 00:00:00,但由于时区的存在这个时间的显示会有时区的区别。

public static void main(String[] args) {

		Date date = new Date(0);
		System.out.println(date);
		//output: Thu Jan 01 08:00:00 CST 1970

		date=new Date(1000*60*60);
		System.out.println(date.toLocaleString());
        //output: 1970-1-1 9:00:00
	}

由于存在时差而且我们在东八区,所以显示的是1970-1-1 8:00:00

 

  • 常用方法

毫秒值 ----> Date

void setTime(long date)

Date  ----> 毫秒值

long getTime()


	public static void main(String[] args) {

		Date date=new Date();
        System.out.println(date);
        //output: Wed Jul 25 00:40:38 CST 2018

		date.setTime(0);
		System.out.println(date);
        //output: Thu Jan 01 08:00:00 CST 1970
		
		System.out.println(date.getTime());
        //output: 0
	}

 

二、DateFormat

属于java.text,这个包中的类多是用来格式化的。DateFormat被abstract关键词修饰,不能创建对象。包含几个方法,需要其导出类来调用,下面将会提到。

 

三、SimpleDateFormat

SimpleDateFormat是DateFormat的子类,用来格式化(Date-->String)和解析(String-->Date)日期的具体类。

  • 格式化(Date-->String)

使用父类DateFormat的方法 String format(Date date),将Date对象按SimpleDateFormat指定的模式转换并返回String类型

  • 解析(String-->Date)

使用父类DateFormat的方法 Date parse(String source),将记录时间的String对象依据指定模式转换并返回Date类型。

注:parse()中的String 参数必须是和创建SimpleDateFormat对象的模式一致,否则会出现异常。

 

  • 构造函数

SimpleDateFormat():使用默认模式创建对象。

public static void main(String[] args) {

		SimpleDateFormat sdf = new SimpleDateFormat();
		Date date = new Date();
		System.out.println(date);
		//output: Wed Jul 25 01:00:36 CST 2018

		String d=sdf.format(date);
		System.out.println(d);
        //output: 18-7-25 上午1:00
	}

从最后的输出结果来看,SimpleDateFormat()的默认模式类似:18-7-25 上午1:00

 

SimpleDateFormat(String pattern):使用指定模式创建对象。

常见的字母以及其代表的模式:

  • y Year
    M Month in year
    D Day in year
    d Day in month
    u Day number of week (1 = Monday, ..., 7 = Sunday)
    a Am/pm marker
    H Hour in day (0-23)
    k Hour in day (1-24)
    K Hour in am/pm (0-11)
    h Hour in am/pm (1-12)
    m Minute in hour
    s Second in minute
public static void main(String[] args) {

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
		Date date = new Date();
		System.out.println(date);
		//output:Wed Jul 25 01:13:28 CST 2018

		String d=sdf.format(date);
		System.out.println(d);
        //output:2018年07月25日 01:13:28
	}

执行格式化后输出的字符串d即按照我们设定的模式:"yyyy年MM月dd日 HH:mm:ss"

 

三、Calendar

Calendar被abstract关键字修饰,因此不能直接去创建Calendar的实例。但类中提供静态方法可以让我们得到Calendar的对象:

Calendar c = Calendar.getInstance(); 之后我们就可以通过c来调用方法。

Calendar中包含对年月日等操作的方法,包括获取,修改,添加,通过Calendar中的静态字段来实现,让我们能够更加方便的对每个字段进行操作。

Calendar中常见的字段:

static int DAY_OF_MONTH

Field number for get and set indicating the day of the month.

static int HOUR

Field number for get and set indicating the hour of the morning or afternoon.

static int MINUTE

Field number for get and set indicating the minute within the hour.

static int MONTH

Field number for get and set indicating the month.

static int SECOND

Field number for get and set indicating the second within the minute.

static int YEAR

Field number for get and set indicating the year.

int get(int field) :返回给定字段的值

public static void main(String[] args) {

		Calendar c = Calendar.getInstance();
		
		int year = c.get(Calendar.YEAR);
		int month = c.get(Calendar.MONTH)+1;
		int day = c.get(Calendar.DAY_OF_MONTH);
		int hour = c.get(Calendar.HOUR);
		int minute = c.get(Calendar.MINUTE);
		int second = c.get(Calendar.SECOND);
		
		System.out.println(year+"年"+month+"月"+day+"日 "+hour+":"+minute+":"+second);
        //output: 2018年7月25日 9:23:29
	}

注:此处month对象在赋值时做了+1操作,原因是month字段所表示的日历中一月份是0。

 

void set(int field, int value):修改指定字段的值,以DAY_OF_MONTH为例

public static void main(String[] args) {

		Calendar c = Calendar.getInstance();
		int day = c.get(Calendar.DAY_OF_MONTH);
		System.out.println(day);
		//output: 25

		c.set(Calendar.DAY_OF_MONTH, 30);
		day = c.get(Calendar.DAY_OF_MONTH);
		System.out.println(day);
		//output: 30
	}

void add(int field, int amount):在指定字段上加指定的amount值

public static void main(String[] args) {

		Calendar c = Calendar.getInstance();
		int day = c.get(Calendar.DAY_OF_MONTH);
		System.out.println(day);
		//output:25

		c.add(Calendar.DAY_OF_MONTH, +1);
		day = c.get(Calendar.DAY_OF_MONTH);
		System.out.println(day);
		//output:26

		c.add(Calendar.DAY_OF_MONTH, -1);
		day = c.get(Calendar.DAY_OF_MONTH);
		System.out.println(day);
		//output:25

	}

四、总结

以后我们用到与日期有关的问题时,就可用SimpleDateFormat类来创建,大体步骤可分为一下几步:

1. 导包 java.text.SimpleDateFormat;  java.util.Date;

2. 创建SimpleDateFormat对象并自定义要输出的日期格式 :SimpleDateFormat(String pattern);

3. 创建Date对象 : new Date()

4. 调用DateFormat的方法format(Date date),返回String类型

 

 

 

猜你喜欢

转载自blog.csdn.net/skisqibao/article/details/81194753