日期:2014-05-20 浏览次数:20768 次
import java.util.TimeZone; import java.util.Date; import java.util.Locale; import java.text.SimpleDateFormat; import java.text.ParseException; /** * Created by IntelliJ IDEA. * User: Administrator * Date: 2011-9-29 * Time: 21:12:06 * To change this template use File | Settings | File Templates. */ public class CalendarTest1 { public static void main(String[] args){ SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss", Locale.CHINA); String dateString = "18:50:36"; Date timeDate = null; dateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai")); dateFormat.setLenient(true); try{ timeDate = dateFormat.parse(dateString); System.out.println(dateFormat.format(timeDate));//输出24小时制的时间 dateFormat.applyPattern("aa hh:mm:ss"); //输出12小时制 的时间 System.out.println(dateFormat.format(timeDate)); } catch (ParseException e) { timeDate = new Date();/* set to current time if anything is wrong */ // Log.e(Log_tag, "The date format string is failed to parse! time_ = " + time_); } } }
------解决方案--------------------
int hour = cal.get(Calendar.HOUR); //这个是12小时制
改成
int horu = cal.get(Calendar.HOUR_OF_DAY); //这个是24小时制