码迷,mamicode.com
首页 > 其他好文 > 详细

获取当前年月日时分秒方法

时间:2019-12-11 18:57:32      阅读:83      评论:0      收藏:0      [点我收藏+]

标签:time()   时分秒   字符串   calendar   time   end   mon   led   instance   

Java

 public static void main(String[] args) throws ParseException {  
        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());  
  
        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);  
          
        String str = "2012-1-13 17:26:33";  //要跟上面sdf定义的格式一样  
        Date today = sdf.parse(str);  
        System.out.println("字符串转成日期:" + today);  
    }  

JS

// y 年份  m 月份  d  日期
var date=new Date;
var y = date.getFullYear()
var m = date.getMonth() + 1
m = m < 10 ? '0' + m : m
var d = time.getDate()
d = d < 10 ? ('0' + d) : d

获取当前年月日时分秒方法

标签:time()   时分秒   字符串   calendar   time   end   mon   led   instance   

原文地址:https://www.cnblogs.com/xiaoyinger/p/12024240.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!