Date,Calender,SimpleDateFormat总结

Date,Calender,SimpleDateFormat总结

类关系
java.lang.Object
   ↳    java.util.Date

java.lang.Object
   ↳    java.util.Calendar 一般用此代替Date

java.lang.Object
   ↳    java.text.Format
        ↳    java.text.DateFormat 抽象类
             ↳    java.text.SimpleDateFormat 格式化一般用这个,符号的意义可见此类的 api

示例:
  1.         long long1 = 1444810705806L;
  2. Date date = new Date(long1);
  3. Log.e("date.api", "-----------------Date------------------");
  4. Log.e("date.getTime()", date.getTime() + "");
  5. Log.e("date.getDate()", date.getDate() + "");
  6. Log.e("date.getDay()", date.getDay() + "");
  7. Log.e("date.getHours()", date.getHours() + "");
  8. Log.e("date.getMinutes()", date.getMinutes() + "");
  9. Log.e("date.getMonth()", date.getMonth() + "");
  10. Log.e("date.tos", date.toString());
  11. Log.e("date.toLocaleString", date.toLocaleString());
  12. Log.e("Calendar.api", "-----------------Calendar------------------");
  13. Calendar cl = Calendar.getInstance();
  14. cl.setTime(date);
  15. Log.e("cl.get(Calendar.DATE)", cl.get(Calendar.DATE) + "");
  16. Log.e("cl.get(Calendar.MONTH)", cl.get(Calendar.MONTH) + "");
  17. Log.e("DAY_OF_MONTH", cl.get(Calendar.DAY_OF_MONTH) + "");
  18. Log.e("Calendar.HOUR_OF_DAY", cl.get(Calendar.HOUR_OF_DAY) + "");
  19. Log.e("Calendar.HOUr", cl.get(Calendar.HOUR) + "");
  20. Log.e("Calendar.MINUTE", cl.get(Calendar.MINUTE) + "");
  21. Log.e("cl.get(Calendar.DATE)", cl.get(Calendar.DATE) + "");
  22. Locale locale = this.getResources().getConfiguration().locale;// 本地时区或语言等
  23. SimpleDateFormat myFmt = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒", locale);// 不谢local 默认jvm本地
  24. SimpleDateFormat myFmt0 = new SimpleDateFormat("MM月dd日 HH:mm", locale);
  25. SimpleDateFormat myFmt1 = new SimpleDateFormat("yy/MM/dd HH:mm", locale);
  26. SimpleDateFormat myFmt2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", locale);//等价于now.toLocaleString()
  27. SimpleDateFormat myFmt3 = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒 E ", locale);
  28. SimpleDateFormat myFmt4 = new SimpleDateFormat(
  29. "一年中的第 D 天 一年中第w个星期 一月中第W个星期 在一天中k时 z时区", locale);
  30. Log.e("SimpleDateFormat.api", "-----------------SimpleDateFormat - date 默认得到本地时区时间------------------");
  31. Log.e("myFmt.format(date)", myFmt.format(date));
  32. Log.e("myFmt0.format(date)", myFmt0.format(date));
  33. Log.e("myFmt1.format(date)", myFmt1.format(date));
  34. Log.e("myFmt2.format(date)", myFmt2.format(date));
  35. Log.e("myFmt3.format(date)", myFmt3.format(date));
  36. Log.e("myFmt4.format(date)", myFmt4.format(date));
  37. Log.e("SimpleDateFormat.api", "-----------------SimpleDateFormat - now 默认得到本地时区时间------------------");
  38. Date now = new Date();
  39. Log.e("myFmt.format(now)", myFmt.format(now));
  40. Log.e("myFmt0.format(now)", myFmt0.format(now));
  41. Log.e("myFmt1.format(now)", myFmt1.format(now));
  42. Log.e("myFmt2.format(now)", myFmt2.format(now));
  43. Log.e("myFmt3.format(now)", myFmt3.format(now));
  44. Log.e("myFmt4.format(now)", myFmt4.format(now));


结果
  1. E/date.api: -----------------Date------------------
  2. E/date.getTime(): 1444810705806
  3. E/date.getDate(): 14
  4. E/date.getDay(): 3
  5. E/date.getHours(): 16
  6. E/date.getMinutes(): 18
  7. E/date.getMonth(): 9
  8. E/date.tos: Wed Oct 14 16:18:25 GMT+08:00 2015
  9. E/date.toLocaleString: 20151014 下午4:18:25
  10. E/Calendar.api: -----------------Calendar------------------
  11. E/cl.get(Calendar.DATE): 14
  12. E/cl.get(Calendar.MONTH): 9
  13. E/DAY_OF_MONTH: 14
  14. E/Calendar.HOUR_OF_DAY: 16
  15. E/Calendar.HOUr: 4
  16. E/Calendar.MINUTE: 18
  17. E/cl.get(Calendar.DATE): 14
  18. E/SimpleDateFormat.api: -----------------SimpleDateFormat - date 默认得到本地时区时间------------------
  19. E/myFmt.format(date): 20151014 161825
  20. E/myFmt0.format(date): 1014 16:18
  21. E/myFmt1.format(date): 15/10/14 16:18
  22. E/myFmt2.format(date): 2015-10-14 16:18:25
  23. E/myFmt3.format(date): 20151014 161825 周三
  24. E/myFmt4.format(date): 一年中的第 287 一年中第42个星期 一月中第3个星期 在一天中16 GMT+08:00时区
  25. E/SimpleDateFormat.api: -----------------SimpleDateFormat - now 默认得到本地时区时间------------------
  26. E/myFmt.format(now): 20151216 105419
  27. E/myFmt0.format(now): 1216 10:54
  28. E/myFmt1.format(now): 15/12/16 10:54
  29. E/myFmt2.format(now): 2015-12-16 10:54:19
  30. E/myFmt3.format(now): 20151216 105419 周三
  31. E/myFmt4.format(now): 一年中的第 350 一年中第51个星期 一月中第3个星期 在一天中10 GMT+08:00时区





posted on 2015-12-18 17:48  陈诚之  阅读(354)  评论(0编辑  收藏  举报

导航