如何在BlueJ"Create Object"中输入LocalDate值.对话框 [英] How to enter a LocalDate value into BlueJ "Create Object" dialog box

查看:115
本文介绍了如何在BlueJ"Create Object"中输入LocalDate值.对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不打算用YYYY-MM-DD或dd/MM/YYYY格式化日期.我在问LocalDate的字面格式.

I'm not trying to format the date in YYYY-MM-DD or dd/MM/YYYY. I'm asking about the literal format of LocalDate.

我刚刚开始学习Java,并且正在使用称为BlueJ的IDE.我想创建一个测试方法.

I just started learning Java and I am using this IDE called BlueJ. and I want to create a test method.

屏幕截图将显示我正在尝试做的事情

The screenshot will show what I am trying to do

现在,因为从构造函数中我们知道它需要一个int,LocalDate和一个double.我在网上搜索后发现

Now since from the constructor we know that it requires a int, LocalDate and a double. I've searched online and found that

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

java.time.LocalDate:LocalDate实例保存一个没有时间的日期 ISO-86011日历系统中的区域. LocalDate具有默认格式 "YYYY-MM-DD",如"2016-12-12".

java.time.LocalDate: A LocalDate instance holds a date without a time zone, in ISO-86011 calendar system. LocalDate has the default format ‘YYYY-MM-DD’ as in ‘2016-12-12’.

所以我将一个普通数字放在10001作为testID,并将double变成50.5 我也知道,要注册一个字符串(如果需要),我需要将其包含在字符串"中

So I would put a normal number in 10001 for the testID and double would be something like 50.5 I also know that for it to register a string (if it was needed) I would need to enclose it within "string"

但是我尝试了各种各样的日期输入方法,但会出现错误

But I've tried all sorts of way to put in the date and I would be left with an error

2018-05-30,30-05-2018,30/05/2018会给我

2018-05-30,30-05-2018,30/05/2018 would give me

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

另一方面,"30/05/2018"会给我

"30/05/2018" on the other hand would give me

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

如果我尝试30.05.2018它会说

If I try 30.05.2018 it would say

Error: ';' expected

如果我尝试"2018-05-30",它会说

If I try '2018-05-30' it would say

Error: unclosed character literal

我用尽了所有尝试的方法.因此,如果您能告诉我如何将其放入其中,那就太好了.

I ran out of ways to try it. So if you could tell me how I should put it in there, that would be great.

我真的真的需要知道BlueJ如何让我输入它.因为BlueJ的资源在线如此稀疏.

I just really need to know how BlueJ wants me to input it. Cause the resources for BlueJ is so sparse online.

代码:

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;
}

推荐答案

包含包

正如评论中所讨论的,解决方案是添加创建LocaDate的代码,但是bluej需要带有包前缀"java.time"的完全合格的类名:

Include package

As discussed in the comments, the solution is to add the code that creates the LocaDate, but bluej needs the fully qualified class name with the package prefix "java.time.":

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

不知道为什么它不仅仅适用于LocalDate.of(...)(即使导入了correclty类),但至少可以奏效.

Not sure why it doesn't work with just LocalDate.of(...) (even with the class correclty imported), but at least this works.

还有另一个细节:日期没有格式.诸如LocalDate之类的类仅保存值(在这种情况下,它具有年,月和日的值),但是日期本身根本没有格式.可以用许多不同的格式表示相同的日期:May 30th 20182018-05-3030/05/18是不同的格式,但是都表示相同的日期.日期对象只保存值,您可以选择任何一种表示它的格式.

Just another detail: a date has no format. Classes like LocalDate just holds values (in this case, it has year, month and day values), but a date itself has no format at all. The same date can be represented in many different formats: May 30th 2018, 2018-05-30, 30/05/18 are different formats, but all represent the same date. A date object just holds the values, and you can choose whatever format you want to represent it.

打印LocalDate时,它隐式调用toString(),默认情况下会选择yyyy-MM-dd格式,即 ISO 8601 格式,但是正如我所说,这只是格式化日期的多种可能方式之一(尽管值始终保持不变).告诉日期具有格式" 是错误的并且具有误导性.

When you print a LocalDate, it implicity calls toString(), which by default chooses yyyy-MM-dd format, which is a ISO 8601 format, but as I said, that's just one of the many possible ways to format a date (although the value always stays the same). Telling that "a date has a format" is wrong and misleading.

这篇关于如何在BlueJ"Create Object"中输入LocalDate值.对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆