Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert string to date format in android

I'm trying to convert string to date format.I trying lot of ways to do that.But not successful. my string is "Jan 17, 2012". I want to convert this as " 2011-10-17". Could someone please tell me the way to do this? If you have any worked through examples, that would be a real help!

like image 677
TRS Avatar asked Jan 16 '12 10:01

TRS


People also ask

How to convert string yyyy MM dd to Date in java?

String start_dt = '2011-01-01'; DateFormat formatter = new SimpleDateFormat("YYYY-MM-DD"); Date date = (Date)formatter. parse(start_dt);

How to format string Date in java?

Formatting Date to String //Instantiating the SimpleDateFormat class SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy"); Format/convert the required Date object to String using the format() method, by passing it as a parameter.


2 Answers

try {

     String strDate = "Jan 17, 2012";

     //current date format
     SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd, yyyy");

     Date objDate = dateFormat.parse(strDate);

     //Expected date format
     SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd");

     String finalDate = dateFormat2.format(objDate);

     Log.d("Date Format:", "Final Date:"+finalDate)

   } catch (Exception e) {
      e.printStackTrace();
 }
like image 199
Samir Mangroliya Avatar answered Oct 11 '22 18:10

Samir Mangroliya


   String format = "yyyy-MM-dd";
   SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.US);
   System.err.format("%30s %s\n", format, sdf.format(new Date(0)));
   sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
   System.err.format("%30s %s\n", format, sdf.format(new Date(0)));

Which produces this output when run in the PDT time zone:

                 yyyy-MM-dd 1969-12-31
                 yyyy-MM-dd 1970-01-01

For more info look at here

like image 20
user370305 Avatar answered Oct 11 '22 20:10

user370305