如何打印日期的日期而不使用内置日历日期日期包,java中的库

时间:2014-01-10 05:30:39

标签: java

在这个程序中,我想打印日期的日期,而不使用内置的日历包..而且我无法得到某些日期(输入日期)的确切答案。在这个我正确的日期正好只有31.12.0004。请注意,这是我的指南在5天之前给出的评估,但仍然没有得到正确的答案,我不应该使用日历包和api,因为课程的教学大纲已完成列表:基本数组,如果&否则,for循环,类和对象。

import java.util.Scanner;
class calendarr
{
public static void main(String arg[])
    {
int z=0;
do{
    int id,im,iy;
    Scanner ip = new Scanner(System.in);
    System.out.print("Enter DD MM YYYY : ");
    id = ip.nextInt();
    im = ip.nextInt();
    iy = ip.nextInt();
    System.out.print("\n");

    int totalDays = 0, leapYearDays = 366, ordinaryYearDays = 365, y = iy-1,day=0;
    int totalDaysOfGivenYear,totalDaysUptoGivenMonth=0, totalDaysEntire;
    int leapYearCount = y%4, ordinaryYearCount = y - leapYearCount;
    int totalDaysUptoPreviousYear=0;
    int totalDaysUptoPreviousYearCount = (leapYearCount * leapYearDays)+(ordinaryYearCount * ordinaryYearDays);
    System.out.print("Odd Days Leap Year and Ordinary Year : "+leapYearCount+" "+ordinaryYearCount+"\n");


    String[] months = new String[] {"January","February","March","April","May","June","July","August","September","October","November","December"};
    int[] monthDates = new int[] {0,31,28,31,30,31,30,31,31,30,31,30,31};
    String[] weekDays = new String[] {"Saturday","Sunday","Monday","Tuesday","Wednesday","Thursday","Friday"};


        if((iy%4==0)&&(iy%100!=0)||(iy%400==0))
            {
            monthDates[2] = 29;
            System.out.println("It is Leap Year");
            }
        else System.out.println("It is Ordinary Year");

    for(int i=1;i<=monthDates.length;i++)
        {


        totalDays = totalDays + monthDates[i-1];
        }
    for(int j=1;j<=im;j++)
        {
        totalDaysUptoGivenMonth = totalDaysUptoGivenMonth+monthDates[j];

        }
        totalDaysOfGivenYear = totalDaysUptoGivenMonth+id-monthDates[im];

        System.out.println("Total Days Upto Given Months : "+totalDaysOfGivenYear+"\nTotal Days of given year :  "+iy+" : "+totalDays);


        System.out.println("Total Count : "+totalDaysUptoPreviousYearCount);


        //totalDaysEntire = totalDaysUptoPreviousYearCount - totalDays+ totalDaysOfGivenYear;
        totalDaysEntire = totalDaysUptoPreviousYearCount+ totalDaysOfGivenYear-y;
        System.out.println("Overall Total Days : "+totalDaysEntire);


    /*//day = (365+366-totalDays+totalDaysUptoPreviousYearCount)%7;

        for(int k=0;k<=y;k++)
        {

        day = (totalDaysEntire%7);



        System.out.println(day+"\n"+weekDays[day]+"\n\n");
        }*/

    day = ((totalDaysUptoPreviousYearCount+ totalDaysOfGivenYear-iy)%7);

    System.out.println(day+"\n"+weekDays[day]+"\n\n");
}
while(z<5); 

    }
}

输出:输入DD MM YYYY:1 1 1 奇数天闰年和普通年份:0 0 这是普通的一年 给定月份的总天数:1 特定年份的总天数:1:365 总数:0 总天数:1 0 星期六

1 个答案:

答案 0 :(得分:1)

嘿,您的代码中需要一些修复程序,请检查此代码现在是否有效。

import java.util.Scanner;
public class calendarr
{
public static void main(String arg[])
    {
int z=0;
do{
    int id,im,iy;
    Scanner ip = new Scanner(System.in);
    System.out.print("Enter DD MM YYYY : ");
    id = ip.nextInt();
    im = ip.nextInt();
    iy = ip.nextInt();
    System.out.print("\n");

    int totalDays = 0, leapYearDays = 366, ordinaryYearDays = 365, y = iy-1,day=0;
    int totalDaysOfGivenYear,totalDaysUptoGivenMonth=0, totalDaysEntire;
    int leapYearCount = 0;
    //calculate the number of leap and non leap years
    for(int i=0;i<=y;i++){
        if((i%4==0)&&(i%100!=0)||(i%400==0))
            {
                leapYearCount++;
            }
    }
    int ordinaryYearCount = y - leapYearCount;
    int totalDaysUptoPreviousYear=0;
    int totalDaysUptoPreviousYearCount = (leapYearCount * leapYearDays)+(ordinaryYearCount * ordinaryYearDays);
    System.out.print("Odd Days Leap Year and Ordinary Year : "+leapYearCount+" "+ordinaryYearCount+"\n");


    String[] months = new String[] {"January","February","March","April","May","June","July","August","September","October","November","December"};
    int[] monthDates = new int[] {0,31,28,31,30,31,30,31,31,30,31,30,31};
    String[] weekDays = new String[] {"Saturday","Sunday","Monday","Tuesday","Wednesday","Thursday","Friday"};

 if((iy%4==0)&&(iy%100!=0)||(iy%400==0))
            {
            monthDates[2] = 29;
            System.out.println("It is Leap Year");
            }
        else System.out.println("It is Ordinary Year");

    for(int i=1;i<=monthDates.length;i++)
        {
        totalDays = totalDays + monthDates[i-1];
        }
    for(int j=1;j<im;j++)
        {
        totalDaysUptoGivenMonth = totalDaysUptoGivenMonth+monthDates[j];

        }
        totalDaysOfGivenYear = totalDaysUptoGivenMonth+id;

        System.out.println("Total Days Upto Given Months : "+totalDaysOfGivenYear+"\nTotal Days of given year :  "+iy+" : "+totalDays);


        System.out.println("Total Count : "+totalDaysUptoPreviousYearCount);


        //totalDaysEntire = totalDaysUptoPreviousYearCount - totalDays+ totalDaysOfGivenYear;
        totalDaysEntire = totalDaysUptoPreviousYearCount+ totalDaysOfGivenYear;
        System.out.println("Overall Total Days : "+totalDaysEntire);


    /*//day = (365+366-totalDays+totalDaysUptoPreviousYearCount)%7;

        for(int k=0;k<=y;k++)
        {

        day = (totalDaysEntire%7);



        System.out.println(day+"\n"+weekDays[day]+"\n\n");
        }*/

    day = (totalDaysEntire%7);

    System.out.println(day+"\n"+weekDays[day]+"\n\n");
}
while(z<5); 

    }
}

我改变了一些事情,将feb的日子摆脱了循环。检查年数和非闰年的计算。并且还对查找totalDaysOfGivenYeartotalDaysEntireday变量进行了更改。

输入:1 1 1998

<强>输出:

Odd Days Leap Year and Ordinary Year : 485 1512
It is Ordinary Year
Total Days Upto Given Months : 1
Total Days of given year :  1998 : 365
Total Count : 729390
Overall Total Days : 729391
5
Thursday