日志文章

2019-12-18 aflfte2011

时间类的使用方法

package com.aflfte.test;

import java.util.Date;

/**
 *时间类的使用
 * @author root
 *
 */
public class TestDate {
    public static void main(String[] args) {
        Date d=new Date();//获得当前时间
        System.out.println(d);
        System.out.println(d.getTime());
        Date d2=new Date(2000);
        System.out.println(d.after(d2));//判断时间d是不是在d2之后
        //在使用日期处理时使用Canlendar日期类来处理
    }
}

////////时间类的格式化方法
package com.aflfte.test;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 测试时间对象和字符串之间的互相转换
 * DateFormat抽象类和SimpleDateFormat实现类的使用
 * @author root
 *
 */
public class TestDateFormat {
    public static void main(String[] args) throws ParseException {
        //把时间对象按照格式字符串指定的格式转成字符串
        DateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String str=df.format(new Date());
        System.out.println(str);
        //把字符串按照格式字符串指定的格式转化为相应的时间对象
        DateFormat df2=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date date=df.parse("1983-5-10 10:45:59");
        System.out.println(date);
        //测试其它的格式字符
        DateFormat df3=new SimpleDateFormat("D");
        String str1=df3.format(new Date());
        System.out.println(str1);
    }
}

« 日历类的使用方式 | String与StringBuilder的使用»