Java日期类

来源:互联网 发布:砸金蛋软件 编辑:程序博客网 时间:2024/05/22 10:54


Date类:

构造函数:

    Date d = new Date(); //当前时间

    Date d = new Date(long timemillis);

    Date d = new Date(10,0,1);  //(比1900多10年 即2000,0代表1月,1代表1号)

    Date d = new Date(10,0,1,9,30,15,0) //  (年,月,日,时,分,秒,毫秒)

获取时间:long dLong = d.getTime(); (设置时间 d.setTime(dLong))

比较时间

    boolean after(Date when) 

    boolean before(Date when)

    boolean after(Date when)  

    int compareTo(Date anotherDate)   //负数小于,0等于,正数大于


Calendar

构造函数:

    Calendar cal = Calendar.getInstance();

设置时间:

    cal.set(2009, 0, 31);  //年月日,简单设置方法

    cal.set(Calendar.MONTH, 3);  //月份从0开始    ,其他的类似

获取时间:

    int month = c1.get(Calendar.MONTH) + 1;

改变时间:

    cal.add(Calendar.DATE,-3);  //往前过三天

判断闰年:

    boolean leapYear = ( (GregorianCalendar)cal ).isLeapYear(2002);

时间转化:

    long t = cal.getTimeInMillis();    (cal.setTimeInMillis(t);)

    Date d = c.getTime();    

    c.setTime(Date d);


SimpleDateFormat

    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");      //日期到字符串的转换

    String today = df.format(new Date());       //日期转字符串

    Date date = df.parse("2009-06-12 02:06:37");   //字符串转日期









          





0 0
原创粉丝点击