java輸入一個日期,編寫程序輸出這個日期所在年份和月份的月歷

importjava.text.ParseException;
importjava.text.SimpleDateFormat;
importjava.util.Calendar;

publicclassTest29{
publicstaticvoidmain(String[]args)throwsParseException{
Stringdate="2015-7-1";
SimpleDateFormatsdf=newSimpleDateFormat("yyyy-MM-dd");
Calendarca=Calendar.getInstance();
ca.setTime(sdf.parse(date));
System.out.println("年:"+ca.get(Calendar.YEAR));
System.out.println("月:"+ca.get(Calendar.MONTH));
}
}

⑵ java輸出一個月的月歷

import java.util.*;

public class Year{
/**
*判斷是否為閏年
*/
public boolean getIsRun(int year){
if((year%4==0 && year %100 !=0)||(year%400==0)){
return true;
}
return false;
}

/**
*返回某年某月有多少天。
*/
public int getDay(int year ,int month){
int[] day={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};//閏年月份
int[] day1={31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};//普通月份
if(this.getIsRun(year)){
return day[month+1];
}else{
return day1[month+1];
}
}
/**
*返回這個月的1號是星期幾
*/

public int getWeek(int year,int month){
Date date=new Date();
date.setYear(year);
date.setMonth(month);
Calendar calendar=Calendar.getInstance();
calendar.setTime(date);
return calendar.get(calendar.DAY_OF_WEEK);
}

/**
*列印出來,嘎嘎
*/
public void showPrint(int year,int month){
int day=this.getDay(year,month);
int week=this.getWeek(year,month);
System.out.println("日\t一\t二\t三\t四\t五\t六\n");

for(int i=0;i<=week;i++){//
System.out.print (" \t");
}
for(int j=1;j<=day;j++){
if((j+week)%7==0){
System.out.println ();
}
System.out.print (j+"\t");

}
// System.out.println(week);
}

public static void main(String[] args){
Year y=new Year();
y.showPrint(2008,3);
}
}

⑶ java日歷,求代碼

importjava.util.Scanner;

/**
*java輸入年,月得到日歷
*
*@authoryoung
*
*/
publicclassPrintCalendar{
publicstaticvoidmain(String[]args){
Scannerinput=newScanner(System.in);
System.out.print("Enterfullyear(eg:2001):");
intyear=input.nextInt();
System.out.print("Entermonthinnumber1between12:");
intmonth=input.nextInt();
printMonth(year,month);
}

publicstaticvoidprintMonth(intyear,intmonth){
printMonthTitle(year,month);
printMonthBody(year,month);
}

(intyear,intmonth){
System.out.println(""+getMonthName(month)+""+year);
System.out.println("______________________________");
System.out.println("SunMonTueWedThuFriSat");
}

(intmonth){
StringmonthName="";
switch(month){
case1:
monthName="Fanuary";
break;
case2:
monthName="January";
break;
case3:
monthName="March";
break;
case4:
monthName="April";
break;
case5:
monthName="May";
break;
case6:
monthName="June";
break;
case7:
monthName="July";
break;
case8:
monthName="August";
break;
case9:
monthName="September";
break;
case10:
monthName="October";
break;
case11:
monthName="November";
break;
case12:
monthName="December";
}
returnmonthName;
}

(intyear,intmonth){
intstartDay=getStartDay(year,month);
intnumberOfDaysInMonth=getNumberOfDaysInMonth(year,month);
inti=0;

for(i=0;i<startDay;i++){
System.out.printf("%4s","");
}
for(i=1;i<=numberOfDaysInMonth;i++){
System.out.printf("%4d",i);
if((i+startDay)%7==0)
System.out.println();
}
//System.out.println();
}

publicstaticintgetStartDay(intyear,intmonth){
finalintSTART_DAY_FOR_JAN_1_1800=3;
inttotalNumberOfDays=getTotalNumberOfDays(year,month);
return(totalNumberOfDays+START_DAY_FOR_JAN_1_1800)%7;
}

(intyear,intmonth){
inttotal=0;
for(inti=1800;i<year;i++)
if(isLeapYear(i))
total=total+366;
else
total=total+365;
for(inti=1;i<month;i++)
total=total+getNumberOfDaysInMonth(year,i);
returntotal;
}

//每個月天數
(intyear,intmonth){
if(month==1||month==3||month==5||month==7||month==8
||month==10||month==12)
return31;
if(month==4||month==6||month==8||month==11)
return30;
if(month==2)
returnisLeapYear(year)?29:28;
return0;
}

//判斷閏年
publicstaticbooleanisLeapYear(intyear){
returnyear%400==0||(year%4==0&&year%100!=0);
}
}

⑷ JAVA編程輸出月歷

importjava.util.Calendar;
importjava.util.Scanner;

publicclassTest{
staticpublicvoidmain(String參數[]){
Calendarc=Calendar.getInstance();
Scannersc=newScanner(System.in);
System.out.println("請輸入年份:");
intyear=sc.nextInt();
c.set(Calendar.YEAR,year);
c.set(Calendar.MONTH,Calendar.JANUARY);
c.set(Calendar.DAY_OF_MONTH,1);
while(c.get(Calendar.YEAR)==year){
intwday=c.get(Calendar.DAY_OF_WEEK);
intmday=c.get(Calendar.DAY_OF_MONTH);
if(mday==1){
System.out.println(" 日 一 二 三 四 五 六 第"+(c.get(Calendar.MONTH)+1)+"月");
System.out.println("---------------------------------------------------");
for(inti=0;i<wday-1;i++)System.out.print(" ");
}
System.out.print(mday+" ");
if(wday==7)System.out.println();
c.add(Calendar.DAY_OF_YEAR,1);
}
}
}

⑸ 關於java中的月歷

int month = Integer.parseInt(yearString); ===》用錯變數了 ,正確的應該用「monthString 」

⑹ JAVA編寫一個輸出一個月月歷的程序

輸入本年的某個月後,屏幕輸出這個月的月歷,每星期一行,從星期日開始,到星期六結束。

import java.util.*;
public class Year{
/**
*判斷是否為閏年
*/
public boolean getIsRun(int year){
if((year%4==0 && year %100 !=0)||(year%400==0)){
return true;
}
return false;
}
/**
*返回某年某月有多少天。
*/
public int getDay(int year ,int month){
int[] day={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};//閏年月份
int[] day1={31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};//普通月份
if(this.getIsRun(year)){
return day[month+1];
}else{
return day1[month+1];
}
}
/**
*返回這個月的1號是星期幾
*/
public int getWeek(int year,int month){
Date date=new Date();
date.setYear(year);
date.setMonth(month);
Calendar calendar=Calendar.getInstance();
calendar.setTime(date);
return calendar.get(calendar.DAY_OF_WEEK);
}
/**
*列印出來,嘎嘎
*/
public void showPrint(int year,int month){
int day=this.getDay(year,month);
int week=this.getWeek(year,month);
System.out.println("日\t一\t二\t三\t四\t五\t六\n");
for(int i=0;i<=week;i++){//
System.out.print (" \t");
}
for(int j=1;j<=day;j++){
if((j+week)%7==0){
System.out.println ();
}
System.out.print (j+"\t");
}
// System.out.println(week);
}
public static void main(String[] args){
Year y=new Year();
y.showPrint(2008,3);
}
}

⑺ java 月歷(補充成能成功輸出的)

是日歷吧,直接拿系統的就是

⑻ 怎麼用java編寫一個指定的年月的日歷

|package com;

import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;

public class MyCalendar
{
public static void main(String[] args)
{
scanner = new Scanner(System.in);
String reg = "^(\\d+)[^\\d]+((0?[1-9])|(1[012]))$";
while(true)
{
System.out.println("輸入年月(年和月用非數字隔開:如2015.1)(什麼都不輸入直接退出)");
String line = scanner.nextLine().trim();
if("".equals(line))
{
scanner.close();
break;
}
if(!line.matches(reg))
{
continue;
}
int year = Integer.parseInt(line.replaceAll(reg, "$1"));
int month = Integer.parseInt(line.replaceAll(reg, "$2"));
System.out.println("日\t一\t二\t三\t四\t五\t六");
Calendar calendar = Calendar.getInstance();
// 這個月的1號是星期幾
calendar.set(year, month - 1, 1);
int day = calendar.get(Calendar.DAY_OF_WEEK);
int start = Calendar.SUNDAY;
calendar.add(Calendar.DATE, -day + start);
while(start < day)
{
System.out.print(calendar.get(Calendar.DATE) + "\t");
calendar.add(Calendar.DATE, 1);
start++;
}
calendar.set(year, month - 1, 1);
Date now = calendar.getTime();
calendar.set(year, month, 1);
Date next = calendar.getTime();
for(Date cur = now; cur.before(next);)
{
calendar.setTime(cur);
int x = calendar.get(Calendar.DATE);
String tmp = x < 10 ? "0" + x : x + "";
System.out.print(tmp + "\t");
if(calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY)
{
System.out.println();
}
calendar.add(Calendar.DATE, 1);
cur = calendar.getTime();
}
calendar.add(Calendar.DATE, -1);
int to = calendar.get(Calendar.DAY_OF_WEEK);
int end = Calendar.SATURDAY;
while(to < end)
{
calendar.add(Calendar.DATE, 1);
int x = calendar.get(Calendar.DATE);
String tmp = x < 10 ? "0" + x : x + "";
System.out.print(tmp + "\t");
to++;
}
System.out.println("\r\n");
}
}
}

⑼ java編程的問題(關於輸出月歷)!

看看這個,說得很清楚了