• Java中关于日期类那些方法


                                                                       转载请注明出处http://blog.csdn.net/harryweasley/article/details/41977633,谢谢

    本篇文章主要介绍Date,DateFormat,SimpleDateFormat,Calendar这四个类

    Date类:

    在java.util包下

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

     
    从 JDK 1.1 開始。应该使用 Calendar 类实现日期和时间字段之间转换,使用 DateFormat 类来格式化和分析日期字符串。

    Date 中的对应方法已废弃。 

    所以Date主要用来生成时间,

    Date有两个构造方法

    Date()
              分配 Date 对象并初始化此对象。以表示分配它的时间(精确到毫秒)。

    Date(long date) 

               分配 Date 对象并初始化此对象,以表示自从标准基准时间(称为“历元(epoch)”。即 1970 年 1 月 1 日 00:00:00 GMT)以来的指定毫秒数。



    		Date date = new Date();
    		Date date2=new Date(0);
    		System.out.println(date);
    		System.out.println(date2);

    输出结果是:

    Tue Dec 16 19:40:32 CST 2014
    Thu Jan 01 08:00:00 CST 1970

    能够看出两个构造函数的差别。这里我个人觉得第二个构造函数,用处太少,由于知道一个时间相对于“历元”的毫秒数,是不现实的。


    Date经常用法:

    boolean after(Date when)        測试此日期是否在指定日期之后
    boolean before(Date when)                    測试此日期是否在指定日期之前
    int compareTo(Date anotherDate)          比較两个日期的顺序
    long getTime() 返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数
    void setTime(long time) 设置此 Date 对象,以表示 1970 年 1 月 1 日 00:00:00 GMT 以后 time 毫秒的时间点

    自我感觉这几个方法都没什么意义,所以就不介绍了。


    DateFormat类:

    在java.text包下

    DateFormat 是日期/时间格式化子类的抽象类,它以与语言无关的方式格式化并解析日期或时间。


    它是抽象类,所以不能构造方法来实例化,能够用getDateInstance()和getDateTimeInstance()这两个静态函数来进行实例化

    这两个的差别是一个返回的是日期,一个返回的是日期+时间。

    同一时候。getDateInstance(int style, Locale aLocale) 

    关于style值:

    FULL:    长度最长 比方:2013年1月9日 星期三
    LONG:   长度更长 比方:January 9, 2013
    MEDIUM:长度比SHORT长 比方:Jan 9,2013
    SHORT: 全然为数字,比方:13/1/9

    说了这么多。赶紧看样例吧:

    		DateFormat df = DateFormat.getDateInstance();
    		// 使用getDateTimeInstance生成format实例。就能够把date和time都显示出来
    		DateFormat df2 = DateFormat.getDateTimeInstance();
    		
    		String s = df.format(date);
    		String s2 = df2.format(date);
    		System.out.println(s);
    		System.out.println(s2);
    输出结果为

    2014-12-16
    2014-12-16 20:01:05

    能够看出一个有日期。有时间,一个仅仅有日期。


    		DateFormat df3 = DateFormat.getDateInstance(DateFormat.FULL,Locale.CHINA);
    		String china_time = df3.format(new Date());
    		DateFormat df4 = DateFormat.getDateInstance(DateFormat.LONG,Locale.CHINA);
    		String g_time = df4.format(new Date());
    		System.out.println("full值:" + china_time);
    		System.out.println("long值:" + g_time);

    输出值为:

    full值:2014年12月16日 星期二
    long值:2014年12月16日


    这里就不在解释了。


    SimpleDateFormat类:

    在java.text包下

    它是DateFormat类的直接子类,继承DateFormat类。我是这么理解SimpleDateFormat类的,它相对于Datef类更接地气。你能够任意给他指定一个形式的日期,进行更改。

    SimpleDateFormat类主要功能是完毕日期之间格式的转换,并且在转换过程中须要採用例如以下步骤:
    1.指定一个模板,并依据这个模板。取出第一个全部的时间数字。

    2.全部的时间数字将採用Date类保存。
    3.将全部的时间数字又一次进行格式转换。
    模板例如以下表,注意区分大写和小写

    日期 模板 描写叙述
    Y 表示年:yyyy
    M 表示月:MM
    d 表示日:dd
    HH 表示时:HH
    mm 表示分:mm
    ss 表示秒:ss
    毫秒 S 毫秒:SSS


    		Date date2 = new Date();
    
    		SimpleDateFormat spf = new SimpleDateFormat("yyyy年-MM月-dd日:HH时-mm分-ss秒");
    
    		System.out.println(spf.format(date2));
    输出为:

    2014年-12月-16日:20时-16分-49秒


    事实上这里能够用sdf.format(System.currentTimeMillis()),而不是用Date类。会更快一些。

    这里有两个方法,一定要搞清楚。一个是spf.format(Date)。一个是spf.parse(String),分别代表。将日期变为新的格式,从给定的字符串中提取出日期。提取出来的日期Date类型。

    同一时候在调用parse这种方法时。会出现异常,

    try {

    }    catch (ParseException e) {
    e.printStackTrace();
    }

    这应该不难理解,可能字符串提取不出日期,就会捕捉异常。


    Calendar类:

    在java.util包下

    Calendar 类是一个抽象类,

    它为“特定瞬间”与一组诸如 “YEAR”、“MONTH”、“DAY_OF_MONTH”、“HOUR ”等日历字段之间的转换提供了一些方法。并为操作日历字段(比如获得下星期的日期)提供了一些方法。

    Calendar实例化有两种方式,第一种是 Calendar nowTime = new GregorianCalendar();。另外一种是Calendar calendar=Calendar.getInstance();


    看样例:

    Calendar nowTime = new GregorianCalendar();// 获取当前系统平台下默认的日期、时间和时区。

    int todayYear = nowTime.get(Calendar.YEAR);// 获取当前系统平台下默认日期的“年”。

    int todayMonth = nowTime.get(Calendar.MONTH) + 1;// 获取当前系统平台下默认日期的“月”。

    int todayDay = nowTime.get(Calendar.DAY_OF_MONTH); // 获取当前系统平台下默认日期的“日”。

    // 输出当前系统平台下的时间。 System.out.println("如今的日期是:" + todayYear + "年" + todayMonth + "月"+ todayDay + "日"); nowTime.set(2013, 2 - 1, 9); System.out.println(nowTime.getTime()); Calendar calendar3 = Calendar.getInstance(); calendar3.add(Calendar.DATE, 10); int year1 = calendar3.get(Calendar.YEAR);// 月份 int month1 = calendar3.get(Calendar.MONTH) + 1;// 日期 int date1 = calendar3.get(Calendar.DATE); System.out.println(year1 + "年" + month1 + "月" + date1 + "日");


    输出为:

    如今的日期是:2014年12月16日
    Sat Feb 09 20:29:30 CST 2013
    2014年12月26日


    呼。

    。。最终完毕了。

    。。


  • 相关阅读:
    设计与声明
    字符串匹配算法——KMP、BM、Sunday
    红黑树——原理
    Linux命令——监视相关
    资源管理
    排序算法——QuickSort、MergeSort、HeapSort(C++实现)
    智能指针——使用与实现
    进程间通信——实现
    构造/析构/赋值运算
    物理内存管理
  • 原文地址:https://www.cnblogs.com/llguanli/p/8793034.html
Copyright © 2020-2023  润新知