我有一个解决方案,可以修改我在网上找到的一些示例,以获取距生日为止的剩余天数,但我的 daysRemaining()
方法中存在一个仅满足一个条件的错误/问题。
Activity .java
private Profiles.AgeCalculation age = null; //this is in the class, and not onCreate
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_transition_edit);
//Age Calculation
age = new Profiles.AgeCalculation();
age.getCurrentDate();
}
//includes for age calculation
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_START_DIALOG_ID:
return new DatePickerDialog(this,
mDateSetListener,
startYear, startMonth, startDay);
}
return null;
}
private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) {
startYear=selectedYear;
startMonth=selectedMonth;
startDay=selectedDay;
System.out.println("Birth Year: " + startYear);
System.out.println("Birth Month: " + startMonth);
System.out.println("Birth Day: " + startDay);
age.setDateOfBirth(startYear, startMonth, startDay);
//birthDate.setText("Date of Birth(DD/MM/YY): "+selectedDay+":"+(startMonth+1)+":"+startYear);
//calculateAge();
//age.daysRemaining();
calculateYear();
}
};
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.age_button:
showDialog(DATE_START_DIALOG_ID);
break;
default:
break;
}
}
private void calculateYear()
{
age.calcualteYear();
age.calcualteMonth();
age.calcualteDay();
age.daysRemaining();
//Toast.makeText(getBaseContext(), "click the resulted button"+age.getResult() , Toast.LENGTH_SHORT).show();
textViewDisplayAge.setText(age.getYearsOld() + " Years Old");
}
年龄计算.java
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
public class AgeCalculation {
private int startYear; //birth year
private int startMonth; //birth month
private int startDay; //birth day
private int endYear;
private int endMonth;
private int endDay;
private int resultYear;
private int checkFor16;
private int resultMonth;
private int resultDay;
private Calendar start;
private Calendar end;
public String getCurrentDate() //gets current date of mobile device using Calendar
{
end=Calendar.getInstance();
endYear=end.get(Calendar.YEAR);
endMonth=end.get(Calendar.MONTH);
endMonth++;
endDay=end.get(Calendar.DAY_OF_MONTH);
System.out.println("Current Year: " + endYear);
System.out.println("Current Month: " + endMonth);
System.out.println("Current Day: " + endDay);
return endDay+":"+endMonth+":"+endYear;
}
public void setDateOfBirth(int sYear, int sMonth, int sDay)
{
startYear=sYear;
startMonth=sMonth;
startMonth++;
startDay=sDay;
}
public void calcualteYear()
{
resultYear=endYear-startYear;
checkFor16 = endYear-startYear;
//In this year the user will be this age
System.out.println("caculateYear: " + resultYear);
System.out.println("checkFor 16 years old: " + checkFor16);
}
public void calcualteMonth()
{
if(endMonth>=startMonth)
{
resultMonth= endMonth-startMonth;
}
else
{
resultMonth=endMonth-startMonth;
resultMonth=12+resultMonth;
resultYear--;
}
System.out.println("calculateMonth: " + resultMonth);
}
public void calcualteDay()
{
if(endDay>=startDay)
{
resultDay= endDay-startDay;
}
else
{
resultDay=endDay-startDay;
resultDay=30+resultDay;
if(resultMonth==0)
{
resultMonth=11;
resultYear--;
}
else
{
resultMonth--;
}
}
System.out.println("calculateDay: " + resultDay);
}
public String getResult()
{
return resultDay+":"+resultMonth+":"+resultYear;
}
public int getYearsOld(){
return resultYear; //returns the age of the user
}
public void daysRemaining(){
final String strBDay = String.valueOf(startDay) + "/" + String.valueOf(startMonth) + "/" + String.valueOf(startYear);
Date example = null;
DateFormat dateFormat;
dateFormat = new SimpleDateFormat("dd/MM/yyyy", Locale.US);
try {
example = new Date(dateFormat.parse(strBDay).getTime());
} catch (ParseException e) {
e.printStackTrace();
}
final Calendar BDay = Calendar.getInstance();
BDay.setTime(example);
final Calendar today = Calendar.getInstance();
// Take your DOB Month and compare it to current month
final int BMonth = BDay.get(Calendar.MONTH);
final int CMonth = today.get(Calendar.MONTH);
BDay.set(Calendar.YEAR, today.get(Calendar.YEAR));
if(BMonth <= CMonth)
{
BDay.set(Calendar.YEAR, today.get(Calendar.YEAR) + 1);
}
long now = new Date().getTime();
// Result in millis
final long millis = BDay.getTimeInMillis() - now;
// Convert to days
final long days = millis / 86400000; // Precalculated (24 * 60 * 60 * 1000) birthday - system time / milliseconds to get the value in days
final long days30 = 2592000000L;
if (millis <= days30 && checkFor16 == 16) {
System.out.println("You are WITHIN 30 days of turning 16yrs old");
}else if (millis > days30 && checkFor16 == 16){
System.out.println("You are MORE THAN 30 DAYS OF BECOMING the age of 16");
}else if(resultYear > 15) {
System.out.println("You are OVER the age of 16");
}else if(checkFor16 < 16 && millis == days30){
System.out.println("You are BELOW the age of 16");
}
System.out.println("Left days to bday: " + days);
//System.out.println("It will be : " + dayOfTheWeek);
}
}
现在每当我在 2016 年 6 月 23 日投入时 - 状况都很好。这意味着你已经 15 岁了,还有 30 天就满 16 岁了。对于我的项目来说,这应该是可以接受的
每当我将生日设置为今天的日期 2016 年 6 月 22 日之前的任何内容(如示例 30 日)时,day
都会返回类似的内容,例如 Left days to bday: 341。而如果我输入 1 日,则为 7 月。它正确返回“距离 16 岁还有 7 天”。如果您输入的日期为 14 岁及以下,则它会正确执行。
如果所有这些解释对任何人来说都令人困惑,我基本上试图获得以下条件:当您使用 DatePicker 输入生日时:
检查以下内容>
- 如果您年满 16 岁,则可以进入。
- 如果您已年满 16 岁,则可以进入。
- 如果您距离 16 岁还有 30 天,您就可以入境。
- 如果您距离 16 岁还有 31 天,那么您将不被允许进入,主要是因为您已经 15 岁了。
- 如果您未满 16 岁(即 15 岁及以下),则不允许您进入。
任何人都可以帮我弄清楚问题是什么,以及为条件添加什么内容,现在可以是简单的 toast 。当我修复此问题并正常工作时,将创建在每个中执行的代码。如果不是,我无法继续该项目
编辑: 我想要
- 如果您将生日设为今天之前的任意一天
在 2000 年,从日期选择器中,它表示您 toast
是 16,CAN 通过。 - 如果您将生日设置为距离今天较远 30 天(1 个月),那么您也可以通过(我估计 是 7 月 24 日),因为您还有 1 个月的时间才能转 16.
- 如果您的生日距今天的日期有 31 天或更远(我估计是 7 月 25 日以上),那么 你不能通过,因为你15岁,有超过30个 距离你生日还有几天。
我对我的代码进行了这样的调整,但它并没有削减它(我想要的如上所述)
final long daysOf30MilliSeconds = days30 / 86400000;
if (days <= daysOf30MilliSeconds && checkFor16 == 16) {
System.out.println("You are WITHIN 30 days of turning 16yrs old");
}else if (days > daysOf30MilliSeconds && checkFor16 == 16 ){
System.out.println("You are MORE THAN 30 DAYS OF BECOMING the age of 16");
}else if(resultYear >= 16) {
System.out.println("You are OVER the age of 16");
}else if(resultYear < 16){
System.out.println("You are BELOW the age of 16");
}else if(resultYear == 16){
System.out.println("You are 16, you can apply");
}
我所需要的只是我认为的条件,有人可以提供问题的答案吗?
最佳答案
这是我用来从生日提取年龄的方法:
private String getAge(int year, int month, int day) {
String age;
int ageYear;
int ageMonth;
Calendar birth = Calendar.getInstance();
Calendar today = Calendar.getInstance();
birth.set(year, month, day);
ageYear = today.get(Calendar.YEAR) - birth.get(Calendar.YEAR);
// if I am borned on february 1st, 2000 and we are on april 1st, 2000 :
// 4 - 2 = 2 months
// if I am borned on april 1st, 2000 and we are on february 1st, 2001
// 12 - (4 - 2) = 10 months
if (today.get(Calendar.MONTH) > birth.get(Calendar.MONTH)) {
ageMonth = today.get(Calendar.MONTH) - birth.get(Calendar.MONTH);
}
else {
ageMonth = 12 - birth.get(Calendar.MONTH) - today.get(Calendar.MONTH);
ageYear--;
}
age = ageYear + " years and " + ageMonth + " months";
return age;
}
ageYear 和 ageMonth 可以是类范围的属性,它们的值可以在条件语句中使用:
if (ageYear >= 16) then ...
if (ageYear == 15 && ageMonth == 11) then...
else then...
返回的年龄仅用于测试目的,可以显示为日志或 toast。
关于java - Android 日历选择生日给出了针对某一条件的剩余天数的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37982497/