Convert string to date format in android

33,495

Solution 1

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();
 }

Solution 2

   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

Solution 3

I suggest using Joda Time, it's the best and simplest library for date / dateTime manipulations in Java, and it's ThreadSafe (as opposed to the default formatting classes in Java).

You use it this way:

// Define formatters:
DateTimeFormatter inputFormat = DateTimeFormat.forPattern("MMM dd, yyyy");
DateTimeFormatter outputFormat = DateTimeFormat.forPattern("yyyy-MM-dd");

// Do your conversion:
String inputDate = "Jan 17, 2012";
DateTime date = inputFormat.parseDateTime(inputDate);
String outputDate = outputFormat.print(date);
// or:
String outputDate = date.toString(outputFormat);
// or:
String outputDate = date.toString("yyyy-MM-dd");

// Result: 2012-01-17

It also provides plenty of useful methods for operations on dates (add day, time difference, etc.). And it provides interfaces to most of the classes for easy testability and dependency injection.

Solution 4

Why do you want to convert string to string try to convert current time in milisecond to formated String, this method will convert your milisconds to a data formate.

 public static String getTime(long milliseconds)
{

         return DateFormat.format("MMM dd, yyyy", milliseconds).toString();
}

you can also try DATE FORMATE class for better understanding.

Solution 5

public static Date getDateFromString(String date) {

    Date dt = null;

    if (date != null) {
        for (String sdf : supportedDateFormats) {
            try {
                dt = new Date(new SimpleDateFormat(sdf).parse(date).getTime());
                break;
            } catch (ParseException pe) {
                pe.printStackTrace();
            }
        }
    }
    return dt;
}
Share:
33,495

Related videos on Youtube

TRS
Author by

TRS

Updated on July 09, 2022

Comments

  • TRS
    TRS almost 2 years

    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!

    • Guillaume
      Guillaume over 12 years
      Not really an Android question
  • Ravindra Rathour
    Ravindra Rathour almost 8 years
    Use this method it's working fine for my, I can also get date in milliseconds also.