小编Saa*_*san的帖子

Calender.set(Calender.Month, ?) 如何工作?

我正在编写一种方法,可以将日期提前给定的周数。这是我的代码:

public class Date {
int year;
int month;
int day;

public Date (int year, int month, int day){
    this.year = year;
    this.month = month;
    this.day = day;

}
    public void addWeeks (int weeks){
    int week = weeks * 7;
    DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.DAY_OF_MONTH, this.day);
    calendar.set(Calendar.MONTH, this.month);
    calendar.set(Calendar.YEAR, this.year);
    calendar.add(Calendar.DAY_OF_MONTH, week);
    System.out.println();
    System.out.println("Date after adding " + weeks + " weeks is: " + dateFormat.format(calendar.getTime()));
}
Run Code Online (Sandbox Code Playgroud)

因此,如果我将今天的日期传递给年、月和日。(03/08/2019) 然后调用 addWeeks(1) 例如,那么日期应该提前为 (03/15/2019) 但它给了我 (04/15/2019)。由于某种原因,月份总是比我输入的月份多 1。如果我输入月份 …

java date java.util.calendar

3
推荐指数
1
解决办法
7991
查看次数

为什么我的总价格应为11时打印为0.0

我是一名新的java程序员.我正在写一个关于餐厅菜单的程序,但我的价格没有正确计算.每次给它0.0,应该是11.0

public class Main {

    public static double priceBreadrollType;
    public static double priceMeat;
    public static double totalPrice;

    public static void main(String[] args) {
        setTotalPrice();
    }

    public static void priceBread (){
        priceBreadrollType = 1;
    }

    public static void priceMeat(){
        priceMeat = 10;
    }

    public static void setTotalPrice(){
        totalPrice = priceBreadrollType + priceMeat;
        System.out.println("The total prize " + totalPrice);
    }



}
Run Code Online (Sandbox Code Playgroud)

java

-4
推荐指数
1
解决办法
82
查看次数

标签 统计

java ×2

date ×1

java.util.calendar ×1