欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

Java获取当前时间的年月日方法,当前时间年月日,package com.

来源: javaer 分享于  点击 6887 次 点评:189

Java获取当前时间的年月日方法,当前时间年月日,package com.


package com.ob;      import java.text.ParseException;      import java.text.SimpleDateFormat;      import java.util.Calendar;      import java.util.Date;      public class DateTest {          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);          }      }

输出结果: 年:2012月:1日:13时:17分:28秒:19当前时间毫秒数:1326446899902FriJan1317:28:19CST2012FriJan1317:28:19CST2012格式化后的日期:2012-01-1317:28:19字符串转成日期:FriJan1317:26:33CST2012

相关栏目:

用户点评