小编alp*_*lle的帖子

如何在出生日期准确计算出Java的年龄

我试图计算java中考虑了几个月的年龄,所以减去几年就不行了.我还想告诉用户今天是他们的生日.这是我到目前为止的代码,但我担心它有点偏.它也不会告诉今天是否是生日,即使它比较的两个日期相同.我试图最初计算的方式是使用毫秒.你看到获得当前日期的两种方法的原因是因为我正在尝试让它工作,但是想向每个人展示我的工作,以便他们能够指出我正确的方向.

编辑澄清我的意思是2015-1993可以是22岁或21岁,这取决于他们今年的生日已经过去了.我想确保在考虑到这一点后得到正确的年龄.

public class ShowAgeActivity extends AppCompatActivity {

private TextView usersAge;

private static long daysBetween(Date one, Date two)
{
    long difference = (one.getTime()-two.getTime())/86400000; return Math.abs(difference);
}

private Date getCurrentForBirthday()
{
    Date birthday = (Date) this.getIntent().getExtras().get("TheBirthDay");
    int birthdayYear = birthday.getYear() + 1900;
    Calendar cal = Calendar.getInstance();
    cal.set(birthdayYear, Calendar.MONTH, Calendar.DAY_OF_MONTH);
    Date current = cal.getTime();
    return current;
}


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_show_age);


    Date birthday = (Date) this.getIntent().getExtras().get("TheBirthDay");
      Date currentDay = Calendar.getInstance().getTime();

      long age = daysBetween(birthday,currentDay)/365;

      usersAge =(TextView)findViewById(R.id.ageTextView);

    if (birthday.compareTo(getCurrentForBirthday()) == …
Run Code Online (Sandbox Code Playgroud)

java android calendar date compareto

3
推荐指数
1
解决办法
8857
查看次数

如何返回一个没有重复的属性的列表?

我有一个从数据库中提取州和城市列表的方法.州是独一无二的,但该州可能有许多城市.我的方法目前所做的是将每个州和城市对作为单独的项目返回.我需要它做的是拥有许多城市的州.

目前退货

辛辛那提

哦 - 克利夫兰

哦,findlay

在印第安纳波利斯

我需要它返回

哦,辛辛那提,克利夫兰,芬德利

在印第安纳波利斯

模型

public class Location
{
    public string State { get; set; }
    public string city { get; set; }

}
Run Code Online (Sandbox Code Playgroud)

REPOSITORY

public HashSet<Location> getlocation()
    {
        HashSet<Location> myHashset = new HashSet<Location>();

        const string storedProc = "someProc";

        dynamic locations;


        using (var conn = DbFactory.myConnection())
        {
            locations = conn.Query(storedProc, commandType: CommandType.StoredProcedure);

        }

        foreach (var location in locations)
        {

                myHashset.Add(new location{State = location.state,City = location.city});


        }
          return myHashset
    }
Run Code Online (Sandbox Code Playgroud)

c# list unique hashset

0
推荐指数
1
解决办法
61
查看次数

标签 统计

android ×1

c# ×1

calendar ×1

compareto ×1

date ×1

hashset ×1

java ×1

list ×1

unique ×1