Java 计算一个月内没有日历的天数

Java 计算一个月内没有日历的天数,java,Java,我试图计算一个月的天数,给定一年和一个月的数字。这是我到目前为止所拥有的 public static int daysInMonth(int m, int y) { int d; if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) { d = 31; } else if (m == 4 || m == 6 || m == 9 || m

我试图计算一个月的天数,给定一年和一个月的数字。这是我到目前为止所拥有的

 public static int daysInMonth(int m, int y) {
    int d;
    if (m == 1 || m == 3 || m == 5 || m == 7 
             || m == 8 || m == 10 || m == 12) { 
      d = 31;
    } else if (m == 4 || m == 6 || m == 9 || m == 11) { 
      d = 30;
    } else if (y % 4 == 0 && m == 2) {  
      d = 29;
    } else if (m == 2) {
      d = 28;   
    } else {    
      d = 0;    
    }   
    return d;   
  }
不幸的是,它在闰年的第二个月不能正常工作


如果不使用
日历
类,我如何修复它?

我会将一个月的天数存储在一个整数数组中,第一个索引为空值,以减少条件

public static int daysInMonth(int m, int y) {
    int[] days = {0, 31, 28, 31, ... 31};
    if (m == 2 && y % 4 == 0) {
        return 29;

    } else {
        return days[m];
    }
}


您可能需要添加一些检查,以确保m参数始终小于13(月数加上开头的空值)

我将每月的天数存储在整数数组中,第一个索引为空值,以减少条件

public static int daysInMonth(int m, int y) {
    int[] days = {0, 31, 28, 31, ... 31};
    if (m == 2 && y % 4 == 0) {
        return 29;

    } else {
        return days[m];
    }
}


您可能需要添加一些检查,以确保m参数始终小于13(月数加上开头的空值)

我将每月的天数存储在整数数组中,第一个索引为空值,以减少条件

public static int daysInMonth(int m, int y) {
    int[] days = {0, 31, 28, 31, ... 31};
    if (m == 2 && y % 4 == 0) {
        return 29;

    } else {
        return days[m];
    }
}


您可能需要添加一些检查,以确保m参数始终小于13(月数加上开头的空值)

我将每月的天数存储在整数数组中,第一个索引为空值,以减少条件

public static int daysInMonth(int m, int y) {
    int[] days = {0, 31, 28, 31, ... 31};
    if (m == 2 && y % 4 == 0) {
        return 29;

    } else {
        return days[m];
    }
}

您可能需要添加一些检查,以确保m参数始终小于13(月数加上开头的空值)

对于22016的情况,输出结果与预期一致

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
    System.out.println(daysInMonth(2,2016));
}
public static int daysInMonth(int m, int y) {
int d;
if (m == 1 || m == 3 || m == 5 || m == 7 
         || m == 8 || m == 10 || m == 12) { 
  d = 31;
} else if (m == 4 || m == 6 || m == 9 || m == 11) { 
  d = 30;
} else if (y % 4 == 0 && m == 2) {  
  d = 29;
} else if (m == 2) {
  d = 28;   
} else {    
  d = 0;    
}   
return d;   
}
}

对于22016的情况,输出结果与预期一致

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
    System.out.println(daysInMonth(2,2016));
}
public static int daysInMonth(int m, int y) {
int d;
if (m == 1 || m == 3 || m == 5 || m == 7 
         || m == 8 || m == 10 || m == 12) { 
  d = 31;
} else if (m == 4 || m == 6 || m == 9 || m == 11) { 
  d = 30;
} else if (y % 4 == 0 && m == 2) {  
  d = 29;
} else if (m == 2) {
  d = 28;   
} else {    
  d = 0;    
}   
return d;   
}
}

对于22016的情况,输出结果与预期一致

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
    System.out.println(daysInMonth(2,2016));
}
public static int daysInMonth(int m, int y) {
int d;
if (m == 1 || m == 3 || m == 5 || m == 7 
         || m == 8 || m == 10 || m == 12) { 
  d = 31;
} else if (m == 4 || m == 6 || m == 9 || m == 11) { 
  d = 30;
} else if (y % 4 == 0 && m == 2) {  
  d = 29;
} else if (m == 2) {
  d = 28;   
} else {    
  d = 0;    
}   
return d;   
}
}

对于22016的情况,输出结果与预期一致

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
    System.out.println(daysInMonth(2,2016));
}
public static int daysInMonth(int m, int y) {
int d;
if (m == 1 || m == 3 || m == 5 || m == 7 
         || m == 8 || m == 10 || m == 12) { 
  d = 31;
} else if (m == 4 || m == 6 || m == 9 || m == 11) { 
  d = 30;
} else if (y % 4 == 0 && m == 2) {  
  d = 29;
} else if (m == 2) {
  d = 28;   
} else {    
  d = 0;    
}   
return d;   
}
}

您需要正确计算闰年:

public static int daysInMonth(int m, int y) {
    int d;
    if (m == 1 || m == 3 || m == 5 || m == 7
             || m == 8 || m == 10 || m == 12) {
      d = 31;
    } else if (m == 4 || m == 6 || m == 9 || m == 11) {
      d = 30;
    } else if (isLeapYear(y) && m == 2) {
      d = 29;
    } else if (m == 2) {
      d = 28;
    } else {
      d = 0;
    }
    return d;
}

public static boolean isLeapYear(int year) {
  if (year % 4 != 0) {
    return false;
  } else if (year % 400 == 0) {
    return true;
  } else if (year % 100 == 0) {
    return false;
  } else {
    return true;
  }
}

这是一个完整的、经过测试的解决方案,其中还包含一个

import java.util.HashSet;
导入java.util.array;
导入java.util.List;
类MyClass{
公共静态void main(字符串[]args){
List leapYears=Arrays.asList(1804, 1808, 1812, 1816, 1820, 1824, 1828, 1832, 1836, 1840, 1844, 1848, 1852, 1856, 1860, 1864, 1868, 1872, 1876, 1880, 1884, 1888, 1892, 1896, 1904, 1908, 1912, 1916, 1920, 1924, 1928, 1932, 1936, 1940, 1944, 1948, 1952, 1956, 1960, 1964, 1968, 1972, 1976, 1980, 1984, 1988, 1992, 1996, 2000, 2004, 2008, 2012, 2016, 2020, 2024, 2028, 2032, 2036, 2040, 2044, 2048, 2052, 2056, 2060, 2064, 2068, 2072, 2076, 2080, 2084, 2088, 2092, 2096, 2104, 2108, 2112, 2116, 2120, 2124, 2128, 2132, 2136, 2140, 2144, 2148, 2152, 2156, 2160, 2164, 2168, 2172, 2176, 2180, 2184, 2188, 2192, 2196, 2204, 2208, 2212, 2216, 2220, 2224, 2228, 2232, 2236, 2240, 2244, 2248, 2252, 2256, 2260, 2264, 2268, 2272, 2276, 2280, 2284, 2288, 2292, 2296, 2304, 2308, 2312, 2316, 2320, 2324, 2328, 2332, 2336, 2340, 2344, 2348, 2352, 2356, 2360, 2364, 2368, 2372, 2376, 2380, 2384, 2388, 2392, 2396, 2400);
对于(整数年=1804;年<2400;年++){
for(整数月:Arrays.asList(1,3,5,7,8,10,12)){
断言daysInMonth(月,年)==31;
}
for(整数月:Arrays.asList(4,6,9,11)){
断言daysInMonth(月,年)==30;
}
if(年数包含(年)){
断言daysInMonth(2,year)=29;
}
否则{
断言daysInMonth(2,year)=28;
}
}
}
公共静态int daysInMonth(int m,int y){
HashSet monthswith31天=新的HashSet(Arrays.asList(1,3,5,7,8,10,12));
HashSet monthsWith30Days=新的HashSet(Arrays.asList(4,6,9,11));
如果(每月31天,包含(m)){
返回31;
}否则,如果(每月30天,包含(m)){
返回30;
}
其他情况(年(y)){
返回29;
}
否则{
返回28;
}
}
公共静态布尔值isLeapYear(整数年){
如果(年份%4!=0){
返回false;
}否则如果(年份%400==0){
返回true;
}否则如果(年份%100==0){
返回false;
}否则{
返回true;
}
}
}

您需要正确计算闰年:

public static int daysInMonth(int m, int y) {
    int d;
    if (m == 1 || m == 3 || m == 5 || m == 7
             || m == 8 || m == 10 || m == 12) {
      d = 31;
    } else if (m == 4 || m == 6 || m == 9 || m == 11) {
      d = 30;
    } else if (isLeapYear(y) && m == 2) {
      d = 29;
    } else if (m == 2) {
      d = 28;
    } else {
      d = 0;
    }
    return d;
}

public static boolean isLeapYear(int year) {
  if (year % 4 != 0) {
    return false;
  } else if (year % 400 == 0) {
    return true;
  } else if (year % 100 == 0) {
    return false;
  } else {
    return true;
  }
}

这是一个完整的、经过测试的解决方案,其中还包含一个

import java.util.HashSet;
导入java.util.array;
导入java.util.List;
类MyClass{
公共静态void main(字符串[]args){
List leapYears=Arrays.asList(1804, 1808, 1812, 1816, 1820, 1824, 1828, 1832, 1836, 1840, 1844, 1848, 1852, 1856, 1860, 1864, 1868, 1872, 1876, 1880, 1884, 1888, 1892, 1896, 1904, 1908, 1912, 1916, 1920, 1924, 1928, 1932, 1936, 1940, 1944, 1948, 1952, 1956, 1960, 1964, 1968, 1972, 1976, 1980, 1984, 1988, 1992, 1996, 2000, 2004, 2008, 2012, 2016, 2020, 2024, 2028, 2032, 2036, 2040, 2044, 2048, 2052, 2056, 2060, 2064, 2068, 2072, 2076, 2080, 2084, 2088, 2092, 2096, 2104, 2108, 2112, 2116, 2120, 2124, 2128, 2132, 2136, 2140, 2144, 2148, 2152, 2156, 2160, 2164, 2168, 2172, 2176, 2180, 2184, 2188, 2192, 2196, 2204, 2208, 2212, 2216, 2220, 2224, 2228, 2232, 2236, 2240, 2244, 2248, 2252, 2256, 2260, 2264, 2268, 2272, 2276, 2280, 2284, 2288, 2292, 2296, 2304, 2308, 2312, 2316, 2320, 2324, 2328, 2332, 2336, 2340, 2344, 2348, 2352, 2356, 2360, 2364, 2368, 2372, 2376, 2380, 2384, 2388, 2392, 2396, 2400);
对于(整数年=1804;年<2400;年++){
for(整数月:Arrays.asList(1,3,5,7,8,10,12)){
断言daysInMonth(月,年)==31;
}
for(整数月:Arrays.asList(4,6,9,11)){
断言daysInMonth(月,年)==30;
}
if(年数包含(年)){
断言daysInMonth(2,year)=29;
}
否则{
断言daysInMonth(2,year)=28;
}
}
}
公共静态int daysInMonth(int m,int y){
HashSet monthswith31天=新的HashSet(Arrays.asList(1,3,5,7,8,10,12));
HashSet monthsWith30Days=新的HashSet(Arrays.asList(4,6,9,11));
如果(每月31天,包含(m)){
返回31;
}否则,如果(每月30天,包含(m)){
ret