用Calendar方法知道月份的天数
package test; import java.math.BigDecimal; import java.nio.channels.NonReadableChannelException; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.*; public class Main { private static final String space_operator = " "; private static final double pi = Math.PI; public static void main(String[] args) throws Exception { Calendar c = Calendar.getInstance(); /* * 获取当前时间 */ showDate(c); /* * 获取昨天的这个时间段 */ c.add(Calendar.DAY_OF_MONTH, -1); showDate(c); /* * 想知道每年的二月份有多少天 */ february(2013); } /** * @param year * 我们直接找出三月一号; * 然后向下偏移一天就是二月的最后一天 * 直接用c.get方法输出 * 需要注意的是月份是从0开始的 */ public static void february(int year) { Calendar c = Calendar.getInstance(); c.set(year, 2, 1); c.add(Calendar.DAY_OF_MONTH, -1); System.out.println(c.get(Calendar.DAY_OF_MONTH)); } public static void showDate(Calendar c) { int year = c.get(Calendar.YEAR); int month = c.get(Calendar.MONTH) + 1; int day = c.get(Calendar.DAY_OF_MONTH); int week = c.get(Calendar.DAY_OF_WEEK); System.out.println(year + "年" + month + "月" + day + "日" + getweek(week)); } public static String getweek(int week) { String [] weeks = {"","星期日","星期一","星期二","星期三","星期四","星期五","星期六"}; return weeks[week]; } }