javaAPI_Date


Date类[*****]

1.Date类概述
类Date表示特定的瞬间,精确到毫秒。

2.构造方法
Date():根据当前的默认毫秒值创建日期对象。
Date(long date):根据给定的毫秒值创建日期对象。

构造方法测试代码:
public static void main(String[] args) {
//构造方法1
Date d = new Date();
System.out.println("d:"+d);
//构造方法2
long time = System.currentTimeMillis();
Date d2 = new Date(time);
System.out.println(d2);
}
//输出结果:
输出俩个相同的日期。

3.常用的成员方法
public long getTime():获取时间,以毫秒为单位(该方法可以用于把date转化为一个毫秒值)
public void setTime(long time):设置时间(该方法用于把一个毫秒值转化为一个date值)


4.DateFormate类概述以及其使用

(1).概述
DateFormate是日期/时间格式化子类的抽象类,它可以与语言无关的方式格式化并解析日期或者是时间。
DateFormate是一个抽象类,所以我们在实际使用的时候使用的是其子类SimpleDateFormate.

(2).SimpleDateFormate的构造方法
public SimpleDateFormate();使用默认的格式对数据进行格式化显示
public SimpleDateFormate(Strng pattern);使用给定的模式的方式格式化显示日期(可以自定义)
public final String format(Date dt);格式化日期

通过这俩个构造方法,可以实现把date转化为String类型的日期:

public static void main(String[] args) {
Date d = new Date();
//使用simpleDateFormate默认构造数据
SimpleDateFormat sdf = new SimpleDateFormat();
String str = sdf.format(d);
System.out.println("默认格式是:"+str);
//使用simpleDateFormate带参构造
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String str2 = sdf1.format(d);
System.out.println("自定义格式是:"+str2);
}

(3).SimpleDateFormate的成员方法

Public Date parse(String source):解析

通过上面方法,可以把String类型的日期转化为Date类型的日期。

public static void main(String[] args) throws ParseException {
String date = "2018-12-12 12:12:12";
//注意:字符串中的日期格式要与我们自定义需求的格式要一致。
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date dt = sdf.parse(date);
System.out.println(dt);

}


5.日期操作的工具类实现

(1).概述
在开发中,对时间的操作是不可或缺的,所以,我们一般就是把这一个对时间的操作封装成为一个工具类来实现里面对时间的一些操作。

(2).代码实现
package com.cn.test29;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
* 这是操作日期的工具类
* @author Administrator
* @version V1.0
* @since 12.29
*/

public class DateUtil {

public DateUtil(){
}


/**
* 日期转String
* @param Date dt:要转化的日期
* @param String formate:要转化成的格式
* @author Administrator
* @return String:日期的字符串表示
*
*/
public static String dateToString(Date dt,String formate){
return new SimpleDateFormat(formate).format(dt);
}


/**
* 字符串解析成为日期
* @param String str:要解析的字符串
* @param String formate:要解析成为的格式
* @throws ParseException
* @author Administrator
* @return Date:字符串的解析结果
*
*/
public static Date stringToDate(String str,String formate) throws ParseException{
return new SimpleDateFormat(formate).parse(str);
}

}


6.Calender类概述以及基本使用

(1).概述
Calender类是一个抽象类,它为特定的瞬间与一组诸如:year,month,day_of_month,hour等日历字段之间的转化提供了一些方法。


(2).常用方法
A:public int get(int field):返回给定日历字段的值。日历类中,每一个日历字段都是静态的成员变量,并且都是int值。
B:public static Calendar getInstance():返回一个Calender的日历对象

//使用代码测试:
public static void main(String[] args) {
//由于Calender是抽象方法,不能够穿件对象,但是其提供了一个静态方法getInstance()用于返回一个其子类对象。
Calendar cd = Calendar.getInstance();
//获取年
int year = cd.get(Calendar.YEAR);
//获取月
int month = cd.get(Calendar.MONTH);
//获取月中的日
int day = cd.get(Calendar.DAY_OF_MONTH);

System.out.println(year + "年" + (month+1) + "月" + day + "日");
}
//输出结果:2018年12月29日

C:public void add(int field,int amount):根据给定的日历字段和对应时间,来对当前的日历进行操作。
D:public final void set(int year,int month,int day):设置单钱日历的年,月,日

//代码测试:
public static void main(String[] args) {
//由于Calender是抽象方法,不能够穿件对象,但是其提供了一个静态方法getInstance()用于返回一个其子类对象。
Calendar cd = Calendar.getInstance();
//获取年
int year = cd.get(Calendar.YEAR);
//获取月
int month = cd.get(Calendar.MONTH);
//获取月中的日
int day = cd.get(Calendar.DAY_OF_MONTH);
System.out.println(year + "年" + (month+1) + "月" + day + "日");
//对日历进行操作
cd.add(Calendar.MONTH, -5);
year = cd.get(Calendar.YEAR);
//获取月
month = cd.get(Calendar.MONTH);
//获取月中的日
day = cd.get(Calendar.DAY_OF_MONTH);
System.out.println(year + "年" + (month+1) + "月" + day + "日");
//设置时间
cd.set(2011, 12, 12);
year = cd.get(Calendar.YEAR);
//获取月
month = cd.get(Calendar.MONTH);
//获取月中的日
day = cd.get(Calendar.DAY_OF_MONTH);
System.out.println(year + "年" + (month+1) + "月" + day + "日");
}

//输出结果
2018年12月29日
2018年7月29日
2012年1月12日

 

转载于:https://www.cnblogs.com/nwxayyf/p/10193910.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值