日期:2014-05-20  浏览次数:20768 次

Java date 日期转换24小时制 失败
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);
} 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_);
}

首先这个问题我已经在google上找了很久了,还是解决不掉,所以想请教大侠们:
转换后的时间怎么会变成 6:50:36, 怎么不是24小时制的Date,

------解决方案--------------------
你的输出为6:50:36肯定你代码哪个地方的原因,单纯你给的这些代码不可能出现这种情况
给你贴一个例子
Java code

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小时制