Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.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中的日期计算_Java_Date - Fatal编程技术网

Java中的日期计算

Java中的日期计算,java,date,Java,Date,我有一个人的出生日期。还有一个日期,比如d1。我想知道此人是否在自d1日期起的最后一年内达到40岁。日期为“yyyyMMdd”格式 我想到一些与找出时间有关的事情,做一些减法,然后检查它是否是40等等 执行此计算的最佳方法是什么?getTime()将以毫秒为单位返回时间。 long diff = d2.getTime() - d1.getTime(); // the difference in milliseconds 我将把这些毫秒转换成年作为一种实践。实现这一点的方法有很多,从使用毫秒和简

我有一个人的出生日期。还有一个日期,比如d1。我想知道此人是否在自d1日期起的最后一年内达到40岁。日期为“yyyyMMdd”格式

我想到一些与找出时间有关的事情,做一些减法,然后检查它是否是40等等

执行此计算的最佳方法是什么?

getTime()
将以毫秒为单位返回时间。

long diff = d2.getTime() - d1.getTime(); // the difference in milliseconds

我将把这些毫秒转换成年作为一种实践。

实现这一点的方法有很多,从使用毫秒和简单的减法,到转换成年,再到使用对象

您可能还想看看(一个方便的处理日期的第三方JavaAPI)和

下面是一种使用

公共类数据测试仪{
专用静态最终长毫秒数(单位:年)=31556926279L;
公共静态void main(字符串[]args){
//注意:这些构造函数已弃用
//我只是用它们做个快速测试
日期开始日期=新日期(2013年3月1日);
日期生日=新日期(1981,011981);//返回false
Date birthday2=新日期(1972,03,20);//返回true
Date birthday3=新日期(1973,02,27);//测试边缘案例//返回false
Date birthday4=新日期(1972,02,27);//测试边缘情况,//返回false
System.out.println(年内(生日,起始日期,40));
系统输出打印项次(年内(生日2,起始日期40));
系统输出打印项次(年内(生日3,起始日期40));
系统输出打印项次(年内(生日4,起始日期40));
System.out.println(年内(生日,起始日期,40));
系统输出打印项次(年内(生日2,起始日期40));
}
年内公共静态布尔值(日期生日、日期开始日期、整数年){
如果(生日之前(开始日期)){
long difference=Math.abs(birth.getTime()-startDate.getTime());
int yearDifference=(int)(差值/毫秒,以年为单位);
收益年差<(年+1)和年差>=年;
}
返回false;
}
}
避免遗留日期时间类 其他答案使用了麻烦的旧日期时间类,这些类现在是遗留的,被java.time类取代

使用java.time 该类表示一个仅限日期的值,不包含一天中的时间和时区

LocalDate birthDate = LocalDate.parse( "19540123" , DateTimeFormatter.BASIC_ISO_DATE ) ;
LocalDate d1 = LocalDate.parse( "20110317" , DateTimeFormatter.BASIC_ISO_DATE );

LocalDate dateWhen40YearsOld = birthDate.plusYears( 40 ) ;
Boolean turning40WithinYear = 
    ( ! dateWhen40YearsOld.isBefore( d1 ) )         // "If not before" is a short way of saying "is equal to or later than". 
    && 
    dateWhen40YearsOld.isBefore( d1.plusYears( 1 ) )
;

关于java.time 该框架内置于Java8及更高版本中。这些类取代了麻烦的旧日期时间类,例如,&

该项目现已启动,建议迁移到类

要了解更多信息,请参阅。并搜索堆栈溢出以获得许多示例和解释。规格是

从哪里获得java.time类

  • ,及以后
    • 内置的
    • 标准JavaAPI的一部分,带有捆绑实现
    • Java9添加了一些次要功能和修复
    • 大部分java.time功能都在中向后移植到Java6和Java7
    • 该项目专门为Android采用了ThreeTen Backport(如上所述)

该项目使用其他类扩展了java.time。这个项目是java.time将来可能添加的一个试验场。您可能会在这里找到一些有用的类,如、、和。

不要在有问题的时候使用“紧急”。只是一个友好的建议。你的约会是什么形式的?它是字符串吗?它是yyyyMMdd格式的,所以问题/答案显示了如何计算两个日期之间的天数。你投了反对票,因为?请不要因为这个练习而告诉我。当你计算日期时,有很多事情你可能会做错。天、年的长度不总是一样的,所以不要自己做。仅供参考,像、和
java.text.SimpleTextFormat
这样麻烦的旧日期时间类现在被这些类取代了。同样,Joda Time项目处于维护模式,团队建议迁移到java.Time。
public class DateTester{

    private static final long MILLISECONDS_IN_YEAR = 31556926279L;

    public static void main(String[] args) {
            //Note:  These constructors are deprecated
            //I'm just using them for a quick test

    Date startDate = new Date(2013,03,01);
    Date birthday = new Date(1981,01,1981);//Returns false
    Date birthday2 = new Date(1972,03,20); //Returns true
    Date birthday3 = new Date(1973,02,27); //Test edge cases  //Returns false
    Date birthday4 = new Date(1972,02,27); //Test edge cases, //Returns false


    System.out.println(withinYear(birthday, startDate,40));
    System.out.println(withinYear(birthday2, startDate,40));
    System.out.println(withinYear(birthday3, startDate,40));
    System.out.println(withinYear(birthday4, startDate,40));


        System.out.println(withinYear(birthday, startDate,40));
        System.out.println(withinYear(birthday2, startDate,40));
    }

    public static boolean withinYear(Date birthday, Date startDate, int years){
        if(birthday.before(startDate)){
            long difference = Math.abs(birthday.getTime() - startDate.getTime());
            int yearDifference = (int) (difference/MILLISECONDS_IN_YEAR);
            return yearDifference  < (years +1) && yearDifference >= years;
        }
        return false;
    }
}
LocalDate birthDate = LocalDate.parse( "19540123" , DateTimeFormatter.BASIC_ISO_DATE ) ;
LocalDate d1 = LocalDate.parse( "20110317" , DateTimeFormatter.BASIC_ISO_DATE );

LocalDate dateWhen40YearsOld = birthDate.plusYears( 40 ) ;
Boolean turning40WithinYear = 
    ( ! dateWhen40YearsOld.isBefore( d1 ) )         // "If not before" is a short way of saying "is equal to or later than". 
    && 
    dateWhen40YearsOld.isBefore( d1.plusYears( 1 ) )
;