5

in my Android application, i am taking date and time from database. but i am not able to get the date in the "Date" Format from the database into my application, the date is in string format, so i am not able to compare the system date to database date. if i convert the system date into string then i am not able to update the date into the database in recurring case. i also want to update the database date if the system date and database date is matched.

how can i achieve this is android.

Thanks in advance.

  • here I answered for how to convert string Date into Date object [Conversion of String to DateTime : Android](http://stackoverflow.com/questions/7963042/conversion-of-string-to-datetime-android/7963067#7963067). – user370305 Nov 15 '11 at 11:36

2 Answers2

2

You can convert String to Date like this:

String str = "12/12/1912";
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
Date date = formatter.parse(str);

And back to String

SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
System.out.println("Date is : " + formatter.format(date));

And Date has before and after methods and can be compared to each other.

By the way there is also a library called Joda, you can also check it out.

medampudi
  • 399
  • 3
  • 15
Caner
  • 57,267
  • 35
  • 174
  • 180
1

Try this code:

Calendar c = Calendar.getInstance();
    System.out.println("Current time => " + c.getTime());
    SimpleDateFormat df = new SimpleDateFormat("dd-MMMM");
    formattedDate = df.format(c.getTime());
Hiren Patel
  • 52,124
  • 21
  • 173
  • 151
  • Better stick to Caner's answer. You're **kind of** right, but you might want to extend your answer beyond this specific non-popular format. Format of `dd-MMMM` seems weird - you ignore the *year* component of the date string and expect *month* component to be full month name? (`MMMM`) – andr Jan 10 '13 at 13:09
  • you're missing the essence - OP's trying to *compare* two dates. You're proposition doesn't cover comparison, only conversion from *Date* to *String*. – andr Jan 10 '13 at 13:16