日期操作类

主要使用java.util包中的Date、Calendar以及java.text包中的SimpleDateFormat.

Date类

//=================================================
// File Name       :	Date_demo
//------------------------------------------------------------------------------
// Author          :	Common

import java.util.Date;

//主类
//Function        : 	Date_demo
public class Date_demo {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		Date date = new Date();										//实例化Date类对象
		System.out.println("当前日期为:"+date);		//输出日期
	}

}

Calendar类

日期操作类

//=================================================
// File Name       :	Date_demo
//------------------------------------------------------------------------------
// Author          :	Common

import java.util.Calendar;
import java.util.GregorianCalendar;

//主类
//Function        : 	Date_demo
public class Calendar_demo {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		Calendar calendar = null;							//声明一个Calendar对象
		calendar = new GregorianCalendar();		//通过子类为其实例化
		System.out.println("年:"+calendar.get(Calendar.YEAR));
		System.out.println("月:"+(calendar.get(Calendar.MONTH)+1));
		System.out.println("日:"+calendar.get(Calendar.DAY_OF_MONTH));
		System.out.println("时:"+calendar.get(Calendar.HOUR_OF_DAY));
		System.out.println("分:"+calendar.get(Calendar.MINUTE));
		System.out.println("秒:"+calendar.get(Calendar.SECOND));
		System.out.println("毫秒:"+calendar.get(Calendar.MILLISECOND));
	}

}

输出

年:2017
月:5
日:17
时:20
分:56
秒:8
毫秒:427

DateFormat类

对java.util.Date进行格式化操作,为了符合中国人的习惯

DateFormat类和MessageFormat类都属于Format类的子类,专门用于格式化数据使用

DateFormat类是一个抽象类,无法直接实例化,但是在此抽象类中提供了一个静态方法,可以直接取得本类的实例。

日期操作类

//=================================================
// File Name       :	DateFormat_demo
//------------------------------------------------------------------------------
// Author          :	Common

import java.util.Date;
import java.text.DateFormat;

//主类
//Function        : 	DateFormat_demo
public class DateFormat_demo {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		DateFormat df1 = null;										//声明DateFormat类对象
		DateFormat df2 = null;										//声明DateFormat类对象
		df1 = DateFormat.getDateInstance();				//取得日期
		df2 = DateFormat.getDateTimeInstance();		//取得日期时间
		System.out.println("DATE:"+df1.format(new Date()));		//格式化日期
		System.out.println("DATETIME:"+df2.format(new Date()));
	}

}
 

指定显示的风格

//=================================================
// File Name       :	DateFormat_demo
//------------------------------------------------------------------------------
// Author          :	Common

import java.util.Date;
import java.util.Locale;
import java.text.DateFormat;

//主类
//Function        : 	DateFormat_demo
public class DateFormat_demo {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		DateFormat df1 = null;										//声明DateFormat类对象
		DateFormat df2 = null;										//声明DateFormat类对象

		
		//取得日期时间,设置日期的显示格式、时间的显示格式
		df1 = DateFormat.getDateInstance(DateFormat.YEAR_FIELD,new Locale("zh","CN"));
		df2 = DateFormat.getDateTimeInstance(DateFormat.YEAR_FIELD,DateFormat.ERA_FIELD,new Locale("zh","CN"));
		System.out.println("DATE:"+df1.format(new Date()));		//格式化日期
		System.out.println("DATETIME:"+df2.format(new Date()));
	}

}

输出

DATE:2017年5月17日
DATETIME:2017年5月17日 下午09时38分22秒 CST

格式化日期操作

首先使用第1个模板将字符串中表示的日期数字取出,然后再使用第2个模板将这些日期数字重新转化为新的格式表示。

//=================================================
// File Name       :	DateFormat_demo
//------------------------------------------------------------------------------
// Author          :	Common

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

//主类
//Function        : 	DateFormat_demo
public class DateFormat_demo {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
//		DateFormat df1 = null;										//声明DateFormat类对象
//		DateFormat df2 = null;										//声明DateFormat类对象
////		df1 = DateFormat.getDateInstance();				//取得日期
////		df2 = DateFormat.getDateTimeInstance();		//取得日期时间
//		
//		//取得日期时间,设置日期的显示格式、时间的显示格式
//		df1 = DateFormat.getDateInstance(DateFormat.YEAR_FIELD,new Locale("zh","CN"));
//		df2 = DateFormat.getDateTimeInstance(DateFormat.YEAR_FIELD,DateFormat.ERA_FIELD,new Locale("zh","CN"));
//		System.out.println("DATE:"+df1.format(new Date()));		//格式化日期
//		System.out.println("DATETIME:"+df2.format(new Date()));
		
		String strDate = "2016-3-11 10:20:30.123";		//定义日期时间的字符串
		String pat1 = "yyyy-MM-dd HH:mm:ss.SSS";		//准备第1个模板,从字符串中提取数字
		String pat2 = "yyyy年MM月dd日HH时mm分ss秒SSS毫秒";		//准备第1个模板,从字符串中提取数字
		SimpleDateFormat sdf1 = new SimpleDateFormat(pat1);			//实例化模板对象
		SimpleDateFormat sdf2 = new SimpleDateFormat(pat2);			//实例化模板对象
		
		Date d = null;
		try{
			d = sdf1.parse(strDate);				//将给定字符串中的日期提取出来
		}catch(ParseException e){				//如果提供的字符串格式有错误,则进行异常处理
			e.printStackTrace();
		}
		System.out.println(sdf2.format(d));				//将日期变成新的格式
	}

}

 输出

2016年03月11日10时20分30秒123毫秒

SimpleDateFormat的parse方法

	public static void main(String[] args) throws ParseException {
		// TODO 自动生成的方法存根
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String dateString = "2017-08-01";
		Date date = sdf.parse(dateString);
		System.out.println(date);
	}

 输出

Tue Aug 01 00:00:00 CST 2017

实现:基于Calendar类

//=================================================
// File Name       :	CalendarClass_demo
//------------------------------------------------------------------------------
// Author          :	Common

import java.sql.Date;
import java.util.Calendar;
import java.util.GregorianCalendar;

//类名:DateTime
//属性:
//方法:
class DateTime {
	private Calendar calendar = null;		//定义一个Calendar对象,可以取得时间

	public DateTime() {									
		super();
		this.calendar = new GregorianCalendar();		//通过Calendar类的子类实例化
	}
	
	public String getDate(){		//得到完整的日期,格式为:yyyy-MM-dd HH:mm:ss.SSS
		//考虑到程序要频繁修改字符串,所以使用StringBuffer提升性能
		StringBuffer buf = new StringBuffer();
		//依次取得时间
		buf.append(calendar.get(Calendar.YEAR)).append("-");
		buf.append(this.addZero(calendar.get(Calendar.MONTH)+1, 2));
		buf.append("-");
		buf.append(this.addZero(calendar.get(Calendar.DAY_OF_MONTH), 2));
		buf.append(" ");
		buf.append(this.addZero(calendar.get(Calendar.HOUR_OF_DAY), 2));
		buf.append(":");
		buf.append(this.addZero(calendar.get(Calendar.MINUTE), 2));
		buf.append(":");
		buf.append(this.addZero(calendar.get(Calendar.SECOND), 2));
		buf.append(".");
		buf.append(this.addZero(calendar.get(Calendar.MILLISECOND), 3));
		
		return buf.toString();
		
	}
	
	public String getDateComplete(){		//得到完整的日期,格式为:yyyy年MM月dd日HH时mm分ss秒SSS毫秒
		//考虑到程序要频繁修改字符串,所以使用StringBuffer提升性能
		StringBuffer buf = new StringBuffer();
		//依次取得时间
		buf.append(calendar.get(Calendar.YEAR)).append("年");
		buf.append(this.addZero(calendar.get(Calendar.MONTH)+1, 2));
		buf.append("月");
		buf.append(this.addZero(calendar.get(Calendar.DAY_OF_MONTH), 2));
		buf.append("日");
		buf.append(this.addZero(calendar.get(Calendar.HOUR_OF_DAY), 2));
		buf.append("时");
		buf.append(this.addZero(calendar.get(Calendar.MINUTE), 2));
		buf.append("分");
		buf.append(this.addZero(calendar.get(Calendar.SECOND), 2));
		buf.append("秒");
		buf.append(this.addZero(calendar.get(Calendar.MILLISECOND), 3));
		buf.append("毫秒");
		
		return buf.toString();
		
	}
	
	//考虑到日期中有前导0,所以在此处加上了补零的方法
	private String addZero(int num,int len){
		StringBuffer s = new StringBuffer(); 
		s.append(num);
		while(s.length()<len){		//如果长度不足,则继续补零
			s.insert(0,"0");					//在第1个位置处补零
		}
		return s.toString();
	}
	
	public String getTimeStamp(){		//得到时间戳:yyyyMMddHHmmssSSS
		StringBuffer buf = new StringBuffer();
		buf.append(calendar.get(Calendar.YEAR));
		buf.append(this.addZero(calendar.get(Calendar.MONTH)+1, 2));
		buf.append(this.addZero(calendar.get(Calendar.DAY_OF_MONTH), 2));
		buf.append(this.addZero(calendar.get(Calendar.HOUR_OF_DAY), 2));
		buf.append(this.addZero(calendar.get(Calendar.MINUTE), 2));
		buf.append(this.addZero(calendar.get(Calendar.SECOND), 2));
		buf.append(this.addZero(calendar.get(Calendar.MILLISECOND), 3));
		
		return buf.toString();
		
	}
	
}

//主类
//Function        : 	CalendarClass_demo
public class CalendarClass_demo {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		DateTime dt = new DateTime();			//实例化DateTime对象
		System.out.println("系统时间:"+dt.getDate());
		System.out.println("中文时间:"+dt.getDateComplete());
		System.out.println("系统时间:"+dt.getTimeStamp());
	}

}

 输出

系统时间:2017-05-17 20:41:06.508
中文时间:2017年05月17日20时41分06秒508毫秒
系统时间:20170517204106508

实现:基于SimpleDateFormat类

//=================================================
// File Name       :	SimpleDateFormat_demo
//------------------------------------------------------------------------------
// Author          :	Common

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


//类名:DateTime
//属性:
//方法:
class DateTime_1 {
	private SimpleDateFormat sdf = null;		//声明日期格式化操作对象,直接对new Date()进行实例化
	
	//得到日期,格式为:yyyy-MM-dd HH:mm:ss.SSS
	public String getDate(){		
		this.sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
		return this.sdf.format(new Date());		
	}
	
	//得到完整的日期,格式为:yyyy年MM月dd日HH时mm分ss秒SSS毫秒
	public String getDateComplete(){		
		this.sdf = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒SSS毫秒");
		return this.sdf.format(new Date());		
	}
	
	//得到时间戳,格式为:yyyyMMddHHmmssSSS
	public String getDateStamp(){		
		this.sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		return this.sdf.format(new Date());		
	}
}

//主类
//Function        : 	SimpleDateFormat_demo
public class SimpleDateFormat_demo {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		DateTime dt = new DateTime();			//实例化DateTime对象
		System.out.println("系统时间:"+dt.getDate());
		System.out.println("中文时间:"+dt.getDateComplete());
		System.out.println("系统时间:"+dt.getTimeStamp());
	}

}

 输出

系统时间:2017-05-17 20:59:26.224
中文时间:2017年05月17日20时59分26秒224毫秒
系统时间:20170517205926224