0

i am converting string in to date object in android...... that string is coming from server in form of "2014-02-22" or something like that... i want to convert it in to my date which i can use in my application.. i am using Simple Date Format ,parse method to convert.... but this statement throws parse exception... meaning its not converting my string.. which is "2014-02-22"... it should convert but its not.... so kindly help me in this..... i am getting null in response

@SuppressLint("SimpleDateFormat")
public static Date getDate(String string){
    Date date = null;
    try {
        date = new Date();
        date = new SimpleDateFormat("yyyy/MM/dd").parse(string);
    }
    catch (ParseException e) { e.printStackTrace(); }
    catch (java.text.ParseException e) { e.printStackTrace(); }
    return date;

}
4
  • Try this date = new SimpleDateFormat("yyyy-MM-dd").parse(string); Commented Mar 2, 2014 at 6:59
  • 1
    Does the format yyyy/MM/dd match 2014-02-22? Commented Mar 2, 2014 at 6:59
  • possible duplicate of how to convert java string to Date object Commented Mar 2, 2014 at 7:03
  • possible duplicate of Java string to date conversion Commented Mar 2, 2014 at 11:05

3 Answers 3

2

Try as follows...

  SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  Date date;

 @SuppressLint("SimpleDateFormat") 
 public static Date getDate(String string){

         date = new Date();

         try {
                date = format.parse(string);
        }     catch (ParseException e) {
                e.printStackTrace();
        }

       return date; 
  }
Sign up to request clarification or add additional context in comments.

3 Comments

Just to improve this, I would define format as a static final at clas level as yo only ever need to create it once, and it then could be reused in other methods.
In addition with your point...I also like to declare the 'Date' object at class level...but, for only for solution, I gave the above solution. thanks for your point.
ok its working... thanku about that... but i am having new problem now.... now i have converted date saved into database.... when next time i read from database i dont need to parse it again... right????? how can i save that String value in date as it is.....if i parse it using this method exception occurs.. however its working fine when i am saving it to database
1
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

Date testDate = null;

try {
      testDate = sdf.parse("2013-11-12");
}
catch(Exception ex) {
      ex.printStackTrace();
}

int date= testDate.getDate();
int month = testDate.getMonth();
int year = testDate.getYear();

2 Comments

lowercase m matches minute, not month. The OP needs uppercase M as all the other answers indicate.
np, we found the same bug in a logging utility someone else had written in production code last week! BTW I would also make sdf a class static final as it's reusuable
0

Just use SimpleDateFormat (click the link to see all format patterns).

String string = "2014-02-22";
Date date = new SimpleDateFormat("yyy-M-d", Locale.ENGLISH).parse(string);
System.out.println(date); // Sat Jan 02 00:00:00 BOT 2010

Here's an extract of relevance from the javadoc, listing all available format patterns:

G   Era designator       Text               AD
y   Year                 Year               1996; 96
M   Month in year        Month              July; Jul; 07
w   Week in year         Number             27
W   Week in month        Number             2
D   Day in year          Number             189
d   Day in month         Number             10
F   Day of week in month Number             2
E   Day in week          Text               Tuesday; Tue
u   Day number of week   Number             1
a   Am/pm marker         Text               PM
H   Hour in day (0-23)   Number             0
k   Hour in day (1-24)   Number             24
K   Hour in am/pm (0-11) Number             0
h   Hour in am/pm (1-12) Number             12
m   Minute in hour       Number             30
s   Second in minute     Number             55
S   Millisecond          Number             978
z   Time zone            General time zone  Pacific Standard Time; PST; GMT-08:00
Z   Time zone            RFC 822 time zone  -0800
X   Time zone            ISO 8601 time zone -08; -0800; -08:00

2 Comments

Yes my string variable is the parameter which is your format, your format in SimpleDateFormat was not matching, hence the issue
I'm not the OP. But what do you mean by - Don't do it, that's the hard way.? OP is already using SimpleDateFormat.

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.