特定年份的特定月份的天数?

如何知道特定年份的特定月份有多less天?

String date = "2010-01-19"; String[] ymd = date.split("-"); int year = Integer.parseInt(ymd[0]); int month = Integer.parseInt(ymd[1]); int day = Integer.parseInt(ymd[2]); Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.YEAR,year); calendar.set(Calendar.MONTH,month); int daysQty = calendar.getDaysNumber(); // Something like this 

创build一个日历,设置年份和月份,并使用getActualMaximum

 int iYear = 1999; int iMonth = Calendar.FEBRUARY; // 1 (months begin with 0) int iDay = 1; // Create a calendar object and set year and month Calendar mycal = new GregorianCalendar(iYear, iMonth, iDay); // Get the number of days in that month int daysInMonth = mycal.getActualMaximum(Calendar.DAY_OF_MONTH); // 28 

testing :在闰年尝试一个月:

 mycal = new GregorianCalendar(2000, Calendar.FEBRUARY, 1); daysInMonth= mycal.getActualMaximum(Calendar.DAY_OF_MONTH); // 29 

编辑:解决scheme为JAVA 8

@Warren M. Nocos。 如果您尝试使用Java 8的新date和时间API,则可以使用java.time.YearMonth类。

 // Get the number of days in that month YearMonth yearMonthObject = YearMonth.of(1999, 2); int daysInMonth = yearMonthObject.lengthOfMonth(); //28 

testing:在闰年尝试一个月:

 yearMonthObject = YearMonth.of(2000, 2); daysInMonth = yearMonthObject.lengthOfMonth(); //29 

java.util.Calendar的代码

如果你必须使用java.util.Calendar ,我怀疑你想要:

 int days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); 

代码乔达时间

但是,我个人build议使用Joda Time而不是java.util.{Calendar, Date}来开始,在这种情况下,您可以使用:

 int days = chronology.dayOfMonth().getMaximumValue(date); 

请注意,不是单独分析string值,而是使用您使用的任何date/时间API来parsing它。 在java.util.* ,可以使用SimpleDateFormat ; 在乔达时间,你会使用DateTimeFormatter

你可以使用Calendar.getActualMaximum方法:

 Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.YEAR, year); calendar.set(Calendar.MONTH, month); int numDays = calendar.getActualMaximum(Calendar.DATE); 

java.time.LocalDate

从Java 1.8开始,您可以在lengthOfMonth上使用java.time.LocalDate方法:

 LocalDate date = LocalDate.of(2010, 1, 19); int days = date.lengthOfMonth(); 
 if (month == 4 || month == 6 || month == 9 || month == 11) daysInMonth = 30; else if (month == 2) daysInMonth = (leapYear) ? 29 : 28; else daysInMonth = 31; 

在Java8中,您可以使用从date字段获取ValueRange。

 LocalDateTime dateTime = LocalDateTime.now(); ChronoField chronoField = ChronoField.MONTH_OF_YEAR; long max = dateTime.range(chronoField).getMaximum(); 

这使您可以在现场参数化。

 import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /* * 44. Return the number of days in a month * , where month and year are given as input. */ public class ex44 { public static void dateReturn(int m,int y) { int m1=m; int y1=y; String str=" "+ m1+"-"+y1; System.out.println(str); SimpleDateFormat sd=new SimpleDateFormat("MM-yyyy"); try { Date d=sd.parse(str); System.out.println(d); Calendar c=Calendar.getInstance(); c.setTime(d); System.out.println(c.getActualMaximum(Calendar.DAY_OF_MONTH)); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { dateReturn(2,2012); } } 
  String date = "11-02-2000"; String[] input = date.split("-"); int day = Integer.valueOf(input[0]); int month = Integer.valueOf(input[1]); int year = Integer.valueOf(input[2]); Calendar cal=Calendar.getInstance(); cal.set(Calendar.YEAR,year); cal.set(Calendar.MONTH,month-1); cal.set(Calendar.DATE, day); //since month number starts from 0 (ie jan 0, feb 1), //we are subtracting original month by 1 int days = cal.getActualMaximum(Calendar.DAY_OF_MONTH); System.out.println(days); 

以下方法将为您提供特定月份的date

 public static int getNoOfDaysInAMonth(String date) { Calendar cal = Calendar.getInstance(); cal.setTime(date); return (cal.getActualMaximum(Calendar.DATE)); } 

你可以使用Calendar.getActualMaximum方法:

  Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.YEAR, year); calendar.set(Calendar.MONTH, month-1); int numDays = calendar.getActualMaximum(Calendar.DATE); 

而month-1是因为月份在Calendar.class中的方法如下所示取其原始月份

  public int getActualMaximum(int field) { throw new RuntimeException("Stub!"); } 

(int字段)如下所示。

  public static final int JANUARY = 0; public static final int NOVEMBER = 10; public static final int DECEMBER = 11; 

这对我来说工作得很好。

这是一个示例输出

 import java.util.*; public class DaysInMonth{ public static void main(String args []){ Scanner input = new Scanner(System.in); System.out.print("Enter a year:"); int year = input.nextInt(); //Moved here to get input after the question is asked System.out.print("Enter a month:"); int month = input.nextInt(); //Moved here to get input after the question is asked int days = 0; //changed so that it just initializes the variable to zero boolean isLeapYear = (year % 4 == 0 && year % 100 != 0)||(year % 400 == 0); switch (month){ case 1: days = 31; break; case 2: if (isLeapYear) days = 29; else days = 28; break; case 3: days = 31; break; case 4: days = 30; break; case 5: days = 31; break; case 6: days = 30; break; case 7: days = 31; break; case 8: days = 31; break; case 9: days = 30; break; case 10: days = 31; break; case 11: days = 30; break; case 12: days = 31; break; default: String response = "Have a Look at what you've done and try again"; System.out.println(response); System.exit(0); } String response = "There are " +days+ " Days in Month "+month+ " of Year " +year+ ".\n"; System.out.println(response); // new line to show the result to the screen. } } //abhinavsthakur00@gmail.com 

这是math方法:

年,月(1至12):

 int daysInMonth = month == 2 ? 28 + (year % 4 == 0 ? 1:0) - (year % 100 == 0 ? (year % 400 == 0 ? 0 : 1) : 0) : 31 - (month-1) % 7 % 2; 
 public class Main { private static LocalDate local=LocalDate.now(); public static void main(String[] args) { int month=local.lengthOfMonth(); System.out.println(month); } }