issue in display Date in different format with one string value android

Hello friends i have one string value for date like "2015-02-04" and below is my code

Date todaysDate = new java.util.Date("2015-02-07");
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
SimpleDateFormat df2 = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
SimpleDateFormat df3 = new SimpleDateFormat("dd-MMM-yyyy");
SimpleDateFormat df4 = new SimpleDateFormat("MM dd, yyyy");
SimpleDateFormat df5 = new SimpleDateFormat("E, MMM dd yyyy");
SimpleDateFormat df6 = new SimpleDateFormat("E, MMM dd yyyy HH:mm:ss");


try
{
    //format() method Formats a Date into a date/time string. 
    String testDateString = df.format(todaysDate);
    System.out.println("String in dd/MM/yyyy format is: " + testDateString);
    String str2 = df2.format(todaysDate);
    System.out.println("String in dd-MM-yyyy HH:mm:ss format is: " + str2);
    String str3 = df3.format(todaysDate);
    System.out.println("String in dd-MMM-yyyy format is: " + str3);
    String str4 = df4.format(todaysDate);
    System.out.println("String in MM dd, yyyy format is: " + str4);
    String str5 = df5.format(todaysDate);
    System.out.println("String in E, MMM dd yyyy format is: " + str5);
    String str6 = df6.format(todaysDate);
    System.out.println("String in E, E, MMM dd yyyy HH:mm:ss format is: " + str6);

}
catch (Exception ex ){
    System.out.println(ex);
}

my when i run above code i gives me error like

02-05 16:34:28.288: E/AndroidRuntime(27931): Caused by: java.lang.IllegalArgumentException
02-05 16:34:28.288: E/AndroidRuntime(27931):    at java.util.Date.parse(Date.java:437)
02-05 16:34:28.288: E/AndroidRuntime(27931):    at java.util.Date.<init>(Date.java:149)
02-05 16:34:28.288: E/AndroidRuntime(27931):    at com.example.paginationdemo.Demp.onCreate(Demp.java:33)

at line Date todaysDate = new java.util.Date("2015-02-07");

any idea how can i solve it?

Jon Skeet
people
quotationmark

Simple: don't use the deprecated new Date(String) constructor. Instead, create a SimpleDateFormat with the right format, and use the parse method.

DateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
format.setTimeZone(TimeZone.getTimeZone("etc/UTC"));
Date date = format.parse("2015-02-07");

Always look at compiler warnings such as when you're using deprecated members. They're usually deprecated for a reason!

people

See more on this question at Stackoverflow