将字符串转换为Java中的日期转换为、字符串、日期、Java

由网友(久伴不如酒伴)分享简介:我试图解析字符串到日期字段在一个Android应用程序,但我似乎无法得到它正确。这是我想要转换为日期2012年3月26日上午11点49分○​​○秒的字符串。我使用的功能是:I'm trying to parse a string to a date field in an android application bu...

我试图解析字符串到日期字段在一个Android应用程序,但我似乎无法得到它正确。这是我想要转换为日期2012年3月26日上午11点49分○​​○秒的字符串。我使用的功能是:

I'm trying to parse a string to a date field in an android application but I can't seem to get it correct. Here is the string I'm trying to convert to a date "03/26/2012 11:49:00 AM". The function I'm using is:

private Date ConvertToDate(String dateString){
    SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss aa");
    Date convertedDate = new Date();
    try {
        convertedDate = dateFormat.parse(dateString);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return convertedDate;
}

不过,我不断收到3/1/112上午11点49分的结果..任何帮助,我真的AP preciate。谢谢

But I keep getting 3/1/112 11:49AM as the result.. Any help I would really appreciate. thanks

推荐答案

您是错在你显示我猜的数据的方式,因为对我来说:

You are wrong in the way you display the data I guess, because for me:

    String dateString = "03/26/2012 11:49:00 AM";
    SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss aa");
    Date convertedDate = new Date();
    try {
        convertedDate = dateFormat.parse(dateString);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println(convertedDate);

打印:

Mon Mar 26 11:49:00 EEST 2012
阅读全文

相关推荐

最新文章