0

I have an android app in which i want to convert my string into date data type. I get the string via date picker in this format ("12/30/2011"). I want to convert it into string in the same format. This is my code:

        Date dateObj = new Date();
        dateFormat = new SimpleDateFormat("MM/dd/yyyy");
        dateFormat.setLenient(false);
        dateObj = dateFormat.parse("12/30/2011");
        Log.v("",""+dateObj);

But i get value in this form:

Wed Dec 28 00:00:00 GMT+05:00 2011

Andro Selva
  • 53,910
  • 52
  • 193
  • 240
user790514
  • 137
  • 1
  • 2
  • 12

4 Answers4

3

Does it help?

SimpleDateFormat sdf = new SimpleDateFormat(format);
sdf.format(new Date())

Please have a look at this

import java.text.SimpleDateFormat;
import java.util.Date;


public class DateIssue {
    public static void main(String[] args) {
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
        System.out.println(sdf.format(new Date()));
    }
}

Output: 12/28/2011

In place of

Log.v("",""+dateObj);

use this

 Log.v("",""+sdf.format(new Date()));

You shouldn't print the original date object but the formatted one.

Sanjay Kumar
  • 1,474
  • 14
  • 22
1
Date dateObj = new Date();
dateFormat = new SimpleDateFormat("MM/dd/yyyy");
dateFormat.setLenient(false);
dateObj = dateFormat.parse("12/30/2011");
Log.v("", "" + dateFormat.format(dateObj));

Try this one. dateFormat.format() should solve your problem.

amukhachov
  • 5,822
  • 1
  • 41
  • 60
0

You are mistaking parse and format.

Here:

    Date dateObj = new Date();
    dateFormat = new SimpleDateFormat("MM/dd/yyyy");
    String dateAsString = dateFormat.format(dateObj);
    Log.v("",dateAsString);

Also, please note that concatenating an object with a String always uses the toString method of the object. In the case of a Date object, toString returns a basic default formating, not related to the SimpleDateFormat you declare above.

njzk2
  • 38,969
  • 7
  • 69
  • 107
-1
date : String date
formatpattern : String formatPatten

SimpleDateFormat formatter = new SimpleDateFormat(formatpattern);
Date date = formatter.parse(date);
Ramindu Weeraman
  • 344
  • 2
  • 10