我是Android工作室的初学者,并创建了一个年龄计算器的应用程序,但我不明白如何从当前日期计算下一个生日以及如何将您的年龄转换为数周,请帮助 谢谢,
答案 0 :(得分:0)
这将有助于获得剩余的日子
import java.text.*;
import java.util.*;
public class NextBday {
public void printRemainingDays()
{
Calendar earlier = Calendar.getInstance();
Calendar later = Calendar.getInstance();
int userDay, currentDay, currentYear, currentMonth;
String delimeter = "/";
String dob = "05/15/1980";
String bDate[] = dob.split(delimeter,3);
currentYear = earlier.get(Calendar.YEAR);
currentMonth = earlier.get(Calendar.MONTH);
currentDay = earlier.get(Calendar.DATE);
earlier.set(currentYear, currentMonth, currentDay);
if(earlier.get(Calendar.MONTH) >= later.get(Calendar.MONTH))
{
later.set(currentYear+1, Integer.valueOf(bDate[0]), Integer.valueOf(bDate[1]));
}
else
later.set(currentYear, Integer.valueOf(bDate[0]), Integer.valueOf(bDate[1]));
int earlierDays = earlier.get( Calendar.DAY_OF_YEAR );
int laterDays = later.get( Calendar.DAY_OF_YEAR );
long remain = laterDays - earlierDays;
System.out.println(remain);
}
}
我希望这会有所帮助!
答案 1 :(得分:0)
如果您需要伪代码,请使用以下代码并编写自己的代码,
String birthDate = "1994-03-10";
String today = "2017-10-25"; // or LocalTime.now();
LocalDate start = LocalDate.parse(birthDate );
LocalDate end = LocalDate.parse(e);
List<LocalDate> totalDates = new ArrayList<>();
while (!start.isAfter(end)) {
totalDates.add(start);
start = start.plusDays(1);
}
答案 2 :(得分:0)
这是正确的方式,请尝试
公共静态字符串computeAge(String strDate){
int years = 0;
int months = 0;
int days = 0;
try {
long timeInMillis = Long.parseLong(strDate);
Date birthDate = new Date(timeInMillis);
//create calendar object for birth day
Calendar birthDay = Calendar.getInstance();
birthDay.setTimeInMillis(birthDate.getTime());
//create calendar object for current day
long currentTime = System.currentTimeMillis();
Calendar now = Calendar.getInstance();
now.setTimeInMillis(currentTime);
//Get difference between years
years = now.get(Calendar.YEAR) - birthDay.get(Calendar.YEAR);
int currMonth = now.get(Calendar.MONTH) + 1;
int birthMonth = birthDay.get(Calendar.MONTH) + 1;
//Get difference between months
months = currMonth - birthMonth;
//if month difference is in negative then reduce years by one and calculate the number of months.
if (months < 0) {
years--;
months = 12 - birthMonth + currMonth;
if (now.get(Calendar.DATE) < birthDay.get(Calendar.DATE))
months--;
} else if (months == 0 && now.get(Calendar.DATE) < birthDay.get(Calendar.DATE)) {
years--;
months = 11;
}
//Calculate the days
if (now.get(Calendar.DATE) > birthDay.get(Calendar.DATE))
days = now.get(Calendar.DATE) - birthDay.get(Calendar.DATE);
else if (now.get(Calendar.DATE) < birthDay.get(Calendar.DATE)) {
int today = now.get(Calendar.DAY_OF_MONTH);
now.add(Calendar.MONTH, -1);
days = now.getActualMaximum(Calendar.DAY_OF_MONTH) - birthDay.get(Calendar.DAY_OF_MONTH) + today;
} else {
days = 0;
if (months == 12) {
years++;
months = 0;
}
}
//adarsh
if (currMonth > birthMonth) {
if (birthDay.get(Calendar.DATE) > now.get(Calendar.DATE)) {
months = months - 1;
}
}//---------------------------------
} catch (Exception e) {
e.printStackTrace();
}
//Create new Age object
return years + " Y " + months + " M " + days + " days";
}