手动添加日期到目前为止?

时间:2015-11-05 04:13:04

标签: java date methods

输出的例子应该是这样的:
输入日期:1
输入月份:1
输入年份:2012年 输入已添加日期:61
假设二月是(每年29天)
然后输出:2012年3月1日;
但相反,我得到了2012年3月0日 我的代码:

import java.util.Scanner;
public class NO3 {

    public static void main(String[] args) 
    {
        Scanner sc = new Scanner(System.in);

        System.out.print("Input days : ");
        int days = sc.nextInt();

        System.out.print("Input month : ");
        int month = sc.nextInt();

        System.out.print("Input year : ");
        int year = sc.nextInt();

        System.out.print("Input added days : ");
        int addeddays = sc.nextInt();



        System.out.print("Output : ");
        System.out.print(add(days,month,year,addeddays));
    }

    public static int add(int days,int month,int year,int addeddays)
    {
        int countermonth=0;


        if(month==1)
        {
            if(days+addeddays>=31)
            {
                countermonth = (days+addeddays)/31;
                month += countermonth;
                days = days+addeddays - (countermonth*31);
                System.out.print(days);
                System.out.print("-");
            }
        }
        else if(month==2)
        {
            if(days+addeddays>=29)
            {
                countermonth = (days+addeddays)/29;
                month += countermonth;
                days = days+addeddays - (countermonth*29);
                System.out.print(days);
                System.out.print("-");
            }
        }
        else if(month==3)
        {
            if(days+addeddays>=31)
            {
                countermonth = (days+addeddays)/31;
                month += countermonth;
                days = days+addeddays - (countermonth*31);
                System.out.print(days);
                System.out.print("-");
            }
        }
        else if(month==4)
        {
            if(days+addeddays>=30)
            {
                countermonth = (days+addeddays)/30;
                month += countermonth;
                days = days+addeddays - (countermonth*30);
                System.out.print(days);
                System.out.print("-");
            }
        }

        else if(month==5)
        {
            if(days+addeddays>=31)
            {
                countermonth = (days+addeddays)/31;
                month += countermonth;
                days = days+addeddays - (countermonth*31);
                System.out.print(days);
                System.out.print("-");
            }
        }

        else if(month==6)
        {
            if(days+addeddays>=30)
            {
                countermonth = (days+addeddays)/30;
                month += countermonth;
                days = days+addeddays - (countermonth*30);
                System.out.print(days);
                System.out.print("-");
            }
        }

        else if(month==7)
        {
            if(days+addeddays>=31)
            {
                countermonth = (days+addeddays)/31;
                month += countermonth;
                days = days+addeddays - (countermonth*31);
                System.out.print(days);
                System.out.print("-");
            }
        }

        else if(month==8)
        {
            if(days+addeddays>=31)
            {
                countermonth = (days+addeddays)/31;
                month += countermonth;
                days = days+addeddays - (countermonth*31);
                System.out.print(days);
                System.out.print("-");
            }
        }

        else if(month==9)
        {
            if(days+addeddays>=30)
            {
                countermonth = (days+addeddays)/30;
                month += countermonth;
                days = days+addeddays - (countermonth*30);
                System.out.print(days);
                System.out.print("-");
            }
        }

        else if(month==10)
        {
            if(days+addeddays>=31)
            {
                countermonth = (days+addeddays)/31;
                month += countermonth;
                days = days+addeddays - (countermonth*31);
                System.out.print(days);
                System.out.print("-");
            }
        }

        else if(month==11)
        {
            if(days+addeddays>=30)
            {
                countermonth = (days+addeddays)/30;
                month += countermonth;
                days = days+addeddays - (countermonth*30);
                System.out.print(days);
                System.out.print("-");
            }
        }

        else if(month==12)
        {
            if(days+addeddays>=31)
            {
                countermonth = (days+addeddays)/31;
                month += countermonth;
                days = days+addeddays - (countermonth*31);
                System.out.print(days);
                System.out.print("-");
            }
        }

        if(month>12)
                {
                   int counteryear = month/12;
                   month = month - (counteryear*12);
                   year +=counteryear;
                }

                if(month==1)
                {
                    System.out.print("January");
                }
                else if(month==2)
                {
                    System.out.print("February");
                }
                else if(month==3)
                {
                    System.out.print("March");
                }
                else if(month==4)
                {
                    System.out.print("April");
                }
                 else if(month==5)
                {
                    System.out.print("May");
                }
                else if(month==6)
                {
                    System.out.print("June");
                }
                else if(month==7)
                {
                    System.out.print("July");
                }
                else if(month==8)
                {
                    System.out.print("August");
                }
                else if(month==9)
                {
                    System.out.print("September");
                }
                else if(month==10)
                {
                    System.out.print("October");
                }
                else if(month==11)
                {
                    System.out.print("November");
                }
                else if(month==12)
                {
                    System.out.print("December");
                }
                System.out.print("-");
                //System.out.println("");

        return year;
    }

}

import java.util.Scanner; public class NO3 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Input days : "); int days = sc.nextInt(); System.out.print("Input month : "); int month = sc.nextInt(); System.out.print("Input year : "); int year = sc.nextInt(); System.out.print("Input added days : "); int addeddays = sc.nextInt(); System.out.print("Output : "); System.out.print(add(days,month,year,addeddays)); } public static int add(int days,int month,int year,int addeddays) { int countermonth=0; if(month==1) { if(days+addeddays>=31) { countermonth = (days+addeddays)/31; month += countermonth; days = days+addeddays - (countermonth*31); System.out.print(days); System.out.print("-"); } } else if(month==2) { if(days+addeddays>=29) { countermonth = (days+addeddays)/29; month += countermonth; days = days+addeddays - (countermonth*29); System.out.print(days); System.out.print("-"); } } else if(month==3) { if(days+addeddays>=31) { countermonth = (days+addeddays)/31; month += countermonth; days = days+addeddays - (countermonth*31); System.out.print(days); System.out.print("-"); } } else if(month==4) { if(days+addeddays>=30) { countermonth = (days+addeddays)/30; month += countermonth; days = days+addeddays - (countermonth*30); System.out.print(days); System.out.print("-"); } } else if(month==5) { if(days+addeddays>=31) { countermonth = (days+addeddays)/31; month += countermonth; days = days+addeddays - (countermonth*31); System.out.print(days); System.out.print("-"); } } else if(month==6) { if(days+addeddays>=30) { countermonth = (days+addeddays)/30; month += countermonth; days = days+addeddays - (countermonth*30); System.out.print(days); System.out.print("-"); } } else if(month==7) { if(days+addeddays>=31) { countermonth = (days+addeddays)/31; month += countermonth; days = days+addeddays - (countermonth*31); System.out.print(days); System.out.print("-"); } } else if(month==8) { if(days+addeddays>=31) { countermonth = (days+addeddays)/31; month += countermonth; days = days+addeddays - (countermonth*31); System.out.print(days); System.out.print("-"); } } else if(month==9) { if(days+addeddays>=30) { countermonth = (days+addeddays)/30; month += countermonth; days = days+addeddays - (countermonth*30); System.out.print(days); System.out.print("-"); } } else if(month==10) { if(days+addeddays>=31) { countermonth = (days+addeddays)/31; month += countermonth; days = days+addeddays - (countermonth*31); System.out.print(days); System.out.print("-"); } } else if(month==11) { if(days+addeddays>=30) { countermonth = (days+addeddays)/30; month += countermonth; days = days+addeddays - (countermonth*30); System.out.print(days); System.out.print("-"); } } else if(month==12) { if(days+addeddays>=31) { countermonth = (days+addeddays)/31; month += countermonth; days = days+addeddays - (countermonth*31); System.out.print(days); System.out.print("-"); } } if(month>12) { int counteryear = month/12; month = month - (counteryear*12); year +=counteryear; } if(month==1) { System.out.print("January"); } else if(month==2) { System.out.print("February"); } else if(month==3) { System.out.print("March"); } else if(month==4) { System.out.print("April"); } else if(month==5) { System.out.print("May"); } else if(month==6) { System.out.print("June"); } else if(month==7) { System.out.print("July"); } else if(month==8) { System.out.print("August"); } else if(month==9) { System.out.print("September"); } else if(month==10) { System.out.print("October"); } else if(month==11) { System.out.print("November"); } else if(month==12) { System.out.print("December"); } System.out.print("-"); //System.out.println(""); return year; } } 我正在学习java所以我知道它的效率非常低,所以任何人都可以告诉我或教我正确而有效的解决方案吗? (这个问题要求仅使用方法,没有类似日期的java类)提前感谢。

3 个答案:

答案 0 :(得分:0)

您可以使用Calendar执行日期算术。像,

public static Date add(int days, int month, int year, int addeddays) {
    Calendar c = Calendar.getInstance();
    c.set(Calendar.YEAR, year);
    c.set(Calendar.MONTH, month - 1);
    c.set(Calendar.DAY_OF_MONTH, days);
    c.add(Calendar.DAY_OF_MONTH, addeddays);
    return c.getTime();
}

然后您可以使用SimpleDateFormat对其进行格式化。像,

System.out.print("Output : ");
Date d = add(days, month, year, addeddays);
DateFormat sdf = new SimpleDateFormat("d-MMMM-yyyy");
System.out.println(sdf.format(d));

当然,根据您提供的输入,您将获得

Output : 2-March-2012

因为1月份有31天,2月份有29天(总计 60天)。因此在3月2日增加了61天。

答案 1 :(得分:0)

是的,有更有效的方法可以做到这一点,java有日期操作类。只需看看java.util.CalendarJava.Date。 因此,一种替代方案可以是: 使用输入

创建Date对象
String input = "05.05.2015";
int addedDays = "61;
SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
Calendar calendarFrom = Calendar.getInstance();

calendarFrom.setTime(dateFormat.parse(s1));
calendarFrom.add(Calendar.DATE, addedDays);

你在那里!

答案 2 :(得分:0)

只是为了让您知道这个问题:

countermonth = (days+addeddays)/31;
            month += countermonth;
            days = days+addeddays - (countermonth*31);

天变为零,因为它就像(countermonth*31=days+addeddays). 希望能帮助到你。