Monday, 12 October 2015

Convert String to Date Format


    Here I am going to explain how to convert string to date and then formatting the date to required format.
    
                                         

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {
TextView newDate;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

newDate=(TextView)findViewById(R.id.newDate);
String myFormat= "yyyy-MM-dd";
String finalString = "";
try {
DateFormat formatter = new SimpleDateFormat("yyyy MMM dd");
Date date = (Date) formatter .parse("2015 Oct 09");
SimpleDateFormat newFormat = new SimpleDateFormat(myFormat);
finalStringnewFormat .format(date );
newDate.setText(finalString);
} catch (Exception e) {

}

}

}


Happy Coding :)