Java中Date、SimpleDateFormat、Calendar类

1.Date类
构造方法
Date():根据当前系统时间创建日期对象
Date(long time):根据传入的毫秒值时间创建日期对象
成员方法:
long getTime():获取当前日期对象的毫秒值时间
String toLocaleString():根据本地格式转换日期对象

2.DateFormat类(抽象类)&&SimpleDateFormat类(常用)
SimpleDateFormat类继承.DateFormat类
构造方法:
SimpleDateFormat(String s): 根据指定模板创建日期格式化对象
成员方法:
String format(Date d):根据指定格式格式化日期对象
Date parse(String s):根据指定格式解析字符串

Date d = new Date(3000L);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
System.out.println(sdf.format(d) );

String str = "2088年08月08日 08:08:08";
Date parse = sdf.parse(str);
System.out.println(parse);

3.Calendar类(抽象类)
创建对象方式:

Calendar c = Calendar.getInstance(); //获取日历类对象
成员方法:
	Int get(int n); 获取指定日历字段信息
	void set(int n, int value); 将指定日历字段设置为指定的值
	void add(int n, int value); 将指定日历字段增加或减少指定的值  
Calendar c = Calendar.getInstace();

c.get(Calendar.YEAR);
System.out.println(year);

int month = c.get(Calendar.Month) + 1;

c.set(Calendar.Year, 2088);

System.out.println(c.get(Calendar.YEAR));

c.add(Calendar.Year, 2);
System.out.println(c.get(Calendar.YEAR));

在使用DateFormat对象的parse()方法将字符串解析为日期时,
需要输入固定格式的字符串,这显然不够灵活。
为了能够更好地格式化日期、解析字符串,Java中提供了一个SimpleDateFormat类。

说明:SimpleDateFormat类是DateFormat类的子类,
它可以使用new关键字创建实例对象。
在创建实例对象时,它的构造方法需要接收一个表示日期格式模板的字符串参数。
 

package changYongLei;

import java.text.*;
import java.util.*;

public class GeShiHuaLei_SimpleDateFormat {
  public static void main(String[] args) throws ParseException {
   //创建一个SimpleDateFormat 对象
   SimpleDateFormat sdf = new SimpleDateFormat();
   System.out.println(sdf.format(new Date()));//new Date()相当于创建一个Date类的对象,即:Date d = new Date;然后将d作为参数传过去


   //创建一个SimpleDateFormat 对象
   SimpleDateFormat sdf2 = new SimpleDateFormat("Gyyyy-MM-dd:今天是yyyy年的第D天,E");//此处模板中的字母不能变
   //按SimpleDateFormat对象的日期模式版格式化Date对象
   System.out.println(sdf2.format(new Date()));

   //创建一个SimpleDateFormat对象,并指定日期格式
   SimpleDateFormat  sdf3 = new  SimpleDateFormat("yyyy-MM-dd");
   //定义一个日期格式的字符串
   String s = "2020-5-15";//此处字符串的内容必须要与sdf3对象new出来的字符串内容相同
   //将字符串解析成Date对象
   System.out.println(sdf3.parse(s));
  }
}

一、概述
Calendar类叫:日历类,是方便我们操作日期的,它里面的功能大多数是用来替代java.util.Date类的功能的

二、构造方法
Calendar类可以理解为是工具类,因为它是一个抽象类,所有外界无法通过new的方式创建本类对象

问:如何创建Calendar类的对象呢
答:通过Calendar#getInstance()方法实现
实例

public class Test {
    public static void main(String[] args) {
        Calendar C = Calendar.getInstance();
        System.out.println(C.getTime());
    }
}

三、成员变量
public static final int YEAR:表示年份
public static final int MONT:表示月份
public static final int DATE:表示年终的某一天
public static final int DAYOFMONTH:表示月中的某一天
public static final int DAYOFWEEK:表示星期中的某一天
四、成员方法
public static Calendar getInstance():创建Calendar类型的子类对象
public int get(int field):根据日历字段,获取其对应的值
public void set(int year, int month, int day):设置时间为指定的年,月,日
public void add(int field, int count):修改指定的时间为指定的偏移量

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值