繁体   English   中英

如何用Java中的BigDecimal,String和LocalDate处理这些问题?

[英]How to deal with these issues with BigDecimal, String and LocalDate in Java?

我目前正在关注以下异常。 问题出在Price类的构造函数中。 我该如何处理这个问题。 我应该能够将价格2.29和日期10202017作为字符串传递

Price price = new Price("2.29", "10/20/2017");

但是Price类应该有

private BigDecimal price;
private LocalDate effectiveDate;

我对转换并不满意。 谁能告诉我如何实现这一目标并指导我。

理想的输出:

产品编号:1产品:土耳其三明治类别:杂货UPC:1001价格:2.29

错误

Exception in thread "main" java.time.format.DateTimeParseException: Text '10/20/2017' could not be parsed at index 0
    at java.time.format.DateTimeFormatter.parseResolved0(Unknown Source)
    at java.time.format.DateTimeFormatter.parse(Unknown Source)
    at java.time.LocalDate.parse(Unknown Source)
    at java.time.LocalDate.parse(Unknown Source)
    at posPD.Price.<init>(Price.java:35)
    at posTest.MainTest.main(MainTest.java:27)

主班

package posTest;

import java.math.BigDecimal;

import posPD.*;

public class MainTest {


    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Store myStore = new Store("1", "My Store 1");

        TaxCategory taxCategory1 = new TaxCategory("Beverages");
        TaxCategory taxCategory2 = new TaxCategory("Grocery");

        Register register1 = new Register("Register 1");
        Register register2 = new Register("Register 2");

        Person person1 = new Person("David", "OK" ,"405000000",  "800-800-1000");
        Person person2 = new Person("Sally", "Ktm", "123456789", "000-000-0000");


        Item item = new Item("1", "Turkey Sandwich");


        Price price = new Price("2.029", "10/20/2017");

        UPC upc = new UPC("1001");
        //Price price = new Price("1.49", "2005");

        //Session session = new Session();
        try {


        //CashDrawer cashDraw1 = new CashDrawer(1, BigDecimal.valueOf(500));

        //System.out.println(cashDraw1);
        //System.out.println(register.toString());

        Cashier cashier1 = new Cashier("1", person1 , "Password1");
        //person1.setCashier(cashier1);
        //myStore.addCashier(cashier);
        Cashier cashier2 = new Cashier("1", person2 , "Password1");
        person1.setCashier(cashier1);
        person2.setCashier(cashier2);

        myStore.addCashier(cashier1);

        myStore.addCashier(cashier2);




        //CashDrawer cashDrawer1 = new CashDrawer("Drawer 1.");
        CashDrawer cashDrawer1 = new CashDrawer(1, BigDecimal.valueOf(500));
        CashDrawer cashDrawer2 = new CashDrawer(2, BigDecimal.valueOf(500));

        myStore.addRegister(register1);
        myStore.addRegister(register2);
        register1.setCashDrawer(cashDrawer1);
        register2.setCashDrawer(cashDrawer2);

        //myStore.addTaxCategory(taxCategory1);
        //myStore.addTaxCategory(taxCategory2);

        Session session1 = new Session(cashier1, register1);
        Session session2 = new Session(cashier2, register2);

        myStore.addSession(session1);
        myStore.addSession(session2);

        myStore.addItem(item);
        //myStore.addUPC(upc);
        item.addUPC(upc);

        //item.addPrice(price);
        item.setTaxCategory(taxCategory2);




        //myStore.addCashier(cashier2);

        SaleLineItem sli = new SaleLineItem();

        System.out.println("=========");
        System.out.println( " "+myStore);
        System.out.println("=========");



        } catch(Exception e) {System.out.println("Exception exists");}

    }

}

价格等级

package posPD;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * Price details
 */
public class Price {

    private BigDecimal price;
    private LocalDate effectiveDate;
    private Item item;

    public Price() {
        // TODO - implement Price.Price
        //throw new UnsupportedOperationException();

    }

    /**
     * 
     * @param price
     * @param effectiveDate
     */
    public Price(String price, String effectiveDate) {
        // TODO - implement Price.Price
        //throw new UnsupportedOperationException();
        this();
        BigDecimal bdprice = new BigDecimal (price);
        this.price = bdprice;

        LocalDate dt = LocalDate.parse(effectiveDate);
        this.setEffectiveDate(dt);

    }

    /**
     * 
     * @param date
     */
    public Boolean isEffective(LocalDate date) {
        // TODO - implement Price.isEffective


        throw new UnsupportedOperationException();
        /*
        if (LocalDate.now().isAfter(date)) {
            return false;
        }
        return true;
        */

    }

    /**
     * 
     * @param quantity
     */
    public BigDecimal calcAmountForQty(int quantity) {
        // TODO - implement Price.calcAmountForQty
        //throw new UnsupportedOperationException();

        return price;
    }

    /**
     * 
     * @param date
     */
    /*
    public Boolean isPriceEffectiveForDate(LocalDate date) {
        // TODO - implement Price.isPriceEffectiveForDate
        throw new UnsupportedOperationException();
    }
    */

    /**
     * 
     * @param price
     */
    public void compareTo(Price price) {
        // TODO - implement Price.compareTo
        throw new UnsupportedOperationException();

    }

    public String toString() {
        // TODO - implement Price.toString
        throw new UnsupportedOperationException();
        //return 
    }

    public Item getItem() {
        return item;
    }

    public void setItem(Item item) {
        this.item = item;
    }

    public LocalDate getEffectiveDate() {
        return effectiveDate;
    }

    public void setEffectiveDate(LocalDate effectiveDate) {
        this.effectiveDate = effectiveDate;
    }

}

您使用的LocalDate.parse的一个参数版本要求使用此格式的CharSequence(“ yyyy-MM-dd”)。 您可以用这种方式格式化日期,例如

Price price = new Price("2.29", "2017-10-20");

相反,可能更可取的是使用像这样的DateTimeFormatter支持您使用的String格式(“ 10/20/2017”):

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
    LocalDate dt = LocalDate.parse(effectiveDate, formatter);

[根据您的评论进行编辑以添加测试示例]:

    String input1 = "2017-10-20";
    LocalDate date1 = LocalDate.parse(input1);
    System.out.println("Using no formatter    input1["+input1+"]  date1 ["+date1+"]");

    String input2 = "10/20/2017";
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
    LocalDate date2 = LocalDate.parse(input2, formatter);
    System.out.println("Formatter MM/dd/yyyy  input2["+input2+"]  date2 ["+date2+"]");

对我来说,这输出以下内容,没有错误:

Using no formatter    input1[2017-10-20]  date1 [2017-10-20]
Formatter MM/dd/yyyy  input2[10/20/2017]  date2 [2017-10-20]
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MM/dd/yyyy");
LocalDate dt = LocalDate.parse(effectiveDate, dtf);

看起来异常,你正在-

Exception in thread "main" java.time.format.DateTimeParseException: Text '10/20/2017' could not be parsed at index 0.

您以MM / dd / yyyy格式提供日期,但是在LocalDate类中无法指定格式,因此请更改代码,例如

DateTimeFormatter format = DateTimeFormatter.ofPattern("MM/dd/yyyy");
LocalDate date = LocalDate.parse(effectiveDate, format);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM