Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.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 如何使用getter和setter打印特定的日期和时间_Java_Date_Methods - Fatal编程技术网

Java 如何使用getter和setter打印特定的日期和时间

Java 如何使用getter和setter打印特定的日期和时间,java,date,methods,Java,Date,Methods,更新:我从未解决过以所需的特定格式输出日期的问题,但程序的其余部分可以工作。在弄乱了time.format类之后,我确实找到了几种不同的方法以不同的格式输出日期,我能够让所有方法都工作,但列表中的最后一种方法除外,它应该是time.format类的一部分,但不幸的是,我从来没有弄清楚如何实现它。然而,在我看来,这是一个计算利息的简单程序的好例子。我读过很多关于能手和二传手的批评,但他们似乎在这个项目中很好地发挥了作用。请注意,我仍然在学习Java和编程作为一个整体 package account

更新:我从未解决过以所需的特定格式输出日期的问题,但程序的其余部分可以工作。在弄乱了time.format类之后,我确实找到了几种不同的方法以不同的格式输出日期,我能够让所有方法都工作,但列表中的最后一种方法除外,它应该是time.format类的一部分,但不幸的是,我从来没有弄清楚如何实现它。然而,在我看来,这是一个计算利息的简单程序的好例子。我读过很多关于能手和二传手的批评,但他们似乎在这个项目中很好地发挥了作用。请注意,我仍然在学习Java和编程作为一个整体

package accountproject;

// two imports needed for date and time
import java.time.format.*;
import java.time.*;
// import standard exception error text
import java.text.ParseException;
// import EVERYTHING!
import java.util.*;

public class Account {

private static int id = 0;
private static double balance = 0;
private static double annualInterestRate = 0;
private static ZonedDateTime dateCreated;
private static double MonthlyInterestRate = annualInterestRate/12;

public Account()
{
    // empty constructor
}

public Account(int id, double balance, double annualInterestRate, ZonedDateTime dateCreated) {
    super();
    Account.id = 0;
    Account.balance = 0;
    Account.annualInterestRate = 4.5;
}

public int getId() {
    return id;
}

public void setId(int id) {
    Account.id = id;
}

public static double getBalance(double d) {
    return balance;
}

public void setBalance(double balance) {
    Account.balance = balance;
}

public double getAnnualInterestRate() {
    return annualInterestRate;
}

public void setAnnualInterestRate(double annualInterestRate) {
    Account.annualInterestRate = annualInterestRate;
}

public static ZonedDateTime ConvertStringToDate(String dateNow) {
    try
{
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("E MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
    ZonedDateTime date = ZonedDateTime.parse(dateNow, formatter);

    return date;
}
 catch (DateTimeParseException e)
{
    System.out.println(e);
}
    ZonedDateTime date = null;
    return date;
}

public static double getMonthlyInterestRate(double annualInterestRate2) {

    double temp = annualInterestRate2/12;

    MonthlyInterestRate = temp;

    return MonthlyInterestRate;
}

public static double getMonthlyInterest(double newBalance2) {

    double temp = 100/MonthlyInterestRate;

    double temp2 = newBalance2/temp;

    double temp3 = newBalance2 + temp2;

    newBalance2 = temp3;

    return temp2;
}

public static double deposit(double balance, double deposit) {

    double temp = balance + deposit;

    balance = temp;

    return balance;
}

public static double withdrawal(double balance, double withdrawal) {

    double temp = balance - withdrawal;

    balance = temp;

    return balance;
}

    public static void main(String[] args) throws ParseException {

        // establish a scanner and set example values
        Scanner stdin = new Scanner(System.in);
        id = 1122;
        balance = 20000;
        MonthlyInterestRate = .375;
        double withdrawal = 2500;
        double deposit = 3000;

        double balanceExp = deposit(balance,deposit);
        balanceExp = withdrawal(balanceExp,withdrawal);
        double balanceExp2 = getMonthlyInterest(balanceExp);
        double monthlyInterest = balanceExp2;

        String dateExp = "Fri Oct 06 16:10:59 GMT 2017";
        dateCreated = ConvertStringToDate(dateExp);

        System.out.println("SAMPLE: Account ID " + id + " with a balance of $" + balanceExp
                 + ",\nhas accrued $" + monthlyInterest + " in interest and was opened on " 
                + dateCreated + ".");

        System.out.println("Please enter the ID number:");

        // get the id number input
        id = stdin.nextInt();
        stdin.nextLine();

        System.out.println("Typically, the original balance will be $20,000.00.\nPlease enter the balance:");

        // get the starting balance input
        balance = stdin.nextInt();
        stdin.nextLine();

        double newBalance = balance;

        Account.getBalance(20000.00);

        System.out.println("Please enter the deposit amount:");

        // ensure deposit is set to 0 before getting input
        deposit = 0.00;

        // get the deposit amount from input
        deposit = stdin.nextDouble();
        stdin.nextLine();

        newBalance = deposit(balance, deposit);

        System.out.println("Please enter the withdrawal amount:");

        // ensure withdrawal is set to 0 before getting input
        withdrawal = 0.00;

        // get the deposit amount from input
        withdrawal = stdin.nextDouble();
        stdin.nextLine();

        double newBalance2 = withdrawal(newBalance, withdrawal);
        double newBalance3 = getMonthlyInterest(newBalance2);
        double MonthlyInterest = newBalance3;

        print(id, newBalance2, MonthlyInterest, dateCreated);

        stdin.close();
    }

    public static void print(int id, double newBalance2, double MonthlyInterest, ZonedDateTime dateCreated2)
    {
            System.out.println("To verify: the Account ID is " + id + " with a balance of $" + newBalance2
                    + ",\nhas accrued $" + MonthlyInterest + " in interest, and was opened on " + dateCreated2 + ".");
    }
}

我相信问题主要在于这条路线

withdrawl(balance, withdrawl);
必须将方法的返回值分配给变量

double currentBalance = withdrawl(balance, withdrawl);
print(id, currentBalance, MonthlyInterestRate, dateCreated);

我花了很多时间自己重新学习这个。我没有意识到Java8有了新的java.time

这里有很多变化。 希望这有助于在代码中注释所有更改

import java.time.format.*;
import java.time.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class Account {

private static int id = 0;
private static double balance = 0;
private static double annualInterestRate = 0;
private static Date dateCreated;
private static double MonthlyInterestRate = annualInterestRate/12;


public Account()
{
    // empty constructor
}

/**
 * @param id
 * @param balance
 * @param annualInterestRate
 * @param dateCreated
 * regular constructors
 */

public Account(int id, double balance, double annualInterestRate, Date dateCreated) {
    super();
    Account.id = 0;
    Account.balance = 0;
    Account.annualInterestRate = 4.5;
    Account.dateCreated = dateCreated;
}

public int getId() {
    return id;
}

public void setId(int id) {
    Account.id = id;
}

public static double getBalance(double d) {
    return balance;
}

public void setBalance(double balance) {
    Account.balance = balance;
}

public double getAnnualInterestRate() {
    return annualInterestRate;
}

public void setAnnualInterestRate(double annualInterestRate) {
    Account.annualInterestRate = annualInterestRate;
}

public static LocalDate ConvertStringToDate(String dateNow) {
    try
{
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM d yyyy", Locale.ENGLISH);
    LocalDate date = LocalDate.parse(dateNow, formatter);
    System.out.println(date);
    return date;
}
 catch (DateTimeParseException e)
{
    System.out.println(e);
}
LocalDate date = null;
return date;



}



public static double getMonthlyInterestRate(double annualInterestRate2) {

    double temp = annualInterestRate2/12;

    MonthlyInterestRate = temp;

    return MonthlyInterestRate;

}

public double getMonthlyInterest() {

    double temp = 100/annualInterestRate;

    double temp2 = getBalance(0)/temp;

    double temp3 = balance + temp2;

    System.out.println(temp3);

    return temp3;

}

public static double deposit(double balance, double deposit) {

    double temp = balance + deposit;

    balance = temp;

    return balance;
}

public static double withdrawl(double balance, double withdrawl) {

    double temp = balance - withdrawl;

    balance = temp;

    return balance;
}

    public static void main(String[] args) throws ParseException {
        //create Scanner 1 time. no need to create over and over
        Scanner stdin = new Scanner(System.in);
        id = 1122;
        balance = 20000;
        MonthlyInterestRate = .375;

        System.out.println("SAMPLE: Account ID " + id + " with a balance of " + balance + " "
                + "and a monthly interest rate of " + MonthlyInterestRate + " opened on " +
                dateCreated + ".");

        System.out.println("Please enter the ID number:");

        id = stdin.nextInt();
        // consume the /n
        stdin.nextLine();

        System.out.println("Typically, the original balance will be $20,000.00.\nPlease enter the balance:");



        balance = stdin.nextInt();
        // consume the /n
        stdin.nextLine();

        double newBalance = balance;

        Account.getBalance(20000.00);

        System.out.println("Please enter the date created like: MM d yyyy");


        // assign your string to your scanned object
        String dateNow = stdin.nextLine();
        //catch your returned date
        LocalDate dateCreated = ConvertStringToDate(dateNow);

        System.out.println("Please enter the deposit amount:");



        double deposit = 0.00;

        deposit = stdin.nextDouble();
        // consume the /n
        stdin.nextLine();

        newBalance = deposit(balance, deposit);

        System.out.println("Please enter the withdrawl amount:");



        double withdrawl = 0.00;

        withdrawl = stdin.nextDouble();
        // consume the /n
        stdin.nextLine();

        double newBalance2 = withdrawl(newBalance, withdrawl);

        double annualInterestRate2 = 4.5;

        double MonthlyInterestRate2 = getMonthlyInterestRate(annualInterestRate2);

        // pass your local date
        print(id, newBalance2, MonthlyInterestRate2, dateCreated);
    }

    public static void print(int id, double balance, double MonthlyInterestRate2, LocalDate dateCreated)
    {
            System.out.println("To verify: the Account ID is " + id + " with a balance of $" + balance + " "
                    + "and a monthly interest rate of " + MonthlyInterestRate2 + "% opened on " +
                    dateCreated + ".");
    }
 }

我从来没有弄清楚如何设置格式以获得教授想要的日期,但我得到了100%,所以我想最终这没什么大不了的。多亏了耶利米·斯蒂林斯,我感谢所有的帮助。这就是我的结局:

 package accountproject;

 // two imports needed for date and time
 import java.time.format.*;
 import java.time.*;
 // import standard exception error text
 import java.text.ParseException;
 // import EVERYTHING!
 import java.util.*;

 public class Account {

private static int id = 0;
private static double balance = 0;
private static double annualInterestRate = 0;
private static ZonedDateTime dateCreated;
private static double MonthlyInterestRate = annualInterestRate/12;

public Account()
{
    // empty constructor
}

/**
 * @param id
 * @param balance
 * @param annualInterestRate
 * @param dateCreated
 * regular constructors
 */

public Account(int id, double balance, double annualInterestRate, ZonedDateTime dateCreated) {
    super();
    Account.id = 0;
    Account.balance = 0;
    Account.annualInterestRate = 4.5;
}

public int getId() {
    return id;
}

public void setId(int id) {
    Account.id = id;
}

public static double getBalance(double d) {
    return balance;
}

public void setBalance(double balance) {
    Account.balance = balance;
}

public double getAnnualInterestRate() {
    return annualInterestRate;
}

public void setAnnualInterestRate(double annualInterestRate) {
    Account.annualInterestRate = annualInterestRate;
}

public static ZonedDateTime ConvertStringToDate(String dateNow) {
    try
{
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("E MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
    ZonedDateTime date = ZonedDateTime.parse(dateNow, formatter);

    return date;
}
 catch (DateTimeParseException e)
{
    System.out.println(e);
}
    ZonedDateTime date = null;
    return date;
}

public static double getMonthlyInterestRate(double annualInterestRate2) {

    double temp = annualInterestRate2/12;

    MonthlyInterestRate = temp;

    return MonthlyInterestRate;
}

public static double getMonthlyInterest(double newBalance2) {

    double temp = 100/MonthlyInterestRate;

    double temp2 = newBalance2/temp;

    double temp3 = newBalance2 + temp2;

    newBalance2 = temp3;

    return temp2;
}

public static double deposit(double balance, double deposit) {

    double temp = balance + deposit;

    balance = temp;

    return balance;
}

public static double withdrawal(double balance, double withdrawal) {

    double temp = balance - withdrawal;

    balance = temp;

    return balance;
}

    public static void main(String[] args) throws ParseException {

        // establish a scanner and set example values
        Scanner stdin = new Scanner(System.in);
        id = 1122;
        balance = 20000;
        MonthlyInterestRate = .375;
        double withdrawal = 2500;
        double deposit = 3000;

        double balanceExp = deposit(balance,deposit);
        balanceExp = withdrawal(balanceExp,withdrawal);
        double balanceExp2 = getMonthlyInterest(balanceExp);
        double monthlyInterest = balanceExp2;

        String dateExp = "Fri Oct 06 16:10:59 GMT 2017";
        dateCreated = ConvertStringToDate(dateExp);

        System.out.println("SAMPLE: Account ID " + id + " with a balance of $" + balanceExp
                 + ",\nhas accrued $" + monthlyInterest + " in interest and was opened on " 
                + dateCreated + ".");

        System.out.println("Please enter the ID number:");

        // get the id number input
        id = stdin.nextInt();
        stdin.nextLine();

        System.out.println("Typically, the original balance will be $20,000.00.\nPlease enter the balance:");

        // get the starting balance input
        balance = stdin.nextInt();
        stdin.nextLine();

        double newBalance = balance;

        Account.getBalance(20000.00);

        System.out.println("Please enter the deposit amount:");

        // ensure deposit is set to 0 before getting input
        deposit = 0.00;

        // get the deposit amount from input
        deposit = stdin.nextDouble();
        stdin.nextLine();

        newBalance = deposit(balance, deposit);

        System.out.println("Please enter the withdrawal amount:");

        // ensure withdrawal is set to 0 before getting input
        withdrawal = 0.00;

        // get the deposit amount from input
        withdrawal = stdin.nextDouble();
        stdin.nextLine();

        double newBalance2 = withdrawal(newBalance, withdrawal);
        double newBalance3 = getMonthlyInterest(newBalance2);
        double MonthlyInterest = newBalance3;

        print(id, newBalance2, MonthlyInterest, dateCreated);

        stdin.close();
    }

    public static void print(int id, double newBalance2, double MonthlyInterest, ZonedDateTime dateCreated2)
    {
            System.out.println("To verify: the Account ID is " + id + " with a balance of $" + newBalance2
                    + ",\nhas accrued $" + MonthlyInterest + " in interest, and was opened on " + dateCreated2 + ".");
    }
 }

非常感谢你,我知道这很简单,我就是想不起来。这是可行的。这其中哪部分是硬代码,哪部分是特定于程序的?很明显,int和string是,但是第一行我不确定哪些是变量,哪些是语法。我的get方法是getDateCreated,变量是dateCreated2=new dateCreated?我目前无法访问我的代码。我实际上错过了阅读问题。我以为你想把一个日期数据类型转换成它的字符串。太棒了,谢谢你。我知道我把它弄得太乱了,你使用的是现在遗留下来的麻烦的旧日期时间类,被java.time类取代了。避免
日期
日历
;使用
Instant
zoneDateTime
。另外,不要在货币事务中使用浮点类型;使用
BigDecimal
。最后,在发布前彻底搜索堆栈溢出。所有基本的日期时间问题都已经问过并回答过了。谢谢你的指点。我问这些问题的原因是我很难找到对我有意义的答案。我花了一年多的时间在这个网站上搜索各种编码问题的答案,因为我所有的教授都教授代码背后的原理,而不是代码本身。这对我来说是一个痛苦的话题,因为我可以理解英文甚至伪代码的想法,但他们都把代码本身当作政府机密。如果你不想回答这个问题,那么就不要回答,没有理由粗鲁。真正的问题是层叠搜索没有过滤旧版本的问题,新用户得到的答案不再有效或被贬低。