无法在电影数组中的新电影对象上添加发布日期 (Java)

发布于 2025-01-17 14:58:02 字数 999 浏览 0 评论 0原文

在我的Java分配中需要您的帮助。我已经尝试了多种方法,但是我仍然无法为新电影添加发布日期信息(除了一些例外处理)。它不能正确解析。然后,我尝试在电影阵列构造函数上添加“新日期”,它正在生成一个随机日期。

请参阅下面的代码:

//Creates the new film Object to be inserted into the array
        Movie addMovie = new Movie(0,"","",new Date(),0,0);

注意:新电影具有以上匹配的格式:(int Movieid,字符串标题,字符串类型,日期发行日期,int持续时间,int Cost)

//CODE SNIPPET only for adding RELEASE DATE for new MOVIE:
        //set DATE - DATE
System.out.println("Please enter RELEASE DATE: ");
SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd");
addFilm.setReleaseDate(input.nextLine(ft.parse);


//        //throws exception for invalid date
if(addFilm.getReleaseDate().before(1893-12-22) || addFilm.getReleaseDate().after(2019-12-25) {
throw new Exception("Please enter a film release date older that the film industry or sometime in the future!");
}
input.nextLine();

我在这里缺少任何想法?

Need your help in my Java assignment. I've tried multiple ways but I am still unable to add a Release Date info for a new Movie (with some exception handling). It is not parsing correctly. Then I tried to add "new Date" on the Movie array constructor, it's generating a random date.

Please see code below:

//Creates the new film Object to be inserted into the array
        Movie addMovie = new Movie(0,"","",new Date(),0,0);

NOTE: NEW MOVIE is has this format matching above: (int MovieID, String Title, String genre, Date Release Date, int duration, int cost)

//CODE SNIPPET only for adding RELEASE DATE for new MOVIE:
        //set DATE - DATE
System.out.println("Please enter RELEASE DATE: ");
SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd");
addFilm.setReleaseDate(input.nextLine(ft.parse);


//        //throws exception for invalid date
if(addFilm.getReleaseDate().before(1893-12-22) || addFilm.getReleaseDate().after(2019-12-25) {
throw new Exception("Please enter a film release date older that the film industry or sometime in the future!");
}
input.nextLine();

Any thoughts what am I missing here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我很坚强 2025-01-24 14:58:02

错误字段名称

一个特定的问题是,您不正确地命名了自定义类的成员字段。

您说的类定义包括:

… , Date Release Date, …

Java中的字段和变量名称必须是一个单词,没有空格。您的名字“发布日期”违反了该规则。

此外,该名称违反了Java命名起始字段的命名和带有小写字母的VAR名称。

正确的命名:

… , Date releaseDate, …

或更简单:

… , Date released, …

有缺陷的文字,

您也有一些错误的代码,您通过了您认为是日期值的字面形式,但实际上是具有算术的整数文字。

.before(1893-12-22)

在该行中,您通过了int值1,859。这是从1,893开始,然后减去两个数字,十二和22个的结果。自1970年1月1日,格林尼治标准时间1月1日,您所传递的这个数字被解释为指定数量的毫秒数。

但更重要的是,您绝不应该使用类date nor SimpleDateFormat

java.time

您使用的是几年前由JSR 310中定义的现代 java.Time 类取代的可怕的日期时间类。

对于仅日期价值而言,一天和没有时区,使用localdate。通过调用静态出厂方法(例如的)来实例化该类型的不变对象。

LocalDate ld = LocalDate.of( 2022 , Month.MAY , 23 ) ;

让我们以您的电影示例。

record Movie( String title , LocalDate released ) {}
List< Movie > movies = 
    List.of(
        new Movie( "Gone With The Wind" , LocalDate.of( 1939, 12 , 15 ) ) , 
        new Movie( "Purple Rain" , LocalDate.of( 1984, 7 , 27 ) ) 
    ) ;

Faulty field name

One specific problem is that you improperly named the member field of your custom class.

You said your class definition included:

… , Date Release Date, …

Field and variable names in Java must be a single word, with no spaces. Your name “Release Date” violates that rule.

Also, that name violates the Java naming convention of starting field and var names with a lowercase letter.

Correct naming:

… , Date releaseDate, …

Or more simply:

… , Date released, …

Faulty literals

You also have some faulty code where you passed what you thought was a literal form of a date value, but was actually integer literals with arithmetic.

.before(1893-12-22)

There in that line you passed the int value of 1,859. That is the result of starting with 1,893 and then subtracting two numbers, twelve and twenty-two. That number you passed was interpreted as a specified number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT.

But more importantly, you should never be using the class Date nor SimpleDateFormat.

java.time

You are using terrible date-time classes that were years ago supplanted by the modern java.time classes defined in JSR 310.

For a date-only value without time of day and without time zone, use LocalDate. Instantiate immutable objects of that type by calling the static factory methods such as of.

LocalDate ld = LocalDate.of( 2022 , Month.MAY , 23 ) ;

Let’s take your movies example.

record Movie( String title , LocalDate released ) {}
List< Movie > movies = 
    List.of(
        new Movie( "Gone With The Wind" , LocalDate.of( 1939, 12 , 15 ) ) , 
        new Movie( "Purple Rain" , LocalDate.of( 1984, 7 , 27 ) ) 
    ) ;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文