javaandroiddatetimedatetime-format

Convert string to date format in android


I'm trying to convert string to date format.I trying lot of ways to do that.But not successful. my string is "Jan 17, 2012". I want to convert this as " 2011-10-17". Could someone please tell me the way to do this? If you have any worked through examples, that would be a real help!


Solution

  • try {
    
         String strDate = "Jan 17, 2012";
    
         //current date format
         SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd, yyyy");
    
         Date objDate = dateFormat.parse(strDate);
    
         //Expected date format
         SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd");
    
         String finalDate = dateFormat2.format(objDate);
    
         Log.d("Date Format:", "Final Date:"+finalDate)
    
       } catch (Exception e) {
          e.printStackTrace();
     }