一.Date类
Date类位于java.util包中,Date类本身使用非常简单,直接输出其实例化对象
import java.util.Date;
public class DateDemo01{
public static void main(String[] args){
Date d = new Date();
System.out.println(d);
}
}
如果现在按照自己需要的格式显示时间,则可以使用Calendar类
二.Calendar类
public abstract class Calendar
Calendar类取得的时间可以精确到毫秒,但是这个类本身是抽象的,所以不能直接实例化,需要依靠多态性,通过子类进行父类的实例化操作,Calendar的子类是GregorianCalendar类
通过Calendar取得完整的日期,使用GregorianCalendar
import java.util.*;
public class DateDemo02{
public static void main(String[] args){
Calendar calendar = new GregorianCalendar();//实例化Calendar对象
System.out.println("ERA: " + calendar.get(Calendar.ERA));
System.out.println("YEAR: " + calendar.get(Calendar.YEAR));
System.out.println("MONTH: " + (calendar.get(Calendar.MONTH)+1));
System.out.println("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH));
System.out.println("AM_PM: " + calendar.get(Calendar.AM_PM));
System.out.println("HOUR_OF_DAY: " + calendar.get(Calendar.HOUR_OF_DAY));
System.out.println("MINUTE: " + calendar.get(Calendar.MINUTE));
System.out.println("SECOND: " + calendar.get(Calendar.SECOND));
System.out.println("MILLISECOND: " + calendar.get(Calendar.MILLISECOND));
}
}
通过此类可以获得完整的日期,注意的是在取得月份的时候要加1.
最好的做法是将Date进行相关的格式化操作
三.DateFormat类
此类是定义在java.text包中的
public abstract class DateFomat extends Format
按照以往的思路,直接使用其子类实例化即可,但是DateFormat类本身的内部提供了可以直接为其实例化的操作
1.得到日期的DateFormat对象:public static final DateFormat getDateInstance()
2.得到日期时间的DateFormat对象:public static final DateFormat getDateTimeInstance()
直接使用DateFormat类完成Date类的转换功能:
public final String format(Date date)
import java.util.Date;
import java.text.DateFormat;
public class DateDemo03{
public static void main(String[] args){
DateFormat df1 = null;//声明一个DateFormat
DateFormat df2 = null;//声明一个DateFormat
df1 = DateFormat.getDateInstance();//得到日期的DateFormat对象
df2 = DateFormat.getDateTimeInstance();//得到日期时间得到DateFormat对象
System.out.println("DATE: "+df1.format(new Date()));//按照日期格式化
System.out.println("DATETIME: "+df2.format(new Date()));//按照日期时间格式化
}
}
通过此类可以将Date类的显示进行合理的格式化操作,此时采用的是默认的格式化操作,也可以通过Locale对象知道要显示的区域。
import java.util.Date;
import java.text.DateFormat;
import java.util.Locale;
public class DateDemo05{
public static void main(String[] args){
DateFormat df1 = null;//声明一个DateFormat
DateFormat df2 = null;//声明一个DateFormat
df1 = DateFormat.getDateInstance(DateFormat.YEAR_FIELD,new Locale("zh","CN"));//得到日期的DateFormat对象
df2 = DateFormat.getDateTimeInstance(DateFormat.YEAR_FIELD,DateFormat.ERA_FIELD,new Locale("zh","CN"));//得到日期时间得到DateFormat对象
System.out.println("DATE: "+df1.format(new Date()));//按照日期格式化
System.out.println("DATETIME: "+df2.format(new Date()));//按照日期时间格式化
}
}
四.SimpleDateFormat类
此类的功能是完成日期的显示格式化的,将一种日期格式变为另一种日期格式,例如:
原始日期:2015-10-10 20:42:30.345
转换后:2015年10月10日 20点42分30秒345毫秒
要进行转换,首先要准备好一个模板,通过此模版进行日期数字的提取工作
在SimpleDateFormat类使用的时候,必须在构造对象时要传入匹配的模板
构造方法:public SimpleDateFormat(String pattern)
转换:public Date parse(String source) throws ParseException
格式化:public final String format(Date date)
import java.util.*;
import java.text.*;
public class DateDemo06{
public static void main(String[] args){
String strDate = "2015-10-10 20:42:30.345";
//准备第一个模板,从字符串中提取出日期数字
String pat1 = "yyyy-MM-dd HH:mm:ss.SSS";
//准备第二个模板,将提取后的日期数字变为指定的格式
String pat2 = "yyyy年MM月dd日 HH点mm分ss秒SSS毫秒";
SimpleDateFormat sdf1 = new SimpleDateFormat(pat1);
SimpleDateFormat sdf2 = new SimpleDateFormat(pat2);
Date d = null;
try{
d = sdf1.parse(strDate);//将给定的日期通过模板一提取出来
}catch(Exception e){
e.printStackTrace();
}
System.out.println(sdf2.format(d));
}
}
五.Calendar类与SimpleDateFormat类的实例
开发中经常需要取得日期,而且每次取得日期的时候代码都会重复,所以既然是重复的代码就可以将其定义成一个类,方便重复调用。注意的是用Calendar获取要注意补0的操作
1.Calendar操作
取出日期:
import java.util.*;
class DateTime{
private Calendar calendar = null;
public DateTime(){// 构造方法直接实例化对象
this.calendar = new GregorianCalendar();
}
public String getDate(){
//因为要频繁修改字符串,所以使用StringBuffer提升性能
StringBuffer buf = new StringBuffer();
buf.append(calendar.get(Calendar.YEAR)).append("-");
buf.append(this.addZero((calendar.get(Calendar.MONTH)+1),2)).append("-");
buf.append(this.addZero(calendar.get(Calendar.DAY_OF_MONTH),2)).append(" ");
buf.append(this.addZero(calendar.get(Calendar.HOUR_OF_DAY),2)).append(":");
buf.append(this.addZero(calendar.get(Calendar.MINUTE),2)).append(":");
buf.append(this.addZero(calendar.get(Calendar.SECOND),2)).append(".");
buf.append(this.addZero(calendar.get(Calendar.MILLISECOND),3));
return buf.toString();
}
private String addZero(int num,int len){
StringBuffer s = new StringBuffer();
s.append(num);
while(s.length() < len){
s.insert(0,"0");
}
return s.toString();
}
}
public class DateDemo07{
public static void main(String[] args){
DateTime dt = new DateTime();
System.out.println(dt.getDate());
}
}
取时间戳:20151010225138507
import java.util.*;
class DateTime{
private Calendar calendar = null;
public DateTime(){// 构造方法直接实例化对象
this.calendar = new GregorianCalendar();
}
public String getTimeStamp(){//取得时间戳
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();
}
private String addZero(int num,int len){
StringBuffer s = new StringBuffer();
s.append(num);
while(s.length() < len){
s.insert(0,"0");
}
return s.toString();
}
}
public class DateDemo07{
public static void main(String[] args){
DateTime dt = new DateTime();
System.out.println(dt.getTimeStamp());
}
}
2.SimpleDateFormat操作
java.util.Date已经是一个完整的日期了,SimpleDateFormat类中存在一个方法,可以针对于Date重新格式化,所以可以将date对象通过SimpleDateFormat类指定好的模板进行相关的格式化
import java.util.*;
import java.text.*;
class DateTime{
private SimpleDateFormat sdf = null;
public String getDate(){
this.sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
return this.sdf.format(new Date());
}
public String getDateComplete(){
this.sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒SSS毫秒");
return this.sdf.format(new Date());
}
}
public class DateDemo07{
public static void main(String[] args){
DateTime dt = new DateTime();
System.out.println(dt.getDate());
System.out.println(dt.getDateComplete());
}
}
<span style="font-size:18px;">
</span>
可以看到,直接使用SimpleDateFormat类取得时间会比使用Calendar类更加方便,而且不需要补0操作,所以开发中基本使用SimpleDateFormat类取得日期的简单做法
参考资
料:
李兴华java基础