Android time syntax analysis with SimpleDateFormat

I have a line 11/08/2013 08:48:10

and i use SimpleDateFormat("MM/dd/yyyy HH:mm:ss")

and when im parsing it throws an exception: unparseable date

what's wrong with him?

            String result = han.ExecuteUrl("http://"+han.IP+":8015/api/Values/GetLastChange"); 
            Log.d("Dal","result date time "+result); #result is 11/08/2013 08:48:10
            SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");

            Date convertedDate = new Date();
            try
            {

                convertedDate = dateFormat.parse(result);
            }
            catch (ParseException e)
            {
                e.printStackTrace();
            }
+4
source share
2 answers

Its a working attempt to analyze your date like this.

String dtStart = "11/08/2013 08:48:10";
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
try {
    date = format.parse(dtStart);
    System.out.println("Date ->" + date);
} catch (ParseException e) {
    e.printStackTrace();
}
+22
source

The working code is here.

You can use the code below to convert from String to Date

String myStrDate = "11/08/2013 08:48:10";
    SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
    try {
        Date date = format.parse(myStrDate);
        System.out.println(date);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

To the contrary means that to convert from Date to String

SimpleDateFormat myFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
    try {
        Date date = new Date();
        String datetime = myFormat.format(date);
        System.out.println("Current Date Time in give format: " + datetime);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

Additional information on date and time formatting. Visit website

+4
source

All Articles