【问题标题】:Accepting variable Date and final int and calculating the day differenmce [duplicate]接受变量 Date 和 final int 并计算天差 [重复]
【发布时间】:2018-09-02 22:00:52
【问题描述】:

我有一个关于方法的赋值,以及 Date 类型的 2 个变量:

  • _borrowedDate(也与 Date 方法有关)
  • _returnDate(也是Date 类型)

我还有一个int MAX_DAYS = 30

我需要设置一个borrowdate,然后接受一个returndate,如果返回日期的天数大于30(MAX_DAYS),与借用日期相比,我应该输出真的。

public class Book {
    private String _title;
    private String _author;
    private int _yearPublished;
    private int _noOfPages;
    private boolean _borrowed;
    private String _studentName;
    private Date _borrowedDate;
    private Date _returnDate;
    final int MAX_DAYS = 30;
    final int FINE = 5;
    final int MIN_YEAR =1800;
    final int MAX_YEAR = 2018;
    final int DEFAULT_YEAR = 2000;
    int d1;
    int days;

    public Book( String title, String author, int yearPublished, int noOfPages)
    {
        if (yearPublished <MIN_YEAR || yearPublished >MAX_YEAR){
            _yearPublished = DEFAULT_YEAR;
            _title = title;
            _author = author;
            _noOfPages = noOfPages;}
        else{
            _title = title;
            _author = author;
            _yearPublished = yearPublished;
            _noOfPages = noOfPages;

        }
    }

    public Book ( Book other) {
        _title = other._title;
        _author = other._author;
        _yearPublished = other._yearPublished;
        _noOfPages = other._noOfPages;
        _borrowed = other._borrowed;
        _studentName = other._studentName;
        _borrowedDate = other._borrowedDate;
        _returnDate = other._returnDate;

    }

    public boolean equal(Book compare){
        return ((_title == compare._title)&&(_author==compare._author)&&(_yearPublished==compare._yearPublished)
            &&(_noOfPages==compare._noOfPages)&&(_borrowed==compare._borrowed)&&(_studentName==compare._studentName)
            &&(_borrowedDate==compare._borrowedDate)&&(_returnDate==compare._returnDate));
    }

    boolean olderBook(Book other){
        return other._yearPublished > _yearPublished;
    }

    public int getYear(){
        return _yearPublished;
    }

    public int getPages(){
        return _noOfPages;
    }

    public String getTitle(){
        return _title;
    }

    public String getAuthor(){
        return _author;
    }

    public boolean getBorrowed(){
        return _borrowed;
    }

    public String toString(){
        return "Title:"+_title+"Author:"+_author+ "Year:"+_yearPublished+","+_noOfPages+"pages";
    }

    private  int calculateDate(int day, int month,int year){
        if(month<3){
            year--;
            month += 12;
        }
        return  365*year+year/4-year/100+year/400+((month+1)*306)/10+(day-62);
    }

    public void borrowBook(String name, Date d){
        _studentName = name;
        _borrowedDate = new Date(d);
        _borrowed = true;
    }

    public boolean returnBook(Date d){
        _returnDate = new Date(d);
        _studentName = null;

        return (_returnDate.after(_borrowedDate));
    }

    public int howLongBorrowed(Date d){
        return 5;   
    }

    public void setYear(int n){
        if(n > MAX_YEAR || n<MIN_YEAR){
            _yearPublished = _yearPublished;
        }
        else
            _yearPublished = n;
    }

    public Date getBorrowedDate(){
        return _borrowedDate;   
    }

    public String getStudentName(){
        return _studentName;
    }

    public Date getReturnDate(){
        return _returnDate;
    }

    public void setTitle(String s){
        _title = s;
    }

    public void setAuthor(String s){
        _author = s;
    }

    public void setPages(int n){
        _noOfPages = n;
    }

    public boolean sameAuthor(Book other){
        return other._author.equals(_author);
    }
}

代码尚未完成,只是在这个特定的事情上遇到了麻烦。 我尝试了明显的&gt;,但由于变量类型不同,它不起作用。

.after 有效,但我需要在 30 天后专门做,仅此而已。

【问题讨论】:

  • 欢迎来到 Stack Overflow!请拿起tour,环顾四周,通读help center,尤其是How do I ask a good question? 分配通常不是任意的;您的讲师、教程或课程将涵盖使您能够做到这一点的必要主题。 查看您的课程资料、课堂笔记等并尝试完成这项工作。 如果您遇到特定问题,请彻底研究它,search thoroughly here,如果您仍然遇到问题,请发布您的代码和问题描述。人们会很乐意提供帮助。
  • 与其描述您的代码,不如将其显示为 as 代码 - 并显示您迄今为止尝试过的内容以及出了什么问题。您还应该考虑 Date 值只是“自 Unix 纪元以来的毫秒数”。您的“天数”是否真的意味着 24 小时周期?如果是这样,您是否打算考虑一天中的时间?这听起来像是您需要实际日期(“2018 年 3 月 24 日”等)而不是时间点的系统。

标签: java date date-difference


【解决方案1】:

如果您使用的是 Java 8 或更高版本,最好使用 LocalDate 而不是 java.util.Date

LocalDate today = LocalDate.now();
LocalDate later = today.plusDays(30);

LocalDate 对象与isBeforeisEqualisAfter 方法进行比较。

如果您使用的是早期版本,或者出于某种原因您只是更喜欢使用 Date,则需要一个 Calendar 实例来执行此操作:

Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, 30);

【讨论】:

  • 感谢您的回答。但是有没有办法在它自己的方法中做到这一点?例如,对于公共布尔返回日期。谢谢
  • 请解释一下究竟是哪种方法以及它应该做什么,因为你没有包含名称,而且很难理解你想要实现的确切目标。
  • 首先,我必须使用 Borrowbook。我在那里设置了 Date 对象(来自另一个类)。然后我使用 returnbook 方法,它接受另一个日期对象。有了这个,我需要计算从借书日期过去的天数,如果它高于 MAX_DAYS(30),我需要返回 true。
  • 您已经掌握了方法,您现在知道如何使用after 方法,以及如何为您的Date 对象添加天数。到目前为止,您尝试了哪些方法来解决您的问题?
  • 要清楚......应该不再需要再次使用 DateCalendar 类。它们令人困惑,设计不良且麻烦。它们被带有JSR 310java.time 类所取代。要与旧代码互操作,您可以通过调用添加到旧类的新方法在旧类和现代类之间进行转换。
猜你喜欢
  • 2021-10-22
  • 1970-01-01
  • 1970-01-01
  • 2013-07-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多