1ï¼DateDemo1ç±»
[java] view plain copy
/*
* åå¾ç³»ç»çæ¶é´
*/
public class DateDemo1 {
public static void main(String args[]) {
System.out.println(System.currentTimeMillis());
}
è¾åºä¸ºï¼1311060393171
æ§è¡ç»æä¼æ¾ç¤ºä»1970å¹´1æ1æ¥å¼å§å°åå¾ç³»ç»æ¶é´ä¸ºæ¢æç»è¿ç毫ç§æ°ï¼ä¾å¦1115346430703è¿ä¸ªæ°åï¼ä½è¿æ ·çæ°å没æ人确åäºè§£å®çæ ä¹æ¯ä»ä¹ï¼æ¨å¯ä»¥ä½¿ç¨Dateç±»å«æ¥è®©è¿ä¸ªæ°ååçæ´ææä¹ä¸äº
2ï¼DateDemo2ç±»
[java] view plain copy
public class DateDemo2 {
@SuppressWarnings("deprecation")
public static void main(String args[]) {
Date date = new Date();
System.out.println("date.toString: " + date.toString());
System.out.println("date: " + date.getDate());
System.out.println("day: " + date.getDay());
System.out.println("hour: " + date.getHours());
System.out.println("minutes: " + date.getMinutes());
System.out.println("month: " + (date.getMonth() + 1));
System.out.println("seconds: " + date.getSeconds());
System.out.println("time: " + date.getTime());
System.out.println("timezone: " + date.getTimezoneOffset());
System.out.println("year: " + (date.getYear() + 1900));
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1;
int day = calendar.get(Calendar.DATE);
int hour = calendar.get(Calendar.HOUR);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
System.out.println("----------Calendar-------");
System.out.println("year: " + year);
System.out.println("month: " + month);
System.out.println("day: " + day);
System.out.println("hour: " + hour);
System.out.println("minute: " + minute);
System.out.println("second: " + second);
}
}
ç¨dateè·å¾æ¶é´çå¼ï¼å¦æ¶ãåãç§ãæãæ¥ãå¹´å¾æ¹æ³å·²ç»è¿æ¶ï¼éè¦æ¹ç¨Calendarçæ¹æ³æ¥è·åæ¶é´çå¼
è¾åºç»æ为ï¼
[java] view plain copy
date.toString: Tue Jul 19 15:32:42 CST 2011
date: 19
day: 2
hour: 15
minutes: 32
month: 7
seconds: 42
time: 1311060762362
timezone: -480
year: 2011
----------Calendar-------
year: 2011
month: 7
day: 19
hour: 3
minute: 32
second: 42
3ï¼DateDemo3ç±»
[java] view plain copy
public class DateDemo3 {
public static void main(String args[]) {
Date date = new Date();
/*
* DateFormatä¼ä¾çµèä¸çåºå设å®æ¾ç¤ºæ¶é´æ ¼å¼ï¼EE表示ææï¼MM表示æ份ãdd表示æ¥æï¼èyyyyæ¯è¥¿å
ï¼æ¯ä¸ªåå
ç设å®é½åæå
¶æä¹
* ä»Date-->String
*/
DateFormat dateFormat = new SimpleDateFormat("EE-MM-dd-yyyy");
System.out.println(dateFormat.format(date));
DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(format1.format(date));
DateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(format2.format(date));
/*
* ä»String-->Date
*/
String dateStr = "2011-7-19";
try {
Date dateTrans = format1.parse(dateStr);
//System.out.println(dateTrans.toString());
System.out.println(dateTrans.toLocaleString()); //转æ¢ä¸ºæ¬å°çå½¢å¼
} catch (ParseException e) {
e.printStackTrace();
}
}
}
使ç¨DateFormatæ¥æ ¼å¼åæ¥ææ°æ®ï¼ä¸é¢é¨å为ä»Date-->Stringï¼ä¸é¢çé¨å为ä»String-->Date
è¾åºç»æ为ï¼
[java] view plain copy
ææäº-07-19-2011
2011-07-19
2011-07-19 15:33:27
2011-7-19 0:00:00
4ï¼DateDemo4ç±»
[java] view plain copy
public class DateDemo4 {
public static void main(String[] args) {
Date date = new Date();
DateFormat shortFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
DateFormat mediumFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
DateFormat longFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);
DateFormat fullFormat = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL);
System.out.println(shortFormat.format(date));
System.out.println(mediumFormat.format(date));
System.out.println(longFormat.format(date));
System.out.println(fullFormat.format(date));
}
}
è¾åºç»æ为ï¼
[java] view plain copy
11-7-19 ä¸å3:33
2011-7-19 15:33:54
2011å¹´7æ19æ¥ ä¸å03æ¶33å54ç§
2011å¹´7æ19æ¥ ææäº ä¸å03æ¶33å54ç§ CST
5ï¼DateDemo5ç±»
[java] view plain copy
public class DateDemo5 {
public static void main(String args[]) {
Date date = new Date();
Locale locale = new Locale("en", "US");
DateFormat shortDateFormat = DateFormat.getDateInstance(DateFormat.SHORT, locale);
DateFormat mediumDateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
DateFormat longDateFormat = DateFormat.getDateInstance(DateFormat.LONG, locale);
DateFormat fullDateFormat = DateFormat.getDateInstance(DateFormat.FULL, locale);
System.out.println(shortDateFormat.format(date));
System.out.println(mediumDateFormat.format(date));
System.out.println(longDateFormat.format(date));
System.out.println(fullDateFormat.format(date));
}
}
è¾åºç»æ为ï¼
[java] view plain copy
7/19/11
Jul 19, 2011
July 19, 2011
Tuesday, July 19, 2011
温馨提示:答案为网友推荐,仅供参考