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

    Java 获取当前时间的年月日方法

    作者:kookob

    出处:http://blog.csdn.net/kookob/article/details/6885383


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

    输出结果:

    年: 2012
    月: 1
    日: 13
    时: 17
    分: 28
    秒: 19
    当前时间毫秒数:1326446899902
    Fri Jan 13 17:28:19 CST 2012
    Fri Jan 13 17:28:19 CST 2012
    格式化后的日期:2012-01-13 17:28:19
    字符串转成日期:Fri Jan 13 17:26:33 CST 2012


  • 相关阅读:
    mapr
    短信 流控规则
    js modify local file
    An O(ND) Difference Algorithm and Its Variations (1986)
    美团金融扫码付静态资源加载优化实践
    前端遇上Go: 静态资源增量更新的新实践
    小程序短信验证码登录的实现与优化
    A Practical Introduction to Blockchain with Python
    numpy计算
    小程序登录方式切换 不做url跳转
  • 原文地址:https://www.cnblogs.com/leonxyzh/p/7289175.html
Copyright © 2011-2022 走看看