Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Groovy中计算一个人的详细年龄?_Java_Grails_Groovy - Fatal编程技术网

Java 在Groovy中计算一个人的详细年龄?

Java 在Groovy中计算一个人的详细年龄?,java,grails,groovy,Java,Grails,Groovy,我想用以下方法计算一个人的年龄: 设x为一个人的年龄 如果x

我想用以下方法计算一个人的年龄:

设x为一个人的年龄

  • 如果x<1周,返回年龄(天)
  • 如果1周
  • 如果1个月
  • 如果1年

我如何计算这种年龄函数?我建议您使用最小的可行时间单位(以天为单位)来重申您的情况,并使用它来构建您的if语句

// x is age in days
if (x < 7) {
    return x;
}
if (x < 31) {
    return x/7;
}
//x是以天为单位的年龄
if(x<7){
返回x;
}
if(x<31){
返回x/7;
}

你还需要决定你考虑一个月和一年的时间,因为两者都是不同的。

< P>我建议你用最小的可行时间单位重新陈述你的条件,在你的情况下,用它来建立你的if语句

// x is age in days
if (x < 7) {
    return x;
}
if (x < 31) {
    return x/7;
}
//x是以天为单位的年龄
if(x<7){
返回x;
}
if(x<31){
返回x/7;
}

你还需要决定你考虑一个月和一年的时间,因为两者都是不同的。

< P>我建议你用最小的可行时间单位重新陈述你的条件,在你的情况下,用它来建立你的if语句

// x is age in days
if (x < 7) {
    return x;
}
if (x < 31) {
    return x/7;
}
//x是以天为单位的年龄
if(x<7){
返回x;
}
if(x<31){
返回x/7;
}

你还需要决定你考虑一个月和一年的时间,因为两者都是不同的。

< P>我建议你用最小的可行时间单位重新陈述你的条件,在你的情况下,用它来建立你的if语句

// x is age in days
if (x < 7) {
    return x;
}
if (x < 31) {
    return x/7;
}
//x是以天为单位的年龄
if(x<7){
返回x;
}
if(x<31){
返回x/7;
}

你还需要决定你考虑一个月和一年的时间,因为两者都是不同的。

< P>当我使用日期的时候,我建议你使用JoDA时间()。然后,您可以执行以下操作:

import org.joda.time.DateTime
import org.joda.time.Period

String getAge(birthday) {
    def period = new Period(birthday, new DateTime())

    int years = period.getYears()
    int months = period.getMonths()
    int weeks = period.getWeeks()
    int days = period.getDays()

    String age

    if (years > 0) {
        age = "${years} year(s) and ${months} month(s)"
    } else if (months > 0) {
        age = "${months} month(s)"
    } else if (weeks > 0) {
        age = "${weeks} week(s)"
    } else {
        age = "${days} day(s)"
    }

    return age
}
有关更多信息,请参阅DateTime()和Period()的文档


既然你用Grails标记了你的问题,我猜你需要Grails项目的这个功能。如果是这样,您可以使用Joda Time插件()来添加所需的依赖项。

在处理日期时,我建议您使用Joda Time()。然后,您可以执行以下操作:

import org.joda.time.DateTime
import org.joda.time.Period

String getAge(birthday) {
    def period = new Period(birthday, new DateTime())

    int years = period.getYears()
    int months = period.getMonths()
    int weeks = period.getWeeks()
    int days = period.getDays()

    String age

    if (years > 0) {
        age = "${years} year(s) and ${months} month(s)"
    } else if (months > 0) {
        age = "${months} month(s)"
    } else if (weeks > 0) {
        age = "${weeks} week(s)"
    } else {
        age = "${days} day(s)"
    }

    return age
}
有关更多信息,请参阅DateTime()和Period()的文档


既然你用Grails标记了你的问题,我猜你需要Grails项目的这个功能。如果是这样,您可以使用Joda Time插件()来添加所需的依赖项。

在处理日期时,我建议您使用Joda Time()。然后,您可以执行以下操作:

import org.joda.time.DateTime
import org.joda.time.Period

String getAge(birthday) {
    def period = new Period(birthday, new DateTime())

    int years = period.getYears()
    int months = period.getMonths()
    int weeks = period.getWeeks()
    int days = period.getDays()

    String age

    if (years > 0) {
        age = "${years} year(s) and ${months} month(s)"
    } else if (months > 0) {
        age = "${months} month(s)"
    } else if (weeks > 0) {
        age = "${weeks} week(s)"
    } else {
        age = "${days} day(s)"
    }

    return age
}
有关更多信息,请参阅DateTime()和Period()的文档


既然你用Grails标记了你的问题,我猜你需要Grails项目的这个功能。如果是这样,您可以使用Joda Time插件()来添加所需的依赖项。

在处理日期时,我建议您使用Joda Time()。然后,您可以执行以下操作:

import org.joda.time.DateTime
import org.joda.time.Period

String getAge(birthday) {
    def period = new Period(birthday, new DateTime())

    int years = period.getYears()
    int months = period.getMonths()
    int weeks = period.getWeeks()
    int days = period.getDays()

    String age

    if (years > 0) {
        age = "${years} year(s) and ${months} month(s)"
    } else if (months > 0) {
        age = "${months} month(s)"
    } else if (weeks > 0) {
        age = "${weeks} week(s)"
    } else {
        age = "${days} day(s)"
    }

    return age
}
有关更多信息,请参阅DateTime()和Period()的文档


既然你用Grails标记了你的问题,我猜你需要Grails项目的这个功能。如果是这样,您可以使用Joda Time plugin()添加所需的依赖项。

一个月的长度应该是多少?ie:你想怎么说
1个月
?还有,你打算用闰年做什么?另外,如果我出生在1月31日,你会显示我在2月28日的年龄吗?一个月的长度应该是多少?ie:你想怎么说
1个月
?还有,你打算用闰年做什么?另外,如果我出生在1月31日,你会显示我在2月28日的年龄吗?一个月的长度应该是多少?ie:你想怎么说
1个月
?还有,你打算用闰年做什么?另外,如果我出生在1月31日,你会显示我在2月28日的年龄吗?一个月的长度应该是多少?ie:你想怎么说
1个月
?还有,你打算用闰年做什么?另外,如果我出生在1月31日,你会显示我在2月28日的年龄吗?这很接近,但我需要的是以下信息。当年龄为5周时,应输出1个月和1周。几个星期都是这样。8天应输出1周和1天。14个月应该是1年2个月。@Confile说我出生在10月1日。在10月31日,我将是4周零2天大,那么它应该打印什么?这很接近,但我需要的是以下内容。当年龄为5周时,应输出1个月和1周。几个星期都是这样。8天应输出1周和1天。14个月应该是1年2个月。@Confile说我出生在10月1日。在10月31日,我将是4周零2天大,那么它应该打印什么?这很接近,但我需要的是以下内容。当年龄为5周时,应输出1个月和1周。几个星期都是这样。8天应输出1周和1天。14个月应该是1年2个月。@Confile说我出生在10月1日。在10月31日,我将是4周零2天大,那么它应该打印什么?这很接近,但我需要的是以下内容。当年龄为5周时,应输出1个月和1周。几个星期都是这样。8天应输出1周和1天。14个月应该是1年2个月。@Confile说我出生在10月1日。在10月31日,我将是4周零2天大,那么它应该打印什么呢?