Android:日期(年,月,日)

And*_*per 6 android calendar date gettime

我想把日期作为一年,一个月,一天没有时间或分钟或其他任何事情,我不想让一年,一个月和一天由它自己.因为作为一个完整的日期,我需要它与另一个日期进行比较

比如今天28.11.2012,比较它,11.12.2011 好像今天减去11.12.2011超过280天,我想执行一些代码

Bha*_*iya 14

你可以使用SimpleDateFormat.

获取当前日期的基础知识

DateFormat df = new SimpleDateFormat("MMM d, yyyy");
String now = df.format(new Date());
Run Code Online (Sandbox Code Playgroud)

要么

DateFormat df = new SimpleDateFormat("MM/dd/yy");
String now = df.format(new Date());
Run Code Online (Sandbox Code Playgroud)

编辑: 首先,你在String Formate中有约会.你必须转换成约会Formate.尝试下面的代码来做到这一点.你已经为String strThatDaystrTodaDay应用了相同的内容,你将获得两个Calender Object.

String strThatDay = "2012/11/27";
  SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
  Date d = null;
  try {
   d = formatter.parse(strThatDay);//catch exception
  } catch (ParseException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } 

  Calendar thatDay = Calendar.getInstance();
  thatDay.setTime(d);
Run Code Online (Sandbox Code Playgroud)

之后尝试下面的代码从两个日期获取日期:

long diff = today.getTimeInMillis() - thatDay.getTimeInMillis(); //result in millis

long days = diff / (24 * 60 * 60 * 1000);
Run Code Online (Sandbox Code Playgroud)

试试看.希望它会对你有所帮助.