如何获得MONTHS,DAYS和周(android datepicker)

盛年不重来,一日难再晨,及时当勉励,岁月不待人。这篇文章主要讲述如何获得MONTHS,DAYS和周(android datepicker)相关的知识,希望能为你提供帮助。
UPDATE
【如何获得MONTHS,DAYS和周(android datepicker)】我正在创建一个怀孕到期日倒计时,所以我使用android.widget.DatePicker作为设置截止日期的工具。
例如:

  • 设定截止日期是Jan. 9 2015
  • 现在的日期是Nov. 9 2014
  • 所以剩下的几个月,几天和几周是2 months, 62 days and 8weeks
到目前为止,我只能显示设定的截止日期。
问题:如何在用户设置截止日期时获得确切的几个星期和几天。
更新代码
这是代码:
private TextView txtResultDueDate ; private DatePicker datePicker; private Calendar calendar; private int year; private int month; private int day; static final int DATE_DIALOG_ID = 999; @Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState); setContentView(R.layout.activity_profile); txtResultDueDate = (TextView) findViewById(R.id.txtDue); btnChangeDate = (Button)findViewById(R.id.button1); calendar = Calendar.getInstance(); year = calendar.get(Calendar.YEAR); month = calendar.get(Calendar.MONTH); day = calendar.get(Calendar.DAY_OF_MONTH); showDate(year, month+1, day); @SuppressWarnings("deprecation") public void setDate(View view) { showDialog(999); Toast.makeText(getApplicationContext(), "ca", Toast.LENGTH_SHORT) .show(); }@Override protected Dialog onCreateDialog(int id) { // TODO Auto-generated method stub if (id == 999) { return new DatePickerDialog(this, myDateListener, year, month, day); } return null; }private DatePickerDialog.OnDateSetListener myDateListener = new DatePickerDialog.OnDateSetListener() {@Override public void onDateSet(DatePicker arg0, int year, int month, int day) { Chronology chrono = GregorianChronology.getInstance(); DateTime end = new DateTime(arg0.getYear(), arg0.getMonth(), arg0.getDayOfMonth(), 0, 0, chrono); DateTime current = new DateTime(); Interval interval = new Interval(current.toInstant(), end.toInstant()); Period duePeriod = interval.toPeriod(); showDate(duePeriod.getYears(), duePeriod.getMonths(), duePeriod.getDays()); } }; private void showDate(int year, int month, int day) { txtResultDueDate.setText(new StringBuilder().append(day).append("/") .append(month).append("/").append(year)); }

这是我使用DatePicker设置截止日期时遇到的错误:
致命异议:主要
java.lang.IllegalArgumentException:结束时刻必须大于等于开头
at org.joda.time.base.Abstraction.checkInterval(AbstractInterval.java.63)
在bundle.year.bath.Baseinterval(游泳池:威士忌)
在org.joda.time.Interval。(Interval.java.122)
在com.date.androin.Profile $ 1.onDataset(Profile.java:168)
在android.app.DatePickerDialog.tryNotifyDataSet(DatePickerDialog.java.148)
在android.app.DatePickerDialog.onClick(DatePickerDialog.java.116)
在com.android.internal.app.AlertController $ ButtonHandler.handleMessage(AlertController.java:166)
在android.os.Handler.dispatchMessage(Handler.java:99)
在android.os.Looper.loop(Looper.java:137)
在android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
在java.lang.reflect.Method.invoke(Method.java:511)
在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:786)
在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStrat.main(Native Method)
答案
//somewhere in your code, init part Calendar then = setDate(9, 0, 2015); //9 january 2015 Calendar c = Calendar.getInstance(); Calendar now = setDate(c.get(Calendar.DAY_OF_MONTH), c.get(Calendar.MONTH), c.get(Calendar.YEAR)); String leftDays = getLeftDays(then, now); //your result//method setting days months years - we ignore hours and minutes private String getLeftDays(Calendar then, Calendar now) { long leftMilis = then.getTimeInMillis() - now.getTimeInMillis(); int seconds = (int) (leftMilis / 1000); Log.d(TAG, "seconds:" + seconds); int minutes = seconds / 60; Log.d(TAG, "minutes:" + minutes); int hours = minutes / 60; Log.d(TAG, "hours:" + hours); int days = hours / 24; Log.d(TAG, "days:" + days); int weeks = days / 7; Log.d(TAG, "weeks:" + weeks); //months.. another way calculating data due not equal amount of days per month Calendar temp = ((Calendar) then.clone()); temp.add(Calendar.MONTH, -now.get(Calendar.MONTH)); int months = temp.get(Calendar.MONTH); Log.d(TAG, "months:" + months); StringBuilder sb = new StringBuilder(); String format = "%d months, %d days, %d weeks"; String formatStr = String.format(format, months, days, weeks); String result = sb.append(formatStr).toString(); Log.d(TAG, sb.toString()); return result; }private Calendar setDate(int day, int month, int year) { Calendar c = Calendar.getInstance(); c.set(Calendar.DAY_OF_MONTH, day); c.set(Calendar.MONTH, month); c.set(Calendar.YEAR, year); c.set(Calendar.HOUR, 0); c.set(Calendar.MINUTE, 0); c.set(Calendar.SECOND, 0); Log.d(TAG, c.getTime().toString()); return c; }

另一答案Joda Time有一个图书馆。 Java提供的Date API更好
Joda Time有一个时间间隔的概念:
Interval interval = new Interval(oldTime, new Instant());
是的,你可以使用joda库与android qazxsw poi
DatePicker

然后使用开始和结束日期时间实例化Interval
Chronology chrono = GregorianChronology.getInstance(); // end datetime DateTime end = new DateTime(datePicker.getYear(), datePicker.getMonth(),datePicker.getDayOfMonth(), 0, 0 ,chrono); // current datetime DateTime current = new DateTime(); 然后使用Interval api获取可以提取月/天/周的差异的时间段
Interval interval = new Interval(current.toInstant(), end.toInstant());

请参阅以下Javadoc of Period以获取API Period duePeriod = interval.toPeriod(); // get difference in monthsduePeriod.getMonths(); // get difference in weeksduePeriod.getWeeks(); 的完整列表
对于Android,在您的情况下将上述代码添加到http://joda-time.sourceforge.net/apidocs/org/joda/time/Period.html onDateSet侦听器中。最后听众方法会这样,
DatePicker

另一答案日历c = calendar.getInstance();
和DatePickerDialog d
@Override public void onDateSet(DatePicker视图,int Year,int mont of Year,int day of month){
烤面包
c.get(Calendar.Year),c.get(的Calendar.MONTH),c.get(Calendar.Day_of_Month);
的.show
另一答案
@Override public void onDateSet(DatePicker arg0, int year, int month, int day) { // TODO Auto-generated method stub Chronology chrono = GregorianChronology.getInstance(); // end datetime DateTime end = new DateTime(arg0.getYear(), arg0.getMonth(), arg0.getDayOfMonth(), 0, 0, chrono); // current datetime DateTime current = new DateTime(); Interval interval = new Interval(current.toInstant(), end.toInstant()); Period duePeriod = interval.toPeriod(); showDate(duePeriod.getYears(), duePeriod.getMonths(), duePeriod.getDays()); }


    推荐阅读