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;

}
Hamid Shatu
  • 9,664
  • 4
  • 30
  • 41
Syed Naeem
  • 681
  • 8
  • 17

3 Answers3

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; 
  }
Hamid Shatu
  • 9,664
  • 4
  • 30
  • 41
  • 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. – Steve Atkinson Mar 02 '14 at 08:19
  • 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. – Hamid Shatu Mar 02 '14 at 08:26
  • 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 – Syed Naeem Mar 03 '14 at 20:57
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();
SHASHIDHAR MANCHUKONDA
  • 3,302
  • 2
  • 19
  • 40
  • lowercase m matches minute, not month. The OP needs uppercase M as all the other answers indicate. – Steve Atkinson Mar 02 '14 at 08:15
  • 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 – Steve Atkinson Mar 02 '14 at 08:21
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
MDMalik
  • 3,951
  • 2
  • 25
  • 39
  • Yes my string variable is the parameter which is your format, your format in `SimpleDateFormat` was not matching, hence the issue – MDMalik Mar 02 '14 at 07:02
  • 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`. – Rohit Jain Mar 02 '14 at 07:03