问题
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.
回答1:
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.
回答2:
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());
来源:https://stackoverflow.com/questions/8135689/how-to-compare-system-date-with-mydate-in-android-2-1