zoukankan      html  css  js  c++  java
  • 用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];
    	}
    	
    }
    

      

  • 相关阅读:
    Codeforces Round #239(Div. 2) 做后扯淡玩
    hdu 3507 Print Article
    prufer序列
    POJ 2778 DNA Sequence
    Codeforces Round #237 (Div. 2)
    poj3352
    图论知识
    POJ 2186
    Codeforces Round #236 (Div. 2)
    POJ 2823 Sliding Window
  • 原文地址:https://www.cnblogs.com/WINDZLY/p/11788745.html
Copyright © 2011-2022 走看看