android将字符串转换为日期异常

android将字符串转换为日期异常,android,Android,尝试将字符串转换为日期时出现异常: String da="16-Jan-2014 10:25:00"; SimpleDateFormat sdf = new SimpleDateFormat("dd-mmm-yyyy HH:mm:SS", Locale.ENGLISH); Date dd = sdf.parse(da); 请问我的代码有什么错误 java.text.ParseException: Unparseable date:"16-Jan-2014 10:25:00" (at offs

尝试将字符串转换为日期时出现异常:

String da="16-Jan-2014 10:25:00";
SimpleDateFormat sdf = new SimpleDateFormat("dd-mmm-yyyy HH:mm:SS", Locale.ENGLISH);
Date dd = sdf.parse(da); 
请问我的代码有什么错误

java.text.ParseException: Unparseable date:"16-Jan-2014 10:25:00" (at offset 3)

提前感谢…

您使用小写字母“m”已经有几个月了。它们应改为大写字母M。如下所述:

所以模式是这样的:

SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy HH:mm:SS", Locale.ENGLISH);

你已经用了几个月的小写字母“m”。它们应改为大写字母M。如下所述:

所以模式是这样的:

SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy HH:mm:SS", Locale.ENGLISH);

考虑下面的例子

String text = "2014-01-17T00:00:00.000-0500";
DateFormat sdf = new SimpleDateFormat("yyyy-MMM-dd'T'HH:mm:ss.SSSZ");

希望这有助于考虑以下示例

String text = "2014-01-17T00:00:00.000-0500";
DateFormat sdf = new SimpleDateFormat("yyyy-MMM-dd'T'HH:mm:ss.SSSZ");
希望这能有所帮助。

正如@Tim Kranen所提到的,月份格式是大写的。所以它应该是
MMM

为了理解月份格式,考虑这个短代码-< /P>
/*
  Formatting month using SimpleDateFormat
  This example shows how to format month using Java SimpleDateFormat class. Month can
  be formatted in M, MM, MMM and MMMM formats.  
*/


import java.text.SimpleDateFormat;
import java.util.Date;

public class FormattingMonth {

  public static void main(String[] args) {

    //create Date object
    Date date = new Date();

     //formatting month in M format like 1,2 etc
     String strDateFormat = "M";
     SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);

     System.out.println("Current Month in M format : " + sdf.format(date));

     //formatting Month in MM format like 01, 02 etc.
     strDateFormat = "MM";
     sdf = new SimpleDateFormat(strDateFormat);
     System.out.println("Current Month in MM format : " + sdf.format(date));

     //formatting Month in MMM format like Jan, Feb etc.
     strDateFormat = "MMM";
     sdf = new SimpleDateFormat(strDateFormat);
     System.out.println("Current Month in MMM format : " + sdf.format(date));

     //formatting Month in MMMM format like January, February etc.
     strDateFormat = "MMMM";
     sdf = new SimpleDateFormat(strDateFormat);
     System.out.println("Current Month in MMMM format : " + sdf.format(date));

  }
}

/*
Typical output would be
Current Month in M format : 2
Current Month in MM format : 02
Current Month in MMM format : Feb
Current Month in MMMM format : February
*
请用上述示例检查格式。

如@Tim Kranen所述,月份格式为大写字母。所以它应该是
MMM

为了理解月份格式,考虑这个短代码-< /P>
/*
  Formatting month using SimpleDateFormat
  This example shows how to format month using Java SimpleDateFormat class. Month can
  be formatted in M, MM, MMM and MMMM formats.  
*/


import java.text.SimpleDateFormat;
import java.util.Date;

public class FormattingMonth {

  public static void main(String[] args) {

    //create Date object
    Date date = new Date();

     //formatting month in M format like 1,2 etc
     String strDateFormat = "M";
     SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);

     System.out.println("Current Month in M format : " + sdf.format(date));

     //formatting Month in MM format like 01, 02 etc.
     strDateFormat = "MM";
     sdf = new SimpleDateFormat(strDateFormat);
     System.out.println("Current Month in MM format : " + sdf.format(date));

     //formatting Month in MMM format like Jan, Feb etc.
     strDateFormat = "MMM";
     sdf = new SimpleDateFormat(strDateFormat);
     System.out.println("Current Month in MMM format : " + sdf.format(date));

     //formatting Month in MMMM format like January, February etc.
     strDateFormat = "MMMM";
     sdf = new SimpleDateFormat(strDateFormat);
     System.out.println("Current Month in MMMM format : " + sdf.format(date));

  }
}

/*
Typical output would be
Current Month in M format : 2
Current Month in MM format : 02
Current Month in MMM format : Feb
Current Month in MMMM format : February
*

请使用上面提到的示例检查,以了解格式。

这行不通。你还需要三个M。两个MM将以这种格式解析日期:2014年1月16日。但是给定的格式有一个三个字符的月。这行不通。你还需要三个M。两个MM将以这种格式解析日期:2014年1月16日。但是给定的格式有三个字符的月份。