Java 在类对象的构造函数中插入 Date 参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29955983/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Insert a Date parameter in the constructor of a class object
提问by Asdrúbal Albi?ana Moreno
I have to create a Cinema project in Java using BlueJ. I have a Class named Movie and a Class named Show. In the Show constructor I want to insert some parameters and I want one to be the date and time of the show using "Date". It should be something like this:
我必须使用 BlueJ 在 Java 中创建一个 Cinema 项目。我有一个名为 Movie 的类和一个名为 Show 的类。在 Show 构造函数中,我想插入一些参数,并且我希望其中一个参数是使用“Date”的节目的日期和时间。它应该是这样的:
public Show(int ID, Movie movie, Date date, int seats)
But I am not able to insert the date. Is there a way to enter it or is it impossible to do it this way?
但我无法插入日期。有没有办法进入它或者不可能这样做?
Thank you for any help.
感谢您的任何帮助。
回答by Floorigin
That should be possible if you structure your class like this:
如果你这样构造你的类,那应该是可能的:
public class Show {
private int id;
private Movie movie;
private Date date;
private int seats;
public Show(int ID, Movie movie, Date date, int seats) {
this.id = ID;
this.movie = movie;
this.date = date;
this.seats = seats;
}
}
Then you should be able to call this constructor:
然后你应该能够调用这个构造函数:
Show show = new Show(5, new Movie(), new java.util.Date(System.currentTimeMillis()), 24);
If you want to specify a specific date, you should use the SimpleDateFormat
class.
如果要指定特定日期,则应使用SimpleDateFormat
该类。
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date yourDate = sdf.parse("1992-07-26");
回答by roeygol
The main issue that you need to use java.util.Date
as your object.
There is another Date object which is used for SQL.
您需要java.util.Date
用作对象的主要问题。
还有另一个用于 SQL 的 Date 对象。
So just add import java.util.Date;
in each file you want to use this certain object.
因此,只需添加import java.util.Date;
要使用此特定对象的每个文件。