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

来自:http://blog.csdn.net/dongguang1082/article/details/4387165

在使用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 

[java] view plaincopy
 
  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.   
  5.         Date date = new Date();  
  6.         SimpleDateFormat sdformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//24小时制  
  7.         String LgTime = sdformat.format(date);  
  8.         System.out.println(LgTime);  
  9.   
  10.   
  11. 结果为  
  12. 2008-05-28 01:32:54  
  13. 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的时间转换类

[java] view plaincopy
 
  1. <span style="color:#000000; font-weight:bold">package</span> <span style="color:#06699">test</span><span style="color:#339933">;</span>  
  2.    
  3. <span style="color:#000000; font-weight:bold">import</span> <span style="color:#06699">java.io.IOException</span><span style="color:#339933">;</span>  
  4. <span style="color:#000000; font-weight:bold">import</span> <span style="color:#06699">java.text.SimpleDateFormat</span><span style="color:#339933">;</span>  
  5. <span style="color:#000000; font-weight:bold">import</span> <span style="color:#06699">java.util.Calendar</span><span style="color:#339933">;</span>  
  6. <span style="color:#000000; font-weight:bold">import</span> <span style="color:#06699">java.util.Date</span><span style="color:#339933">;</span>  
  7.    
  8.    
  9. <span style="color:#000000; font-weight:bold">public</span> <span style="color:#000000; font-weight:bold">class</span> Main <span style="color:#0990">{</span>  
  10.    
  11.     <span style="color:#0800; font-style:italic; font-weight:bold">/** 
  12.      * @param args 
  13.      * @throws IOException  
  14.      */</span>  
  15.     <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>  
  16.         <span style="color:#666666; font-style:italic">// TODO Auto-generated method stub</span>  
  17.         <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>  
  18.         <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>  
  19.         <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>  
  20.         <span style="color:#666666; font-style:italic">//当前时间</span>  
  21.         <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>   
  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">"直接格式化毫秒值输出:"</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>  
  23.         <span style="color:#666666; font-style:italic">//2011-08-20 04:27:16</span>  
  24.         <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>  
  25.         <span style="color:#666666; font-style:italic">//转换成Date对象</span>  
  26.         <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>  
  27.         <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>  
  28.         <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>  
  29.         c2.<span style="color:#06633">setTime</span><span style="color:#0990">(</span>d<span style="color:#0990">)</span><span style="color:#339933">;</span>  
  30.         <span style="color:#666666; font-style:italic">//通过setTime方法转换回Calendar对象</span>  
  31.         <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>  
  32.     <span style="color:#0990">}</span>  
  33.    
  34. <span style="color:#0990">}</span>  
posted on 2013-12-25 16:41  jixu  阅读(463)  评论(0编辑  收藏  举报