0

I'm trying to convert json date string into date for storing into SQLite database . After run the application is crash after some time and getting error of Caused by: java.lang.IllegalArgumentException: Parse error: at this line Date date = new Date(strActiondate); First 2 to 3 months its run but right now its getting crash.Thanks in advanced !!

Here is my date code

Log.e(" strActiondate "," ==========>>>>> "+strActiondate);
                    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a", Locale.getDefault());
                    Date date = new Date(strActiondate);
                    String newDate = dateFormat.format(date);
                    Log.e(" newDate "," = "+ newDate);

Here is my LOG cat error

Caused by: java.lang.IllegalArgumentException: Parse error:
            at java.util.Date.parseError(Date.java:364)
            at java.util.Date.parse(Date.java:560)
            at java.util.Date.<init>(Date.java:154)
4
  • What is the value of strActiondate? Commented Feb 1, 2016 at 4:26
  • What is the value for strActiondate ? Also what is the date format that you're trying to convert strActiondate to? Commented Feb 1, 2016 at 4:32
  • Sorry for late reply . 12/23/2015 1:28:14 PM this is the value of strActiondate . Commented Feb 1, 2016 at 4:34
  • 2015-12-23 1:28:14 PM this is the format I want to convert . Commented Feb 1, 2016 at 4:37

1 Answer 1

1

You can use below function to convert one date string format to another date string format.

public static String convertDateStringFormat(String dateString, String originalDateFormat, String outputDateFormat){
        String finalDate = null;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(originalDateFormat);
        try {
            Date date = simpleDateFormat.parse(dateString);
            simpleDateFormat = new SimpleDateFormat(outputDateFormat);
            finalDate = simpleDateFormat.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return finalDate;
}

Use:

String newDateString = convertDateStringFormat("12/23/2015 1:28:14 PM", "MM/dd/yyyy hh:mm:ss a", "yyyy-MM-dd hh:mm:ss a");
Sign up to request clarification or add additional context in comments.

7 Comments

I'm getting this output 0008-01-06 02:26:25 PM using your code :- String newDateString = convertDateStringFormat("12/23/2015 1:28:14 PM", "yyyy/MM/dd hh:mm:ss a", "yyyy-MM-dd hh:mm:ss a");
Thanks for your update, There was minor mistake, I have updated my answer.. please check it..
For "12/23/2015 1:28:14 PM" date String, Date format should be "MM/dd/yyyy hh:mm:ss a".
if i want only in that format 2016-01-13 , then i used to like this right = String newDateString = convertDateStringFormat("12/23/2015 1:28:14 PM", "MM/dd/yyyy hh:mm:ss a", "yyyy-MM-dd");
But this date is store in SQLite database 2015 -12-23 1:28:14 PM and again i want to convert it into 2016-01-13 , So can I use same format ?convertDateStringFormat("12/23/2015 1:28:14 PM", "MM/dd/yyyy hh:mm:ss a", "yyyy-MM-dd");
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.