How to Compare system date with mydate in android 2.1?

删除回忆录丶 提交于 2019-12-19 03:18:48

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!