Question

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;

}
Était-ce utile?

La solution

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; 
  }

Autres conseils

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();

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
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top