[java]代码库
第一种方法
Date date=new Date();
DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time=format.format(date);
第二种方法
public class Test
{
    public static void main(String[] args)
    {
        //利用构造函数设置格式化模板
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日");
        Date date = new Date();
        //执行格式化功能
        System.out.println(formatter.format(date));
        //设置格式化模板
        formatter.applyPattern("yyyy-MM-dd");
        System.out.println(formatter.format(date));
    }
}
Java对日期Date类进行加减运算,年份加减,月份加减
public class DateTestUtil {
    
    public static void main(String[] args) throws Exception {
        
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
        String str="20110823";
        Date dt=sdf.parse(str);
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(dt);
        rightNow.add(Calendar.YEAR,-1);//日期减1年
        rightNow.add(Calendar.MONTH,3);//日期加3个月
        rightNow.add(Calendar.DAY_OF_YEAR,10);//日期加10天
        Date dt1=rightNow.getTime();
        String reStr = sdf.format(dt1);
        System.out.println(reStr);
    }
}
中级程序员
by: 暖暖的蟹 发表于:2016-03-17 10:08:36 顶(0) | 踩(0) 回复
666
回复评论