当前位置: 首页 > 知识库问答 >
问题:

如何打印给定日期月年的日期?

嵇俊德
2023-03-14

在这我得到飞跃

import java.util.Scanner;

class day {

    public static void main(String arg[]) {
        int tm, sm, w;
        int y;
        int[] m = new int[12];
        m[0] = 31;
        m[1] = 28;
        m[2] = 31;
        m[3] = 30;
        m[4] = 31;
        m[5] = 30;
        m[6] = 31;
        m[7] = 31;
        m[8] = 30;
        m[9] = 31;
        m[10] = 30;
        m[11] = 31;
        //{31,28,31,30,31,30,31,31,30,31,30,31};
        String[] mo = new String[12];
        mo[0] = "January";
        mo[1] = "February";
        mo[2] = "March";
        mo[3] = "April";
        mo[4] = "May";
        mo[5] = "June";
        mo[6] = "July";
        mo[7] = "August";
        mo[8] = "September";
        mo[9] = "October";
        mo[10] = "November";
        mo[11] = "December";
        String[] we = new String[w];
        we[0] = "Sunday";
        we[1] = "Monday";
        we[2] = "Tuesday";
        we[3] = "Wednesday";
        we[4] = "Thursday";
        we[5] = "Friday";
        we[6] = "Saturday";
        Scanner ip = new Scanner(System.in);
        System.out.print("\nEnter year ");
        y = ip.nextInt();
        System.out.print("\nEnter month ");
        sm = ip.nextInt();
        if (y % 4 == 0) {
            m[1] = m[1] + 1;
            for (tm = 1; tm <= m.length; tm++) {
                if (tm == sm) {
                    System.out.print("\n" + y + " is a Leap Year\n"
                            + mo[sm - 1] + " month " + "has " + m[sm - 1] + " days\n");
                }
            }
        } else {
            for (tm = 1; tm <= 12; tm++) {
                if (tm == sm) {
                    System.out.print("\n" + y + " is an Ordinary year\n"
                            + mo[sm - 1] + " month " + "has " + m[sm - 1] + " days\n");
                }
            }
        }
    }
}

共有3个答案

孙昂然
2023-03-14

根据之前的信息:

public class JavaApplication1 {

    public static void main(String[] args) {
        int y, sm, sd;
        int[] m = new int[12];
        m[0] = 31;
        m[1] = 28;
        m[2] = 31;
        m[3] = 30;
        m[4] = 31;
        m[5] = 30;
        m[6] = 31;
        m[7] = 31;
        m[8] = 30;
        m[9] = 31;
        m[10] = 30;
        m[11] = 31;
        //{31,28,31,30,31,30,31,31,30,31,30,31};

        String[] mo = new String[12];
        mo[0] = "January";
        mo[1] = "February";
        mo[2] = "March";
        mo[3] = "April";
        mo[4] = "May";
        mo[5] = "June";
        mo[6] = "July";
        mo[7] = "August";
        mo[8] = "September";
        mo[9] = "October";
        mo[10] = "November";
        mo[11] = "December";

        String[] we = new String[7];
        we[0] = "Sunday";
        we[1] = "Monday";
        we[2] = "Tuesday";
        we[3] = "Wednesday";
        we[4] = "Thursday";
        we[5] = "Friday";
        we[6] = "Saturday";

        Scanner ip = new Scanner(System.in);
        System.out.print("\nEnter year ");
        y = ip.nextInt();
        System.out.print("\nEnter month ");
        sm = ip.nextInt();
        System.out.print("\nEnter day ");
        sd = ip.nextInt();

        // Using the method Evgeniy Dorofeev kindly shared with us:
        if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0) {
            m[1] = m[1] + 1;
            System.out.print(y + " is a Leap Year. ");
        } else {
            System.out.print(y + " is an Ordinary year. ");
        }

        System.out.println(mo[sm - 1] + " month " + "has " + m[sm - 1] + " days");

        int dow = dayOfWeek(y, sm, sd);
        System.out.println("Day " + sd + " is a " + we[dow]);
    }

    //https://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week#Implementation-dependent_methods_of_Sakamoto.2C_Lachman.2C_Keith_and_Craver
    public static int dayOfWeek(int y, int m, int d) {
        int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
        y -= (m < 3 ? 1 : 0);
        return (y + y / 4 - y / 100 + y / 400 + t[m - 1] + d) % 7;
        // 0 is Sunday, 1 is monday, ...
    }
}
帅煌
2023-03-14

为什么不使用java呢。util。日期和java。util。Gregoriandar课程?然后,您只需从控制台读取输入,并将其解析为日历对象。

import java.io.Console;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
// ... other stuff
Console c = System.console();
Date date;
// Note: Console is not working from Netbeans, you have to run it through a real shell.
String dateinput = c.readLine("Phlease, enter the date (as year-month-day): ");
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
try
{
    date = simpleDateFormat.parse(dateinput);
}
catch (ParseException ex)
{
    System.out.println("Oops, problem! Exception: " + ex);
}

// Now, you have the input date as a Date object.
// Checking if it is a leap year:
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(date);
// Now, the calendar object has the date. 
// From now on, we use this calendar to extract date/time informations:
if (gc.isLeapYear(gc.get(Calendar.YEAR))) {
    System.out.println("Yeah, this year is a leap year.");
} else {
    System.out.println("No, it is a normal year.");
}
// DOW returns an int, that is encoded in the following constants:
switch(gc.get(Calendar.DAY_OF_WEEK)) {
    case Calendar.MONDAY: System.out.println("Monday!"); break;
    case Calendar.TUESDAY: System.out.println("Thuesday!"); break;
    case Calendar.WEDNESDAY: System.out.println("Wednesday!"); break;
    case Calendar.THURSDAY: System.out.println("Thor's day!"); break;
    case Calendar.FRIDAY: System.out.println("Friday!"); break;

    case Calendar.SATURDAY: System.out.println("Saturday!"); break;
    case Calendar.SUNDAY: System.out.println("Sunday!"); break;
}

闰年可以被4除,除了它也可以被100除,除了(!)也可以除以400。因此,1800年不是闰年,因为它可以除以4和100,但不能除以400。2000年是闰年,因为它可以被400整除。

容俊豪
2023-03-14

这是如何为公历确定一个闰年

if ((y % 4 == 0  && y % 100 !=0) || y % 400 == 0) {
  ...

看见http://en.wikipedia.org/wiki/Leap_year

 类似资料:
  • 问题内容: 我正在处理Java作业,它涉及在用户指定月份和年份之后打印日历。我不能使用Calendar或GregorianCalendar类。我的问题是日历在星期六的第一天无法正确打印月份。我已经看了大约一个小时的代码,但不确定出什么问题。我正在使用Zeller的Congruence查找以“ h”表示的每月的第一天。 例如,2008年3月的日历(错误地)如下所示: 这是我的代码: 问题答案: 从星

  • 我正在做一个Java作业,它涉及在用户指定月份和年份后打印日历。我不能使用日历或日历类。我的问题是日历不能正确地打印月份的第一天是星期六。我已经看了我的代码大约一个小时了,我不确定哪里出了问题。我用Zeller的同余来计算月的第一天,它由“h”表示。 例如,2008年3月的日历(错误)如下所示: 这是我的代码:

  • 我需要一个解决方案来解决返回日(周一、周二…),当我输入诸如年,月,日期。

  • 如何打印以下格式的当前日期? 年/月/日 我一直在使用下面的,但它增加了一个额外的空间。我需要它看起来像‘2020/9/14’,而不是‘2020/9/14’。有什么想法吗?下面是后一个选项的当前代码。

  • 问题内容: 我在String上有个约会,像这样: 我想将其划分为: 做这个的最好方式是什么? 问题答案: 在Java中拆分字符串的一种方法是使用.split(“ regex”),该方法根据提供的模式拆分字符串,并返回String数组。 获取当前日期:您还可以通过更改传递给SimpleDateFormat的值来更改日期格式。不要忘记进口。

  • 当我使用以下代码时,Object错误。 谁能告诉我如何从年、月和日的值中获取Date对象?