In order to parse the String in date, first of all you need to check in which date format the String is coming.
Then we will define date pattern accordingly.
Following steps will guide you how to parse String in Date:-
Step 1
Understand the date format of String:-
Example:-
2015-11-20 -> yyyy-MM-dd
2015-11-22 12:55 -> yyyy-MM-dd HH:mm
Step 2
Create instance of SimpleDateFormat. Suppose your string is coming in "yyyy-MM-dd" then define SimpleDateFormat like this:-
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Step 3
Parse that string using parse method of SimpleDateFormat class. Put this call in try catch as it can return ParseException
try {
Date date = simpleDateFormat.parse(dateString);
} catch (ParseException e) {
e.printStackTrace();
}
0 Comment(s)