Saturday, 3 December 2016

how to convert dateformat to yyyy-mm--dd in android

String dateTime = "Sun Dec 18 00:00:00 GMT+05:30 2016";
I am getting this date i want to convert it in to YYYY-MM-dd
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String check = dateFormat.format(dt);
System.out.println("DATE TO FROM DATEBASE   " + 
        arrayOfStringDate[d].toString());
System.out.println("CURRENT DATE   " + check);
i am trying in this way but i am unable to get value in this format please tell me where am doing wrong how to convert
-------------------------------------------------------------------------------------------------------------------------------
Answer;

Please provide output ex like "2016-DEC-13". Is this what you want?
-------------------------------------------------------------------------------------------------------------------------------
Answer;



First use an appropriate format mask to convert your string timestamp into a Date object:
String dateTime = "Sun Dec 18 00:00:00 GMT+05:30 2016";
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd hh:mm:ss Z yyyy");
Date date = sdf.parse(dateTime);
Then use a second SimpleDateFormat to render that date using the format mask you actually want (yyyy-MM-dd):
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
String output = sdf2.format(date);



No comments:

Post a Comment