gpt4 book ai didi

android - 如何从特定日期获取总月数?

转载 作者:行者123 更新时间:2023-11-29 14:30:57 25 4
gpt4 key购买 nike

我是 Android 新手。我有一个要求,我有一个字段可以输入一个人的出生日期。成功选择后,我想返回从 DOB 到当前日期的总月数。例如,如果我在 19/10/2012 输入 DOB 我想返回 36(个月)。我搜索了这个,但没有找到适合我要求的东西。这是我当前返回成功数据的代码,

private void showDate(int year, int month, int day) {

Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(0);
cal.set(year, month, day);
Date date = cal.getTime();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");

if(System.currentTimeMillis() > date.getTime()) {
edtDate.setText(sdf.format(date));
LocalDate date1 = new LocalDate(date);
LocalDate date2 = new LocalDate(new java.util.Date());
PeriodType monthDay = PeriodType.yearMonthDayTime();
Period difference = new Period(date1, date2, monthDay);
int months = difference.getMonths();
months=months + 1;
System.out.println("16102015:Nunber of Months"+months);
}else{
Toast.makeText(mActivity,getResources().getString(R.string.date_validationmsg),Toast.LENGTH_LONG).show();
}


}

最佳答案

Calendar startCalendar = new GregorianCalendar();
startCalendar.setTime(startDate);
Calendar endCalendar = new GregorianCalendar();
endCalendar.setTime(endDate);

int diffYear = endCalendar.get(Calendar.YEAR) - startCalendar.get(Calendar.YEAR);
int diffMonth = diffYear * 12 + endCalendar.get(Calendar.MONTH) - startCalendar.get(Calendar.MONTH);

关于android - 如何从特定日期获取总月数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33207081/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com