Date format conversion Android

I have string form of date:

2011-03-27T09:39:01.607

and I want to format it to March 27, 2011

  • Set Android SDK behind server proxy?
  • the method getApplicationContext() is undefined
  • Open Android app from URL using intent-filter not working
  • Fragment saveInstanceState is coming as null after orientation change
  • Android contact photo: How to get back (Google) contact picture from the web (and so, 'cancel' a former local picture change)
  • How can I focus on a collapsible action view EditText item in the action bar (when it is expanded) and force the soft keyboard to open?
  • I am using

    DateFormat[] formats = new DateFormat[] {
    DateFormat.getDateInstance(), DateFormat.getDateTimeInstance(),
             DateFormat.getTimeInstance(), };
    String actDate= formats[0].format(uploadeddate.substring(0,9));
    

    but its not working.

    How do I convert to March 27, 2011?

  • How can I make the Android emulator show the soft keyboard?
  • How to set ProgressBar at the center screen in frame layout?
  • Whats the difference between Thread.setPriority() and android.os.Process.setThreadPriority()
  • ACTION_SENDTO for sending an email
  • Could not open Selected VM debug port (8700)
  • Android EditText Transparent Background
  • 8 Solutions collect form web for “Date format conversion Android”

    try this

    SimpleDateFormat form = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
    java.util.Date date = null;
    try 
    {
        date = form.parse("2011-03-27T09:39:01.607");
    }
    catch (ParseException e) 
    {
    
        e.printStackTrace();
    }
    SimpleDateFormat postFormater = new SimpleDateFormat("MMMMM dd, yyyy");
    String newDateStr = postFormater.format(date);
    

    now newDateStr = March 27, 2011;

    May be this is of any help;

    String convertDate(String inputDate) {
    
        DateFormat theDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
    
        try {
            date = theDateFormat.parse(inputDate);
        } catch (ParseException parseException) {
            // Date is invalid. Do what you want.
        } catch(Exception exception) {
            // Generic catch. Do what you want.
        }
    
        theDateFormat = new SimpleDateFormat("MMM dd, yyyy");
    
        return theDateFormat.format(date);
    }
    

    You can use android.text.format.DateFormat in this way:

    DateFormat.getMediumDateFormat(context).format(date);
    
    String dateimput = "2011-03-27T09:39:01.607"
    
    SimpleDateFormat formatrecived = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");    
    SimpleDateFormat formarwanted = new SimpleDateFormat("MMMM dd, yyyy");
    
    Date recived = formatrecived.parse(dateimput);
    
    Date output = formatwanted.format(recived);
    

    Note: dateimput you need to format to yyyy-MM-dd hh:mm:ss using replace and substring

    To get AM or PM and 12 hour date format use hh:mm:ss a as string formatter where hh is for 12 hour format and a is for AM PM format.

    note ## HH is for 24 hour and hh is for 12 hour date format.

    SimpleDateFormat formatter = new SimpleDateFormat("mm/dd/yyyy hh:mm:ss a");
            String newFormat = formatter.format(testDate);
    

    example

    String date = "2011/11/12 16:05:06";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/mm/dd HH:MM:SS");
        Date testDate = null;
        try {
            testDate = sdf.parse(date);
        }catch(Exception ex){
            ex.printStackTrace();
        }
        SimpleDateFormat formatter = new SimpleDateFormat("mm/dd/yyyy hh:mm:ss a");
        String newFormat = formatter.format(testDate);
        System.out.println(".....Date..."+newFormat);
    
    public static String getFormatedDate(String strDate, String sourceFormate,
            String destinyFormate) {
        SimpleDateFormat df;
        df = new SimpleDateFormat(sourceFormate);
        Date date = null;
        try {
            date = df.parse(strDate);
    
        } catch (ParseException e) {
            e.printStackTrace();
        }
    
        df = new SimpleDateFormat(destinyFormate);
        return df.format(date);
    
    }
    

    Call it like:

    getFormatedDate(strDate, "yyyy-MM-dd HH:mm:ss", "mm/dd/yyyy");
    

    Example code using the Joda-Time library.

    DateTime dateTime = new DateTime( "2011-03-27T09:39:01.607", DateTimeZone.forID( "Asia/Kolkata" ) );
    DateTimeFormatter formatter = DateTimeFormat.forStyle( "M-" );
    String output = formatter.withLocale( java.util.Locale.ENGLISH ).print( dateTime );
    

    What you’ll want to do is create a format for your date to be able to parse the date out into the way you want it.

    The formatted well have to be exactly the format of your timestamp and then what I did is create another format as to what I want the date to look like and then parse the it.

    Check out the documentation for exactly what to use to format and then what to use to parse. Once I figured this out it got a lot easier.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.