簡體   English   中英

如何在BlueJ“創建對象”對話框中輸入LocalDate值

[英]How to enter a LocalDate value into BlueJ “Create Object” dialog box

我不嘗試將日期格式設置為YYYY-MM-DD或dd / MM / YYYY。 我在問LocalDate的字面格式。

我剛剛開始學習Java,並且正在使用稱為BlueJ的IDE。 我想創建一個測試方法。

屏幕截圖將顯示我正在嘗試做的事情 忽略底部的錯誤部分

現在,從構造函數開始,我們知道它需要一個int,LocalDate和一個double。 我在網上搜索后發現

https://www.javabrahman.com/java-8/java-8-working-with-localdate-localtime-localdatetime-tutorial-with-examples/

java.time.LocalDate:在ISO-86011日歷系統中,LocalDate實例保存一個不帶時區的日期。 LocalDate的默認格式為“ YYYY-MM-DD”,如“ 2016-12-12”一樣。

因此,我將一個普通數字放在10001中作為testID,並將double變成50.5之類的東西,我也知道要為它注冊一個字符串(如果需要),我需要將其括在“字符串”中

但是我嘗試了各種各樣的方式輸入日期,但我會遇到一個錯誤

2018-05-30,30-05-2018,30 / 05/2018將給我

Error: incompatible types: Int cannot be converted to java.time.LocalDate

另一方面,“ 30/05/2018”會給我

Error: Incompatible types: java.lang.String cannot be converted to java.time.LocalDate

如果我嘗試30.05.2018它會說

Error: ';' expected

如果我嘗試'2018-05-30'會說

Error: unclosed character literal

我沒有辦法嘗試。 因此,如果您能告訴我如何將其放入其中,那就太好了。

我只是真的需要知道BlueJ如何讓我輸入它。 因為BlueJ的資源在線如此稀疏。


碼:

import java.time.LocalDate;
import java.util.ArrayList;
/**
 * Write a description of class TestPaper here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class TestPaper
{
    // instance variables - replace the example below with your own
    private int testID;
    private LocalDate testDate;
    private double testMarks;
    private ArrayList<MCQ> MCQDetails;

    /**
     * Constructor for objects of class TestPaper
     */
    public TestPaper(int testID, LocalDate testDate, double testMarks)
    {
        this.testID = testID;
        this.testDate = testDate;
        this.testMarks = testMarks;
        MCQDetails = new ArrayList<MCQ>() ; 
    }

/**
 * Accessor Method getTestID to get the testID
 *
 * @return int value of the choice ID
 */
public int getTestID(){
    return testID;
}

/**
 * Mutator Method to set the testID
 * 
 *  @param int format of the testID to set
 */
public void setTestID(int testID){
    this.testID = testID;
}

/**
 * Accessor Method getTestMarks to get the Test Marks
 *
 * @return double value of the test marks 
 */
public double getTestMarks(){
    return testMarks;
}

/**
 * Mutator Method to set the testMarks
 * 
 *  @param String format of the choice Description to be set
 */
public void setTestMarks(double testMarks){
    this.testMarks = testMarks;
}

    /**
 * Accessor Method getTestDate to get the testDate
 *
 * @return LocalDate value of the testDate
 */
public LocalDate getTestDate(){
    return testDate;
}

/**
 * Mutator Method to set the testDate
 * 
 *  @param LocalDate format of the testDate to set
 */
public void setTestDate(LocalDate testDate){
    this.testDate = testDate;
}

/**
 * Method addMCQ will allow users to add a MCQ Object to the list of MCQ
 *
 * @param addMCQ a MCQ Object
 * @return boolean will return true if it is successfully added or false if not
 */
public boolean addMCQ(MCQ MCQName)
{
    return MCQDetails.add(MCQName);
}

/**
 * Method removeMCQ to remove an MCQ object from the Arraylist
 *
 * @param MCQName A parameter of type MCQ 
 */
public void removeMCQ(MCQ MCQName)
{
    MCQDetails.remove(MCQName);
}

/**
 * Method listMCQ to return a list of MCQ arraylist
 *
 * @return The return value of MCQDetails (MCQ Arraylist)
 */
public ArrayList<MCQ> listMCQ()
{
    return MCQDetails;
}

    public MCQ findMCQ(int MCQID)
{
    for(MCQ m : MCQDetails)
    {
        if(m.getQuestionID() == MCQID)
        {
            return m;
        }
    }
    return null;
}

嘗試轉換呼叫中的LocalDate,例如:

TestPaper (2018-05-30, LocalDate.parse("2018/05/30"), 30/05/2018);

您可以在LocalDate中使用其他靜態方法。 有關更多示例,請參見此處

從上面的評論中,請不要忘記導入:

import java.time.LocalDate;

包括包裝

如評論中所述,解決方案是添加創建LocaDate的代碼,但是LocaDate需要帶有包前綴“ java.time”的完全合格的類名:

java.time.LocalDate.of(2018, 5, 30)

不知道為什么它不能只與LocalDate.of(...) (即使導入了correclty類也是如此),但是至少可以這樣。


只是另一個細節: 日期沒有格式 諸如LocalDate類的類僅保存值(在這種情況下,它具有年,月和日的值),但是日期本身根本沒有格式。 可以用許多不同的格式表示相同的日期: May 30th 20182018-05-30 / 30/05/18是不同的格式,但是都表示相同的日期。 日期對象僅包含值,並且您可以選擇想要表示它的任何格式。

當您打印LocalDate ,它隱式調用toString() ,默認情況下會選擇yyyy-MM-dd格式,這是ISO 8601格式,但是正如我所說,這只是格式化日期的多種可能方式之一(盡管該值始終保持不變)。 告訴“日期具有格式”是錯誤的並且具有誤導性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM