无法在电影数组中的新电影对象上添加发布日期 (Java)
在我的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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
错误字段名称
一个特定的问题是,您不正确地命名了自定义类的成员字段。
您说的类定义包括:
Java中的字段和变量名称必须是一个单词,没有空格。您的名字“发布日期”违反了该规则。
此外,该名称违反了Java命名起始字段的命名和带有小写字母的VAR名称。
正确的命名:
或更简单:
有缺陷的文字,
您也有一些错误的代码,您通过了您认为是日期值的字面形式,但实际上是具有算术的整数文字。
在该行中,您通过了
int
值1,859。这是从1,893开始,然后减去两个数字,十二和22个的结果。自1970年1月1日,格林尼治标准时间1月1日,您所传递的这个数字被解释为指定数量的毫秒数。但更重要的是,您绝不应该使用类
date
norSimpleDateFormat
。java.time
您使用的是几年前由JSR 310中定义的现代 java.Time 类取代的可怕的日期时间类。
对于仅日期价值而言,一天和没有时区,使用
localdate
。通过调用静态出厂方法(例如的)来实例化该类型的不变对象。
让我们以您的电影示例。
Faulty field name
One specific problem is that you improperly named the member field of your custom class.
You said your class definition included:
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:
Or more simply:
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.
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
norSimpleDateFormat
.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 asof
.Let’s take your movies example.