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
- 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
Date类,已经很少用了。更多使用的是Calendar
Calendar date = Calendar.getInstance();
date.get(Calendar.HOUR_OF_DAY );//得到24小时机制的
date.get(Calendar.HOUR);// 得到12小时机制的
如下是应用calendar的时间转换类
- <span style="color:#000000; font-weight:bold">package</span> <span style="color:#06699">test</span><span style="color:#339933">;</span>
- <span style="color:#000000; font-weight:bold">import</span> <span style="color:#06699">java.io.IOException</span><span style="color:#339933">;</span>
- <span style="color:#000000; font-weight:bold">import</span> <span style="color:#06699">java.text.SimpleDateFormat</span><span style="color:#339933">;</span>
- <span style="color:#000000; font-weight:bold">import</span> <span style="color:#06699">java.util.Calendar</span><span style="color:#339933">;</span>
- <span style="color:#000000; font-weight:bold">import</span> <span style="color:#06699">java.util.Date</span><span style="color:#339933">;</span>
- <span style="color:#000000; font-weight:bold">public</span> <span style="color:#000000; font-weight:bold">class</span> Main <span style="color:#0990">{</span>
- <span style="color:#0800; font-style:italic; font-weight:bold">/**
- * @param args
- * @throws IOException
- */</span>
- <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>
- <span style="color:#666666; font-style:italic">// TODO Auto-generated method stub</span>
- <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>
- <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>
- <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>
- <span style="color:#666666; font-style:italic">//当前时间</span>
- <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>
- <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>
- <span style="color:#666666; font-style:italic">//2011-08-20 04:27:16</span>
- <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>
- <span style="color:#666666; font-style:italic">//转换成Date对象</span>
- <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>
- <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>
- <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>
- c2.<span style="color:#06633">setTime</span><span style="color:#0990">(</span>d<span style="color:#0990">)</span><span style="color:#339933">;</span>
- <span style="color:#666666; font-style:italic">//通过setTime方法转换回Calendar对象</span>
- <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>
- <span style="color:#0990">}</span>
- <span style="color:#0990">}</span>