SimpleDateFormat转换时间,12,24时间格式[转]

SimpleDateFormat转换时间,12,24时间格式

在使用SimpleDateFormat时格式化时间的 yyyy.MM.dd 为年月日而如果希望格式化时间为12小时制的,则使用hh:mm:ss 如果希望格式化时间为24小时制的,则使用HH:mm:ss

Date d = new Date();

SimpleDateFormat ss = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");//12小时制

System.out.println(ss.format(d));

Date date = new Date();

SimpleDateFormat sdformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//24小时制

String LgTime = sdformat.format(date);

System.out.println(LgTime);

结果为

2008-05-28 01:32:54

2008-05-28 13:32:54

  1. Date d = new Date();
  2. SimpleDateFormat ss = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");//12小时制
  3. System.out.println(ss.format(d));
  4. Date date = new Date();
  5. SimpleDateFormat sdformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//24小时制
  6. String LgTime = sdformat.format(date);
  7. System.out.println(LgTime);
  8. 结果为
  9. 2008-05-28 01:32:54
  10. 2008-05-28 13:32:54

Date类,已经很少用了。更多使用的是Calendar   
  Calendar
    date    =    Calendar.getInstance();   
  date.get(Calendar.HOUR_OF_DAY    );//得到24小时机制的   
  date.get(Calendar.HOUR);//    得到12小时机制的

如下是应用calendar的时间转换类

  1. <span style="color:#000000; font-weight:bold">package</span> <span style="color:#06699">test</span><span style="color:#339933">;</span>
  2. <span style="color:#000000; font-weight:bold">import</span> <span style="color:#06699">java.io.IOException</span><span style="color:#339933">;</span>
  3. <span style="color:#000000; font-weight:bold">import</span> <span style="color:#06699">java.text.SimpleDateFormat</span><span style="color:#339933">;</span>
  4. <span style="color:#000000; font-weight:bold">import</span> <span style="color:#06699">java.util.Calendar</span><span style="color:#339933">;</span>
  5. <span style="color:#000000; font-weight:bold">import</span> <span style="color:#06699">java.util.Date</span><span style="color:#339933">;</span>
  6. <span style="color:#000000; font-weight:bold">public</span> <span style="color:#000000; font-weight:bold">class</span> Main <span style="color:#0990">{</span>
  7. <span style="color:#0800; font-style:italic; font-weight:bold">/**
  8. * @param args
  9. * @throws IOException
  10. */</span>
  11. <span style="color:#000000; font-weight:bold">public</span> <span style="color:#000000; font-weight:bold">static</span> <span style="color:#0066; font-weight:bold">void</span> main<span style="color:#0990">(</span><span style="color:#03399">String</span><span style="color:#0990">[</span><span style="color:#0990">]</span> args<span style="color:#0990">)</span> <span style="color:#000000; font-weight:bold">throws</span> <span style="color:#03399">IOException</span> <span style="color:#0990">{</span>
  12. <span style="color:#666666; font-style:italic">// TODO Auto-generated method stub</span>
  13. <span style="color:#03399">Calendar</span> c<span style="color:#339933">=</span><span style="color:#03399">Calendar</span>.<span style="color:#06633">getInstance</span><span style="color:#0990">(</span><span style="color:#0990">)</span><span style="color:#339933">;</span>
  14. <span style="color:#0066; font-weight:bold">long</span> milliseconds<span style="color:#339933">=</span>c.<span style="color:#06633">getTimeInMillis</span><span style="color:#0990">(</span><span style="color:#0990">)</span><span style="color:#339933">;</span>
  15. <span style="color:#03399">System</span>.<span style="color:#06633">out</span>.<span style="color:#06633">println</span><span style="color:#0990">(</span><span style="color:#00ff">"当前时间毫秒值:"</span><span style="color:#339933">+</span>milliseconds<span style="color:#0990">)</span><span style="color:#339933">;</span>
  16. <span style="color:#666666; font-style:italic">//当前时间</span>
  17. <span style="color:#03399">SimpleDateFormat</span> sdf<span style="color:#339933">=</span><span style="color:#000000; font-weight:bold">new</span> <span style="color:#03399">SimpleDateFormat</span><span style="color:#0990">(</span><span style="color:#00ff">"yyyy-MM-dd hh:mm:ss"</span><span style="color:#0990">)</span><span style="color:#339933">;</span>
  18. <span style="color:#03399">System</span>.<span style="color:#06633">out</span>.<span style="color:#06633">println</span><span style="color:#0990">(</span><span style="color:#00ff">"直接格式化毫秒值输出:"</span><span style="color:#339933">+</span>sdf.<span style="color:#06633">format</span><span style="color:#0990">(</span>milliseconds<span style="color:#0990">)</span><span style="color:#0990">)</span><span style="color:#339933">;</span>
  19. <span style="color:#666666; font-style:italic">//2011-08-20 04:27:16</span>
  20. <span style="color:#03399">Date</span> d<span style="color:#339933">=</span><span style="color:#000000; font-weight:bold">new</span> <span style="color:#03399">Date</span><span style="color:#0990">(</span>milliseconds<span style="color:#0990">)</span><span style="color:#339933">;</span>
  21. <span style="color:#666666; font-style:italic">//转换成Date对象</span>
  22. <span style="color:#03399">System</span>.<span style="color:#06633">out</span>.<span style="color:#06633">println</span><span style="color:#0990">(</span><span style="color:#00ff">"Date对象输出时间:"</span><span style="color:#339933">+</span>sdf.<span style="color:#06633">format</span><span style="color:#0990">(</span>d<span style="color:#0990">)</span><span style="color:#0990">)</span><span style="color:#339933">;</span>
  23. <span style="color:#03399">Calendar</span> c2<span style="color:#339933">=</span><span style="color:#03399">Calendar</span>.<span style="color:#06633">getInstance</span><span style="color:#0990">(</span><span style="color:#0990">)</span><span style="color:#339933">;</span>
  24. <span style="color:#03399">System</span>.<span style="color:#06633">out</span>.<span style="color:#06633">println</span><span style="color:#0990">(</span><span style="color:#00ff">"Calendar设置前毫秒值:"</span><span style="color:#339933">+</span>c2.<span style="color:#06633">getTimeInMillis</span><span style="color:#0990">(</span><span style="color:#0990">)</span><span style="color:#0990">)</span><span style="color:#339933">;</span>
  25. c2.<span style="color:#06633">setTime</span><span style="color:#0990">(</span>d<span style="color:#0990">)</span><span style="color:#339933">;</span>
  26. <span style="color:#666666; font-style:italic">//通过setTime方法转换回Calendar对象</span>
  27. <span style="color:#03399">System</span>.<span style="color:#06633">out</span>.<span style="color:#06633">println</span><span style="color:#0990">(</span><span style="color:#00ff">"Calendar设置后毫秒值:"</span><span style="color:#339933">+</span>c2.<span style="color:#06633">getTimeInMillis</span><span style="color:#0990">(</span><span style="color:#0990">)</span><span style="color:#0990">)</span><span style="color:#339933">;</span>
  28. <span style="color:#0990">}</span>
  29. <span style="color:#0990">}</span>
上一篇:VSCode Snippet 小试牛刀


下一篇:synchronized简介