Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.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_Simpledateformat_Parseexception - Fatal编程技术网

Java 在构造函数上声明日期

Java 在构造函数上声明日期,java,date,simpledateformat,parseexception,Java,Date,Simpledateformat,Parseexception,刚开始学习java,还有一个练习要完成 我有以下代码: package exercise1; import java.text.SimpleDateFormat; import java.util.Date; public class Singer { //Class Parameters int id; String name; String address; Date dob; int nAlbums; //Overloaded Constructors public Singer(

刚开始学习java,还有一个练习要完成

我有以下代码:

package exercise1;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Singer {

//Class Parameters
int id;
String name;
String address;
Date dob;
int nAlbums;

//Overloaded Constructors
public Singer()
{
    
}

public Singer(int id)
{
this.id = id;
}

public Singer(int id, String name)
{
this.id = id;
this.name = name;
}

public Singer(int id, String name, String address)
{
this.id = id;
this.name = name;
this.address = address;
}

public Singer(int id, String name, String address, Date dob)
{
this.id = id;
this.name = name;
this.address = address;
this.dob = dob;
}

public Singer(int id, String name, String address, Date dob, int nAlbums)
{
this.id = id;
this.name = name;
this.address = address;
this.dob = dob;
this.nAlbums = nAlbums;
}

//All Setters
public void setId (int id)
{

    this.id = id;
    
}

public void setName (String name)
{

    this.name = name;
    
}

public void setAddress (String address)
{

    this.address = address;
    
}

public void setDOB (Date dob)
{

    this.dob = dob;
    
}

public void setNAlbums (int nAlbums)
{

    this.nAlbums = nAlbums;
    
}

public void setSinger (int id, String name, String address, Date dob, int nAlbums)
{
    this.id = id;
    this.name = name;
    this.address = address;
    this.dob = dob;
    this.nAlbums = nAlbums;
    
}

//All Getters

public int ID ()
{
    return id;
}

public String Name ()
{
    return name;
}

public String Address ()
{
    return address;
}

public Date DOB ()
{
    return dob;
}

public int NAlbums ()
{
    return nAlbums;
}

public String Display()
{
    return "\nID: " + id + "\nName: " + name + "\nAddress: " + address + "\nBirthday: " +dob+ "\nNumber of Albums: "+nAlbums;
}
public static void main(String[] args) {
    
    String pattern = "yyyy-MM-dd";
    SimpleDateFormat sdf = new SimpleDateFormat(pattern);
    
    Singer singer1 = new Singer();

    System.out.println("\nID (singer1): "+ singer1.id);

    System.out.println("\nName (singer1) :"+ singer1.name);

    System.out.println("\nAddress (singer1) :"+ singer1.address);
    
    System.out.println("\nBirthday (singer1) :"+ singer1.dob);
    
    System.out.println("\nNumber of Albums (singer1) :"+ singer1.nAlbums);

        singer1.setSinger(1, "Davy Jones", "12 Main Street", sdf.parse("1947-01-08"), 27);
            
    System.out.println("\nsinger1 Details: "+ singer1.Display());

        
}
}

因此发生了两个错误:

  • 它要求我抛出一个异常
  • 即使我接受更正,我也会通过以下格式收到日期:
  • 1详细信息:
    身份证号码:1
    姓名:戴维·琼斯
    地址:美因街12号
    生日:星期三1947年1月8日00:01:00东部时间
    专辑数量:27张
    
    使用Eclipse

    提前谢谢你的帮助

    编辑

    感谢您的帮助,以下是最终代码:

    package exercise1;
    
    import java.time.LocalDate;
    import java.time.format.DateTimeFormatter;
    import java.util.Locale;
    
    public class Singer {
    
    //Class Parameters
    int id;
    String name;
    String address;
    LocalDate dob;
    int nAlbums;
    
    //Overloaded Constructors
    public Singer()
    {
        
    }
    
    public Singer(int id)
    {
    this.id = id;
    }
    
    public Singer(int id, String name)
    {
    this.id = id;
    this.name = name;
    }
    
    public Singer(int id, String name, String address)
    {
    this.id = id;
    this.name = name;
    this.address = address;
    }
    
    public Singer(int id, String name, String address, LocalDate dob)
    {
    this.id = id;
    this.name = name;
    this.address = address;
    this.dob = dob;
    }
    
    public Singer(int id, String name, String address, LocalDate dob, int nAlbums)
    {
    this.id = id;
    this.name = name;
    this.address = address;
    this.dob = dob;
    this.nAlbums = nAlbums;
    }
    
    //All Setters
    public void setId (int id)
    {
    
        this.id = id;
        
    }
    
    public void setName (String name)
    {
    
        this.name = name;
        
    }
    
    public void setAddress (String address)
    {
    
        this.address = address;
        
    }
    
    public void setDOB (LocalDate dob)
    {
    
        this.dob = dob;
        
    }
    
    public void setNAlbums (int nAlbums)
    {
    
        this.nAlbums = nAlbums;
        
    }
    
    public void setSinger (int id, String name, String address, LocalDate dob, int nAlbums)
    {
        this.id = id;
        this.name = name;
        this.address = address;
        this.dob = dob;
        this.nAlbums = nAlbums;
        
    }
    
    //All Getters
    
    public int ID ()
    {
        return id;
    }
    
    public String Name ()
    {
        return name;
    }
    
    public String Address ()
    {
        return address;
    }
    
    public LocalDate DOB ()
    {
        return dob;
    }
    
    public int NAlbums ()
    {
        return nAlbums;
    }
    
    public String Display()
    {
        return "\nID: " + id + "\nName: " + name + "\nAddress: " + address + "\nBirthday: " +dob+ "\nNumber of Albums: "+nAlbums;
    }
    public static void main(String[] args) {
        
        Singer singer1 = new Singer();
        
        System.out.println("\nID (singer1): "+ singer1.id);
    
        System.out.println("\nName (singer1) :"+ singer1.name);
    
        System.out.println("\nAddress (singer1) :"+ singer1.address);
        
        System.out.println("\nBirthday (singer1) :"+ singer1.dob);
        
        System.out.println("\nNumber of Albums (singer1) :"+ singer1.nAlbums);
    
        singer1.id = 1;
        
        singer1.name = "Davy Jones";
        
        singer1.address = "12 Main Street";
        
        String string = "January 08, 1947";
        
        DateTimeFormatter pattern = DateTimeFormatter.ofPattern("MMMM dd, yyyy", Locale.ENGLISH);
        
        LocalDate date = LocalDate.parse(string, pattern);
        
        singer1.dob = date;
        
        singer1.nAlbums = 27;
        
        System.out.println("\nAll singer properties set\n");
        
        System.out.println("\nsinger1 Details: "+ singer1.Display());
        
    
    }
    

    }

    这个问题需要澄清:您遇到了一个java错误,要求您抛出一个ParseException或捕获一个?不管是哪种方式,我们为什么要在这里这样做?Oracle doc表示SimpleDataFormat的各种构造函数和方法都可以抛出NullPointerException或IllegalArgumentException。我在任何地方都看不到ParseException。#1请参阅顶部的链接,该链接解释了什么是选中的异常。--#2:这是打印
    Date
    值时的默认格式,如
    Date
    方法的javadoc中所述。如果需要其他格式,请使用
    SimpleDateFormat
    并调用
    format()
    方法。我建议不要使用
    SimpleDateFormat
    Date
    。这些类设计得很糟糕,而且早已过时,其中前者尤其令人讨厌。而是使用
    LocalDate
    DateTimeFormatter
    ,两者都来自。这也不需要声明
    thorws ParseException
    ,它会将日期显示为
    1947-01-08
    ,非常感谢您的回复。根据@OleV.V。建议我把重点放在本地日期上。它确实感觉更平滑了。再次感谢!