java擷取當月天數,指定年月的天數,指定日期擷取對應星期(轉)

來源:互聯網
上載者:User

標籤:

 java擷取當月天數,指定年月的天數,指定日期擷取對應星期分類: Java2012-12-25 23:52 3201人閱讀 評論(0) 收藏 舉報j2seJ2SEjavaJavaJAVA擷取當月天數擷取指定年月天數擷取指定日期對應的星期
package com;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;/** * @author Gerrard */public class TestGetWeek {public static void main(String[] args) {int currentMaxDays = getCurrentMonthDay();int maxDaysByDate = getDaysByYearMonth(2012, 11);String week = getDayOfWeekByDate("2012-12-25");System.out.println("本月天數:" + currentMaxDays);System.out.println("2012年11月天數:" + maxDaysByDate);System.out.println("2012-12-25是:" + week);}/** * 擷取當月的 天數 * */public static int getCurrentMonthDay() {Calendar a = Calendar.getInstance();a.set(Calendar.DATE, 1);a.roll(Calendar.DATE, -1);int maxDate = a.get(Calendar.DATE);return maxDate;}/** * 根據年 月 擷取對應的月份 天數 * */public static int getDaysByYearMonth(int year, int month) {Calendar a = Calendar.getInstance();a.set(Calendar.YEAR, year);a.set(Calendar.MONTH, month - 1);a.set(Calendar.DATE, 1);a.roll(Calendar.DATE, -1);int maxDate = a.get(Calendar.DATE);return maxDate;}/** * 根據日期 找到對應日期的 星期 */public static String getDayOfWeekByDate(String date) {String dayOfweek = "-1";try {SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");Date myDate = myFormatter.parse(date);      SimpleDateFormat formatter = new SimpleDateFormat("E");      String str = formatter.format(myDate);      dayOfweek = str;      } catch (Exception e) {System.out.println("錯誤!");}return dayOfweek;}}

java擷取當月天數,指定年月的天數,指定日期擷取對應星期(轉)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.