zoukankan      html  css  js  c++  java
  • Java获取当前时间的年月日方法

    Java 获取当前时间的年、月、日、小时、分钟、秒数。

     1     public static void getDateTime() throws ParseException{
     2             Calendar now = Calendar.getInstance();
     3             System.out.println("年: " + now.get(Calendar.YEAR));
     4             System.out.println("月: " + (now.get(Calendar.MONTH) + 1) + "");
     5             System.out.println("日: " + now.get(Calendar.DAY_OF_MONTH));
     6             System.out.println("时: " + now.get(Calendar.HOUR_OF_DAY));
     7             System.out.println("分: " + now.get(Calendar.MINUTE));
     8             System.out.println("秒: " + now.get(Calendar.SECOND));
     9             System.out.println("当前时间毫秒数:" + now.getTimeInMillis());
    10             System.out.println(now.getTime());
    11 
    12             Date d = new Date();
    13             System.out.println(d);
    14             SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    15             String dateNowStr = sdf.format(d);
    16             System.out.println("格式化后的日期:" + dateNowStr);
    17             
    18             String str = "2012-1-13 17:26:33";    //要跟上面sdf定义的格式一样
    19             Date today = sdf.parse(str);
    20             System.out.println("字符串转成日期:" + today);
    21    }

    输出结果:

    2014-12-26
    年: 2014
    月: 12
    日: 26
    时: 15
    分: 6
    秒: 35
    当前时间毫秒数:1419577595014
    Fri Dec 26 15:06:35 CST 2014
    Fri Dec 26 15:06:35 CST 2014
    格式化后的日期:2014-12-26 15:06:35
    字符串转成日期:Fri Jan 13 17:26:33 CST 2012

  • 相关阅读:
    Linux性能及调优指南(翻译)之Linux内存架构
    dtrace4linux
    perlchina2016 大会
    GO 语言圣经 -在线阅读
    dtrace4linux_Example
    hellogcc -100GDB技巧
    ITGEGE在线教育
    编译系统透视:图解编译原理
    C 高级编程5 IO与文件权限
    C 高级编程4 makefile 与 IO
  • 原文地址:https://www.cnblogs.com/kingxiaozi/p/4186824.html
Copyright © 2011-2022 走看看