Date、Calendar、DateFormat、SimpleDateFormat类
来源:互联网 发布:teamviever网络未就绪 编辑:程序博客网 时间:2024/05/24 03:49
Date类是一个较为常用的类,但其操作的日期格式会有一些不符合个人要求,而如果想要进一步取得一些自己需要的时间,则可以使用Calendar类。
在java.util包中定义了Date类,Date类本身使用非常简单,直接输出其实例化对象即可。
使用Calendar类可以直接将日期精确到毫秒。
public abstract class Calendar
extends Object
Calendar类是一个抽象类,既然是一个抽象类则肯定无法直接使用,此时就要利用对象多态性的概念,通过向上的转型关系实例化本类对象。
通过此类就可以取得一个完整的日期,但是在取得月份时要特别注意,需要增加1.如果按照此方式取得,则也会很麻烦,最好的做法是将Date进行一些相关的格式化操作。
DateFormat类是一个日期的格式化类,专门格式化日期操作,因为java.util.Date类本身就已经包含了完整的日期,所以只需要将此日期按照一些好的格式化显示以下就好了。
观察DateFormat类的定义:此类是定义在java.text包中的。
public abstract class DateFormat
extends Format
但是从定义上可以发现,此类依然是一个抽象类,按照以往的思路,直接使用其子类实例化即可,但是DateFormat类本身的内部提供了可以直接为其实例化的操作。
得到日期的DateFormat对象:public static final DateFormatgetDateInstance()
得到日期时间的DateFormat对象:public static final DateFormatgetDateTimeInstance()
直接使用DateFormat类完成Date类的转换功能:public final String format(Date date)
SimpleDateFormat类的功能是完成日期的显示格式化,例如,在开发中,可能会将一种日期格式变为另一种日期格式,如下所示:
原始日期:2017-8-22 12:57:23.232
转换后日期:2017年8月22日12时57分23秒232毫秒
但是以上的两个日期中的数字是完全一样的,唯一不同的是日期的显示格式不同,所以要想实现这样的转换功能就必须依靠SimpleDateFormat类。
如果想实现转换,则必须首先准备好一个模板,通过此模板进行日期数字的提取工作。
No.
标记
描述
1
y
表示年,年份是四位数字,所以需要使用“yyyy”表示年。
2
M
表示月,月份时两位数字,所以需要使用“MM”表示月。
3
d
表示日,日是两位数字,所以需要使用“dd”表示日。
4
H
表示时,两位数字“HH”表示
5
m
表示分,两位数字“mm”
6
s
表示秒,两位数字“ss”
7
S
表示毫秒,三位数字“SSS”
在SimpleDateFormat类使用的时候,必须注意的是在构造对象时要传入匹配的模板。
构造方法:public SimpleDateFormat(String pattern)
转换:public Date parse(String source) throws ParseExceptionà此时取得的是全部的时间数
格式化:public final String format(Date date)à将时间重新格式化成字符串显示
import java.text.DateFormat;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import java.util.GregorianCalendar;import java.util.Locale;public class Main {public static void main(String[] args) {Date date = new Date();//直接实例化Date对象System.out.println(date);//Date类可直接使用直接打印,格式不是很友好,一般不用Calendar calendar = new GregorianCalendar();//实例化Calendar对象,要用其子类来实例化,Calendar本身是抽象类System.out.println("YEAR:"+calendar.get(Calendar.YEAR));//打印年System.out.println("MONTH:"+(calendar.get(Calendar.MONTH)+1));//月,月要加1System.out.println("DAY_OF_MONTH:"+calendar.get(Calendar.DAY_OF_MONTH));//日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));//毫秒System.out.println("-----------------DateFormat类--------------------------");//DateFormat类不能用构造函数直接实例化,但是其中提供了取得实例的方法,如下DateFormat df1 = null;DateFormat df2 = null;df1 = DateFormat.getDateInstance();//得到日期对象:DATE:2017-8-22df2 = DateFormat.getTimeInstance();//得到时间对象 :DATETIME:12:29:30System.out.println("DATE:"+df1.format(new Date()));//默认的格式化System.out.println("DATETIME:"+df2.format(new Date()));//默认的格式化System.out.println("----------------------国际化的日期时间显示---------------------");DateFormat df3 = null;DateFormat df4 = null;//三种实例化方法包含以上一种和以下两种(df1,df3)//df3 = DateFormat.getDateInstance(DateFormat.YEAR_FIELD);df3 = DateFormat.getDateInstance(DateFormat.YEAR_FIELD, new Locale("zh","CN"));//以下三种实例化方法//df4 = DateFormat.getDateTimeInstance();//df4 = DateFormat.getDateTimeInstance(DateFormat.YEAR_FIELD, DateFormat.ERA_FIELD);df4 = 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()));//按照日期时间格式化System.out.println("-------------------SimpleDateFormat类------------------------");String strDate = "1111-11-11 11:11:11:111";//一个日期String pat1 = "yyyy-MM-dd HH:mm:ss:SSS";//按照这个格式取得日期String pat2 = "yyyy年MM月dd日HH时mm分ss秒SSS毫秒";//按照这个格式显示日期SimpleDateFormat sdf1 = new SimpleDateFormat(pat1);//按照pat1格式初始化一个sdf1对象SimpleDateFormat sdf2 = new SimpleDateFormat(pat2);//按照pat2格式初始化一个sdf2对象Date d = null;//创建一个Date对象try {d = sdf1.parse(strDate);//按照pat1格式取一个日期,注意定义的格式pat1和要显示的日期strDate格式必须相同,不然报错} catch (ParseException e) {// TODO Auto-generated catch blocke.printStackTrace();}System.out.println(sdf2.format(d));//按照pat2显示日期:1111年11月11日11时11分11秒111毫秒DateFormat df5 = null; //df2已经定义过为:df2 = DateFormat.getTimeInstance();看一下两者区别df5 = DateFormat.getDateTimeInstance();System.out.println(df2.format(new Date()));//结果:12:44:00System.out.println(df5.format(new Date()));//结果:2017-8-22 12:44:00}}
- Date、Calendar、DateFormat、SimpleDateFormat类
- 日期操作类(Date、Calendar、DateFormat、SimpleDateFormat)
- Date类、DateFormat抽象类、SimpleDateFormat类和Calendar类
- Date类,Calendar类,DateFormat类,SimpleDateFormat类
- 日期处理类总结:Date/Calendar/GregorianCalendar/DateFormat/SimpleDateFormat类
- 关于java日期类Date,DateFormat,SimpleDateFormat,Calendar总结
- Calendar Date ,DateFormat, SimpleDateFormat的区别
- Java中的Date、Calendar、DateFormat、SimpleDateFormat
- java中Random,Date,Calendar,DateFormat,SimpleDateFormat
- Date、 Calendar、SimpleDateFormat类
- Date,DateFormat,Calendar类用法
- JAVA中的时间:Date类、SimpleDateFormat类、DateFormat类、Calendar类
- java常用类库续1(日期操作类Date、Calendar、DateFormat、SimpleDateFormat、Math与Random类)
- java日期处理类总结:Date/Calendar/GregorianCalendar/DateFormat/SimpleDateFormat类
- java日期处理类总结:Date/Calendar/GregorianCalendar/DateFormat/SimpleDateFormat类 .
- java日期处理类总结:Date/Calendar/GregorianCalendar/DateFormat/SimpleDateFormat类
- java日期处理类总结:Date/Calendar/GregorianCalendar/DateFormat/SimpleDateFormat类
- java日期处理类总结:Date/Calendar/GregorianCalendar/DateFormat/SimpleDateFormat类
- java的值交换
- JAVA-序列化与反序列化
- Unable to open debugger port (127.0.0.1:50694): java.net.SocketException "socket closed"
- consul集群架构
- Uva 10601 Cubes(polya计数)
- Date、Calendar、DateFormat、SimpleDateFormat类
- Hexo快速部署教程
- 数据库中内连接、外连接、全连接
- 最新手机号码、电话号码正则表达式
- 一个简单的springmvc框架写的 helloworld例子
- perl基础学习1
- 等价表达式
- lua function 可变参数
- selenium在eclipse中的使用(详细介绍)