见代码:
/*
* @(#)TestSimpleDateFormat.java 1.0 Jun 27, 2012
*
* Copyright (c) 2010-2015. All Rights Reserved. **Technology Company LTD.
*/
package com.**;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* 添加说明
*
* @auther sunyong
* @version 1.0 Jun 27, 2012
*
*/
public class TestSimpleDateFormat {
public static void main(String[] args) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
System.out.println(sdf.format(new Date()));
System.out.println(sdf1.format(new Date()));
System.out.println(sdf2.format(new Date()));
Calendar cal = Calendar.getInstance();
System.out.println("Calendar.getInstance():" + cal.getTime());
System.out.println("秒" + cal.getTime().getTime());
System.out.println("此时此刻是多少秒:" + (new Date()).getTime());
// cal.setTimeInMillis((new Date()).getTime());
// cal.setTimeInMillis(cal.getTime().getTime());
cal.add(Calendar.MONTH, -1);
System.out.println("前一个月:" + cal.getTime());
System.out.println("前一个月的秒数:" + cal.getTime().getTime());
System.out.println("前一个月的日期:" + sdf.format(cal.getTime().getTime()));
System.out.println("前一个月的日期:" + sdf1.format(cal.getTime()));
System.out.println("前一个月的日期:" + sdf2.format(cal.getTime()));
}
}
输出结果:
20120627160108
2012-06-27
2012-06-27 04:01:08
Calendar.getInstance():Wed Jun 27 16:01:08 CST 2012
秒1340784068296
此时此刻是多少秒:1340784068296
前一个月:Sun May 27 16:01:08 CST 2012
前一个月的秒数:1338105668296
前一个月的日期:20120527160108
前一个月的日期:2012-05-27
前一个月的日期:2012-05-27 04:01:08
SimpleDateFormat和Calendar
最新推荐文章于 2019-03-30 20:39:11 发布