Java Calendar和Date使用

来源:互联网 发布:数据新常态 编辑:程序博客网 时间:2024/05/22 04:41
import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;/** * Created by dapeng . */public class Test {    @org.junit.Test    public void test() throws Exception {        Calendar now = Calendar.getInstance();        System.out.println("年: " + now.get(Calendar.YEAR));        System.out.println("月: " + (now.get(Calendar.MONTH) + 1));        System.out.println("日: " + now.get(Calendar.DAY_OF_MONTH));        System.out.println("时: " + now.get(Calendar.HOUR_OF_DAY));        System.out.println("分: " + now.get(Calendar.MINUTE));        System.out.println("秒: " + now.get(Calendar.SECOND));        System.out.println("当前时间毫秒数:" + now.getTimeInMillis());        System.out.println(now.getTime());        System.out.println("--------------------");        Date d = new Date();        System.out.println(d);        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");        String dateNowStr = sdf.format(d);        System.out.println("格式化后的日期:" + dateNowStr);        System.out.println("--------------------");        String str = "2017-10-24 11:11:11";  //要跟上面sdf定义的格式一样        Date today = sdf.parse(str);        System.out.println("字符串转成日期:" + today);    }}

打印输出:

年: 2017
月: 5
日: 11
时: 10
分: 53
秒: 30
当前时间毫秒数:1494471210139
Thu May 11 10:53:30 CST 2017
--------------------
Thu May 11 10:53:30 CST 2017
格式化后的日期:2017-05-11 10:53:30
--------------------
字符串转成日期:Tue Oct 24 11:11:11 CST 2017

0 0
原创粉丝点击