java月历
⑴ 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编程的问题(关于输出月历)!
看看这个,说得很清楚了