- 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("時(shí): " + now.get(Calendar.HOUR_OF_DAY));
- System.out.println("分: " + now.get(Calendar.MINUTE));
- System.out.println("秒: " + now.get(Calendar.SECOND));
- System.out.println("當(dāng)前時(shí)間毫秒數(shù):" + 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";
- Date today = sdf.parse(str);
- System.out.println("字符串轉(zhuǎn)成日期:" + today);
- }
- }
- 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("時(shí): " + now.get(Calendar.HOUR_OF_DAY));
System.out.println("分: " + now.get(Calendar.MINUTE));
System.out.println("秒: " + now.get(Calendar.SECOND));
System.out.println("當(dāng)前時(shí)間毫秒數(shù):" + 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("字符串轉(zhuǎn)成日期:" + today);
}
}
輸出結(jié)果:
年: 2012
月: 1
日: 13
時(shí): 17
分: 28
秒: 19
當(dāng)前時(shí)間毫秒數(shù):1326446899902
Fri Jan 13 17:28:19 CST 2012
Fri Jan 13 17:28:19 CST 2012
格式化后的日期:2012-01-13 17:28:19
字符串轉(zhuǎn)成日期:Fri Jan 13 17:26:33 CST 2012