Java 解决解析日期异常

Java 解决解析日期异常,java,android,Java,Android,我有一个字符串“12/3/2014 12:00:00 AM”,我想将这个字符串转换为日期,格式如下“yyy-MM-dd HH:MM:ss” 上面是从日期字符串获取值的对象 SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); outlet.setOut_date(formater.parse(json.getString("outletDate"))); 在我使用like后,出现错误java.text.

我有一个字符串“12/3/2014 12:00:00 AM”,我想将这个字符串转换为日期,格式如下“yyy-MM-dd HH:MM:ss”

上面是从日期字符串获取值的对象

SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
outlet.setOut_date(formater.parse(json.getString("outletDate")));
在我使用like后,出现错误java.text.ParseException:不可解析日期:。。。(在偏移量2处)如何解决该问题提前感谢转换步骤:

  • 从日期格式创建
    date
    对象
  • 目标日期格式提供给类构造函数
  • 使用
    SimpleDateFormat
    解析
    date
  • 提供的代码将
    yyyy-MM-dd-HH:MM:ss
    转换为
    dd/MM/yyyy-HH:MM:ss
    格式

    try {
    
        SimpleDateFormat sdf1 = new SimpleDateFormat("dd/mm/yyyy HH:MM:SS a");
        Date date = sdf1.parse("12/3/2014 12:00:00 AM");
        String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
        System.out.println(format);
    
    }catch(Exception e){
        e.printStackTrace();
    }
    
    错误:
    java.text.ParseException:不可解析的日期:。。。(在偏移量2处)
    表示您使用了错误的日期格式来解析您的sting

    基于此,您需要这样的东西。首先将当前格式的字符串转换为日期对象,然后重新格式化日期对象

    String date = "12/3/2014 12:00:00 AM";
    SimpleDateFormat sdf = new SimpleDateFormat("mm/dd/yyyy HH:MM:SS a");
    Date testDate = null;
    try {
        testDate = sdf.parse(date);
    }catch(Exception ex){
        ex.printStackTrace();
    }
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String newFormat = formatter.format(testDate);
    System.out.println(".....Date..."+newFormat);
    
    String date = "12/3/2014 12:00:00 AM";
    SimpleDateFormat sdf = new SimpleDateFormat("mm/dd/yyyy HH:MM:SS a");
    Date testDate = null;
    try {
        testDate = sdf.parse(date);
    }catch(Exception ex){
        ex.printStackTrace();
    }
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String newFormat = formatter.format(testDate);
    System.out.println(".....Date..."+newFormat);