Java實現按年月列印日曆功能【基於Calendar】,日曆calendar

來源:互聯網
上載者:User

Java實現按年月列印日曆功能【基於Calendar】,日曆calendar

本文執行個體講述了Java實現按年月列印日曆功能。分享給大家供大家參考,具體如下:

import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;public class CalendarBook {  public static void main(String[] args) throws ParseException {    CalendarBook cb = new CalendarBook();    cb.printWeekTitle();    cb.printCalendar(2018, 3);  }  public void printCalendar(int year, int month) throws ParseException {    String monthStr; // 格式化月份,因為要轉成yyyyMMdd格式的    if (month < 10) {      monthStr = "0" + month;    } else {      monthStr = month + ""; // 數字跟字串拼接轉成字串格式    }    String yearMonthStr = year + monthStr;    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");    Calendar calendarEnd = Calendar.getInstance();    Calendar calendarStart = Calendar.getInstance();    // 根據年份和月份得到輸入月份有多少天    int monthDays = getMonthLastDay(year, month);    // 月初的date字串    String dateStartStr = yearMonthStr + "01";    // 月末的date字串    String dateEndStr = yearMonthStr + monthDays;    Date startDate = sdf.parse(dateStartStr);    Date endDate = sdf.parse(dateEndStr);    calendarStart.setTime(startDate);    calendarEnd.setTime(endDate);    // 得到輸入月份有多少周    int weeks = calendarEnd.get(Calendar.WEEK_OF_MONTH);    // 得到當月第一天是星期幾,這裡周日為第一天,從1開始,周一則為2    int dayOfWeek = calendarStart.get(Calendar.DAY_OF_WEEK);    int day = 1;    // 當月的第一周做特殊處理,單獨列印一行    for (int i = 1; i <= 7; i++) {      if (i >= dayOfWeek) {        System.out.print(" " + day + " ");        day++;      } else {        System.out.print("  ");      }    }    System.out.println();    // 開始列印從第二周開始的日期    for (int week = 1; week < weeks; week++) {      for (int i = 1; i <= 7; i++) {        if (day > monthDays) {          break;        }        if (day < 10) {          System.out.print(" " + day + " ");        } else {          System.out.print(day + " ");        }        day++;      }      System.out.println();    }  }  public int getMonthLastDay(int year, int month) {    int monthDay;    int[][] day = { { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },        { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } };    if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {      // 閏年      monthDay = day[1][month];    } else {      monthDay = day[0][month];    }    return monthDay;  }  public void printWeekTitle() {    System.out.println("日" + "   " + "一" + "   " + "二" + "   " + "三"        + "   " + "四" + "   " + "五" + "   " + "六");  }}

運行結果(運行效果,字型大小5號最佳):

PS:這裡再為大家推薦幾款關於日期與時間計算的線上工具供大家參考使用:

線上日期/天數計算機:
http://tools.jb51.net/jisuanqi/date_jisuanqi

線上萬年曆日曆:
http://tools.jb51.net/bianmin/wannianli

線上陰曆/陽曆轉換工具:
http://tools.jb51.net/bianmin/yinli2yangli

Unix時間戳記(timestamp)轉換工具:
http://tools.jb51.net/code/unixtime

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.