大家好,今天给各位分享java时间类的一些知识,其中也会对java比较时间大小进行解释,文章篇幅可能偏长,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在就马上开始吧!
本文目录
一、java语言中的date类及 *** 的用法
1、Date和Calendar是Java类库里提供对时间进行处理的类,由于日期在商业逻辑的应用中占据着很重要的地位,所以在这里想对这两个类进行一个基本的讲解,由于技术有限,不到之处请指正。
2、Date类顾名思义,一看就知道是和日期有关的类了,这个类最主要的作用就是获得当前时间了,然而这个类里面也具有设置时间以及一些其他的功能,可是由于本身设计的问题,这些 *** 却遭到众多批评,而这些遭受批评的功能都已移植到另外一个类里面,这就是今天要讲到的第二个类Calendar里面。
3、在讲两个类之前,这里又不能不多提一个类,那就是DateFormat类,这个类是用来格式化日期的,稍后也会讲到。
4、首先,让我们来看一个获取当前时间的例子:
5、System.out.println(date.getTime());上面的语句首先创建了Date的一个对象,接着使用getTime *** 获得当前的时间,但是注意了,输出后的结果确实一串长整型的数字,这是为什么?实际上这是系统根据当前时间计算出来的一个long型的数,至于是如何计算出来的就不在本文中讲述了,那既然这样的话又如何显示正确的时间呢?这就要利用到上面的DateFormat类了,这个类是一个基类,它有一个子类是SimpleDateFormat,具体用法请看下面的代码:
6、SimpleDateFormat dateFm= new SimpleDateFormat("EEEE-MMMM-dd-yyyy");
7、System.out.println(dateFm.format(date));这段代码开始创建了一个Date的对象,用来获取当前时间,而重点就在于后面的SimpleDateFormat对象,这个对继承了DateFormat,利用format *** 对Date对象进行格式化,然后输出,而格式的定制是由用户定制的,EEEE代表星期,MMMM代表月份,而dd代表日,yyyy代表年。使用这个 *** 就可以根据用户自定义的格式进行输出时间。
8、上面介绍了由用户自定义格式的输出时间,下面将来介绍通过JAVA类库提供的标准格式输出时间,这就要用到DateFormat类了,请看以下代码:
9、DateFormat dateFm= DateFormat.getDateTimeInstance(DateFormat.SHORT,
10、System.out.println(dateFm.format(date));这里使用的 *** 和用户自定义的 *** 差不多,只是这里使用的是一个抽象类,由于DateFormat是一个抽象类,所以它不能通过构造函数构造对象,在这里是通过getDateTimeInstance() *** 获得该对象,而所传递的参数就是DateFormat里面定义的一些常量,系统根据这些常量输出当前时间,由于这里使用的是getDateTimeInstance *** ,所以将传递两个常量参数,用来分别格式化日期和当前的时间。
11、上面讲述了如何获得系统时间以及如何格式化输出,那如果想获取或者设置时间当中的某一部分又该如何呢?例如年,月,日。这就要靠Calendar这个类了,这个类也是一个抽象类,它有一个子类GregorianCalendar,接下来我会利用这个子类来演示这个过程,请看以下代码:
12、DateFormat dateFormat= DateFormat.getDateInstance(DateFormat.FULL);
13、GregorianCalendar cal= new GregorianCalendar();
14、System.out.println("System Date:"+ dateFormat.format(cal.getTime()));
15、cal.set(GregorianCalendar.DAY_OF_WEEK,GregorianCalendar.FRIDAY);
16、System.out.println("After Setting Day of Week to Friday:"+
17、dateFormat.format(cal.getTime()));
18、这段代码当中,首先创建了一个DateFormat对象进行格式设置,接着创建了一个GregorianCalendar对象cal,接着使用cal.setTime() *** 设置cal对象中的时间为当前时间,然后通过format格式化由cal.getTime()返回的时间进行输出,后面利用set *** 设置cal的日期为当前星期的FRIDAY,此时cal中存储的时间就是这个星期五的该时刻,而后面利用format格式化输出,假如当前时间为2005年1月27日星期4的11点30分,那么最后将那句将会输出2005年1月28日星期5的11点30分。
二、java里表示时间的类型是什么
1、java.util.Date,和java.util.Calendar是 java的主要的时间类型
2、Java.util.Calendar类是java.util.Date类的一个更加深入,更加全面的替代。Java.util.Calendar类支持java.util.Date的所有功能,此外,Calendar还引入了多语言,多区域的特性,可以根据需要获取不同区域,不同时区的时间,Calendar还增加了比Date更加方便和快捷的许多操作,如获取一年当中的第几个星期,各个月的天数等便捷的 *** 。
3、Java.util.Calendar区别与java.util.Date的几个地方也需要注意一下:首先,Calendar增加了毫秒的时间段,通过它可以获取时间点的毫秒值,而java.util.Date只是精确到秒。其次,Calendar过去年的时候是当前年份比如:2010,而Date获取年份的时获取到的是当前年份-1900的一个值(2010-1900=110,因此,你调用getYear后过去的值就是110)。最后Calendar是一个抽象类,之所以能够实例化,是因为此处的Calendar充当了一个类似于工厂的作用,在getInstance *** 中实例化了Calendar子类GregorianCalendar,并把它返回给客户使用。
4、此外,还有 java.sql.Date, java.sql.Time, java.sql.Timestamp
三、java中的Date类为什么很多 *** 被废弃了
Date类中有很多 *** 都标有删除线,是因为Date类在设计中有很多问题,如getYear指的是1900年以来的年数,getMonth是从0开始的。事实上,不止Date类,Java的其实时间相关类都存在设计问题,以下举些例子,并提供解决方案。
我们通常使用 Date和Calander用作时间处理,其实会有两个问题:
1.Date的缺陷,我们知道 Date的setYear和getYear等函数是删除线显示的
原因在:比如今天是2009-01-04日,那么获取的年竟然是109,所以是有问题的
2.Calender常常用于时间的回卷,经常使用的就是roll(Day_of_Year,-7)就是七天前
但是如果是2009-01-04日,那么七天前是2009-12-28日,而非2008年,这是因为它只对天回卷了,年没有回卷
3、针对这些问题,提供一套日期工具类:
import org.apache.log4j.Logger;
import java.text.ParseException;
import java.text.SimpleDateFormat;
private static Logger logger= Logger.getLogger(AdDateUtil.class);
static public String getNowStr(String format){
SimpleDateFormat sdf= new SimpleDateFormat(format);
String now= sdf.format(new Date());
static public Date getFormatDate(String date, String format){
SimpleDateFormat sdf= new SimpleDateFormat(format);
static public String getDateStr(Date date, String format){
SimpleDateFormat sdf= new SimpleDateFormat(format);
static public String getPadZeroString(String s, int size){
StringBuffer *** = new StringBuffer();
for(int i= 0; i<(size- s.length()); i++){
static public int getDayCountOfMonth(String year, String month){
Calendar cal= Calendar.getInstance();
cal.set(Calendar.YEAR, Integer.parseInt(year));
//月,因为Calendar里的月是从0开始,所以要-1
cal.set(Calendar.MONTH, Integer.parseInt(month)- 1);
return cal.getActualMaximum(Calendar.DAY_OF_MONTH);
static public String getYesterday(String format){
SimpleDateFormat df= new SimpleDateFormat(format);
Calendar now= Calendar.getInstance();
now.roll(Calendar.DAY_OF_YEAR,-1);//昨天
return df.format(now.getTime());
static public String getADay(String format, int diff){
SimpleDateFormat df= new SimpleDateFormat(format);
Calendar now= Calendar.getInstance();
int beforeM= now.get(Calendar.MONTH);
now.roll(Calendar.DAY_OF_YEAR, diff);//
int nowM= now.get(Calendar.MONTH);
//必须进行日期处理,否则2009-01-04日前七天是2009-12-28
return df.format(now.getTime());
static public String getTomorrow(String format){
SimpleDateFormat df= new SimpleDateFormat(format);
Calendar now= Calendar.getInstance();
now.roll(Calendar.DAY_OF_YEAR, 1);//明天
return df.format(now.getTime());
* 2.如果num=2,日期是2008-03-14,则返回的结果为 2008-03-12、2008-03-13
public static String[] getDaysByNum(int num, String date){
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
Calendar cal= Calendar.getInstance();
cal.setTime(getDateFromString(date,"yyyy-MM-dd"));
for(int i= num; i> 0; i--){
cal.add(Calendar.DAY_OF_YEAR,-1);
result[i- 1]= sdf.format(new Date(cal.getTimeInMillis()));
public static Date getDateFromString(String dateStr, String format){
if((dateStr== null)||(format== null)){
throw new Exception("数据类型异常"+ dateStr+"|"+ format);
logger.error("数据类型异常:"+ e);
SimpleDateFormat df= new SimpleDateFormat(format);
static public int getNowYear(){
Calendar cal= Calendar.getInstance();
return cal.get(Calendar.YEAR);
static public int getNowMonth(){
Calendar cal= Calendar.getInstance();
return cal.get(Calendar.MONTH)+ 1;
public static String[] getMonthRang(String year, String month){
String beginDate= year+"-"+ month+"-01";
String endDate= year+"-"+ month+"-"+
getDayCountOfMonth(year, month);
return getDaysByRang(beginDate, endDate);
public static String[] getDaysByRang(String beginDate, String endDate){
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
int num= dateDiff(beginDate, endDate);
Calendar cal= Calendar.getInstance();
cal.setTime(sdf.parse(beginDate));
num= num+ 1;//把开始和结束日期都包含进去
for(int i= 0; i< num; i++){
cal.add(Calendar.DAY_OF_YEAR, 1);
result[i]= sdf.format(new Date(cal.getTimeInMillis()));
public static int dateDiff(String beginDate, String endDate){
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
long day=(end- begin)/(1000* 3600* 24);//除1000是把毫秒变成秒
return Integer.parseInt(Long.toString(day));
public static void main(String[] args){
System.out.println(AdDateUtil.getADay("yyyy-MM-dd",-7));
关于java时间类的内容到此结束,希望对大家有所帮助。