0

Am getting current date using date picker. Using StringBuilder it displayed like this (10-08-2014) now I can able to get that in string using String selecteddate=datepick.getText().toString();

selecteddate=10-08-2014 but i need to store in database like this (2014-08-10). How to convert a string value(10-08-2014) inti string value(2014-08-10) ? Help me

Sri
  • 159
  • 1
  • 6
  • 14

4 Answers4

1

Use SimpleDateFormat Class

Date d = Calendar.getInstance().getTime(); // Current time
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // Set your date format
String currentData = sdf.format(d); // Get Date String according to date format
NIlesh Sharma
  • 5,445
  • 6
  • 36
  • 53
  • Hi, I get date from datepicker and set it to `textview` now i need to get that textview in string n then convert to this format(yyyy-MM-dd) – Sri Aug 28 '14 at 11:42
1
 Date cDate = new Date();
 String fDate = new SimpleDateFormat("yyyy-MM-dd").format(cDate);

You may use this also

and if you want to set selecteddate=10-08-2014 in yyyy-mm-dd format you can use

String s[] = selecteddate.split("-");
String newdate = s[2]+"-"+s[1]+"-"+s[0];
nawaab saab
  • 1,892
  • 2
  • 20
  • 36
1

Check this Code, It suits to your requirement.

      String date="10-08-2014";
        DateFormat df=new SimpleDateFormat("dd-MM-yyyy");
        Date d;
        try {
            d = df.parse(date);
            df=new SimpleDateFormat("yyyy-MM-dd");
            String myDate = df.format(d);
           Log.i(TAG,myDate);
        } catch (ParseException e) {}
Manikanta Ottiprolu
  • 751
  • 1
  • 7
  • 23
0

Simple date format:

    Date date = Calendar.getInstance().getTime();
    // Display a date in day, month, year format
    DateFormat formatter = new SimpleDateFormat("yyyy MM dd");
    String today = formatter.format(date);
    System.out.println("Today : " + today);

Link for the code above: Simple Date Format

Code snippet: DatePicker example find more in the link below:

    final Calendar c = Calendar.getInstance();
    year = c.get(Calendar.YEAR);
    month = c.get(Calendar.MONTH);
    day = c.get(Calendar.DAY_OF_MONTH);

    // set current date into textview
    textView.setText(new StringBuilder()
    .append(year) .append(month + 1).append("-").append(day).append("-")
    .append(" "));

Here you can find a great example: Mkyong android-date-picker-example

MSA
  • 2,502
  • 2
  • 22
  • 35