package com.cn.gjy;
成都創(chuàng)新互聯(lián)2013年開創(chuàng)至今,先為海珠等服務(wù)建站,海珠等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為海珠企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。
import java.util.Scanner;
/*
說明:1這個(gè)應(yīng)該沒有問題,package是我編輯用的包名,你去掉;
2,純自己手打的
3剛剛看到你這個(gè)不是從外面輸入,那更簡單,你加一個(gè)set方法就OK了
public void setDay(int d){}然后調(diào)用一下就OK了
* 要用下列六種METHOD
* validateInput (判定用戶輸入是否正確)
* getMonth (拿月份名稱)
* isLeapYear (判定是否閏年)
* daysInMonth (確定一個(gè)月有多少天)
* computeTotalDays (計(jì)算從1月1日開始過去了多少天)
* nextDay (明天是幾號.)*/
public class JustJustJust {
private static int day;
private static int month;
private static int year;
static Scanner sc = new Scanner(System.in);
//輸入錯(cuò)誤則推出程序
public static void validateInput(){
System.out.println("請輸入年份");
//String dateYear = sc.next();
int dateYear = Integer.parseInt(sc.next());
if(dateYear2013dateYear1899) {
year = dateYear;
}else{
System.out.println("輸入錯(cuò)誤:超出范圍");
System.out.println("程序終止");
System.exit(0);
}
System.out.println("請輸入月份");
//String dateMon = sc.next();
int dateMon = Integer.parseInt(sc.next());
if(dateMon13dateMon0) {
month = dateMon;
}else{
System.out.println("輸入錯(cuò)誤:超出范圍");
System.out.println("程序終止");
System.exit(0);
}
System.out.println("請輸入日期");
//String dateDay = sc.next();
int dateDay = Integer.parseInt(sc.next());
if((month==1||month==3||month==5||month==7||month==8||month==10||month==12)(dateDay0dateDay33)){
day = dateDay;
}
else if((month==2)(year%4==0)){
if(year%100 !=0){
if(dateDay0dateDay30){
day = dateDay;
}else{
System.out.println("輸入錯(cuò)誤:超出范圍");
System.out.println("程序終止");
System.exit(0);
}
}
}
else if((month==2)(year%400==0)(dateDay0dateDay30))
{day=dateDay;}
else if((month==4||month==6||month==8||month==11)(dateDay0dateDay31))
{day=dateDay;}
else{
System.out.println("輸入錯(cuò)誤:超出范圍");
System.out.println("程序終止");
System.exit(0);
}
}
//獲取
//天
public static int getDay(){
return day;
}
//月
public int getMonth() {
return month;
}
//年
public int getYear() {
return year;
}
//判斷閏年
public static void isLeapYear(){
if((year%100 !=0)(year%4==0)){
System.out.println("今年是:" + year + "年" );
System.out.println("是閏年");
}else{
System.out.println("今年是:" + year + "年");
System.out.println("不是潤年");
}
}
public static void daysInMonth(){
if((year%100 !=0)(year%4==0)){
if((month==1||month==3||month==5||month==7||month==8||month==10||month==12)){
System.out.println("這個(gè)月是" + month + "月有31天");
}
else if(month == 2){
System.out.println("這個(gè)月是二月有29天");
}else{
System.out.println("這個(gè)月是" + month + "月有30天");
}
}else{
System.out.println("今年是:" + year + "年");
System.out.println("不是潤年");
if((month==1||month==3||month==5||month==7||month==8||month==10||month==12)){
System.out.println("這個(gè)月是" + month + "月有31天");
}
else if(month == 2){
System.out.println("這個(gè)月是二月有28天");
}else{
System.out.println("這個(gè)月是" + month + "月有30天");
}
}
}
public static int getFebDay(){
int febDay;
if((year%100 !=0)(year%4==0)){
febDay = 29;
}else{
febDay = 28;
}
return febDay;
}
public static void computeTotalDays(int m){
m = month;
switch(m)
{
case 1:m = 1;System.out.println("1月1日開始過去了" + (day + 0) + "天");break;
case 2:m = 2;System.out.println("1月1日開始過去了" + (day + 31) + "天");break;
case 3:m = 3; System.out.println("1月1日開始過去了" + (day + 31+ JustJustJust.getFebDay()) + "天");break;
case 4:System.out.println("1月1日開始過去了" + (day + 31+ JustJustJust.getFebDay() + 31) + "天");break;
/*
* 寫的匆忙,沒有想到社么好方法,你就以此類推吧
case 5:System.out.println("1月1日開始過去了" + (day + ) + "天");break;
case 6:System.out.println("1月1日開始過去了" + (day + ) + "天");break;
case 7:System.out.println("1月1日開始過去了" + (day + ) + "天");break;
case 8:System.out.println("1月1日開始過去了" + (day + ) + "天");break;
case 9:System.out.println("1月1日開始過去了" + (day + ) + "天");break;
case 10:System.out.println("1月1日開始過去了" + (day + ) + "天");break;
case 11:System.out.println("1月1日開始過去了" + (day + ) + "天");break;
case 12:System.out.println("1月1日開始過去了" + (day + ) + "天");break;
*/
}
}
public static void nextDay(){
System.out.println("明天是 " + (JustJustJust.getDay() + 1) + "號");
}
public static void main(String[] args) {
JustJustJust jjj = new JustJustJust();
jjj.validateInput();
jjj.getMonth();
jjj.isLeapYear();
jjj.daysInMonth();
jjj.computeTotalDays(month);
jjj.nextDay();
/*
* 要用下列六種METHOD
* validateInput (判定用戶輸入是否正確)
* getMonth (拿月份名稱)
* isLeapYear (判定是否閏年)
* daysInMonth (確定一個(gè)月有多少天)
* computeTotalDays (計(jì)算從1月1日開始過去了多少天)
* nextDay (明天是幾號.)*/
}
}
import java.util.Calendar; import java.util.Date; import java.util.Scanner; /** * */ /** * @author think * */ public class Test1 { /** * */ public Test1() { // TODO Auto-generated constructor stub } /** * function: * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub new Test1().print(); } /** * function: 打印函數(shù) 包含對當(dāng)前輸入的月歷的打印 以及輸入和輸出的打印 */ public void print(){ //當(dāng)月天數(shù) //輸入你的查詢年月 如果直接輸入月 默認(rèn)是當(dāng)前年 Scanner scanner = new Scanner(System.in); System.out.println("1.please input the month:"); int month = scanner.nextInt(); while(month = 0 || month 12) { System.out.println("1.please input the month:"); month = scanner.nextInt(); } int days = days(month); String[] weeks = {"sun","mon","tue","wed","thr","Frd","sat"}; for (int i = 0; i weeks.length; i++) { System.out.print(weeks[i]); for (int j = 0; j 2; j++) { System.out.print(" "); } } System.out.println(); int weekOfday = getWeekOfday(month,1); //獲得第一天的星期 for (int i = 0; i (weekOfday - 1) * 5; i++) { System.out.print(" "); } for (int i = 1; i = days; i++,weekOfday +=1) { if(weekOfday == 8) { weekOfday = 1; System.out.println(); System.out.print(i); } else { System.out.print(i); } for (int j = 0; j 5 - (String.valueOf(i).length()); j++) { System.out.print(" "); } } System.out.println("\n"); System.out.println("2.please input the date(format is like:20030204):"); String date = scanner.next(); while(date.length() 8) { System.out.println("2.please input the date(format is like:20030204):"); date = scanner.next(); } System.out.println("your input date weekday is:" + (getWeekDayStr(getWeekOfday(date)))); System.out.println("\n"); System.out.println("3.please input the compute date1(format is like:20030204):"); String date1 = scanner.next(); while(date.length() 8) { System.out.println("3.please input the compute date1(format is like:20030204):"); date1 = scanner.next(); } System.out.println("3.please input the compute date2(format is like:20030204):"); String date2 = scanner.next(); while(date.length() 8) { System.out.println("3.please input the compute date2(format is like:20030204):"); date2 = scanner.next(); } int xiangchadays = daysxiangcha(date1, date2); System.out.println("xiangcha days is:" + xiangchadays); } /** * function: 通過準(zhǔn)確的年月日查詢星期幾的整數(shù)表示 * @param year * @param month * @param day * @return */ public int getWeekOfday(int year,int month, int day) { Calendar c = Calendar.getInstance(); c.set(Calendar.YEAR, year); c.set(Calendar.MONTH, month - 1); c.set(Calendar.DAY_OF_MONTH, day); return c.get(Calendar.DAY_OF_WEEK); } /** * function: 通過輸入月日 查詢星期幾的整數(shù)表示 默認(rèn)是當(dāng)前年 * @param month * @param day * @return */ public int getWeekOfday(int month, int day) { Calendar c = Calendar.getInstance(); c.setTime(new Date()); int year = c.get(Calendar.YEAR); return getWeekOfday(year, month, day); } /** * function: 通過日期字符串獲得星期幾的整數(shù)表示 * @param dateStr * @return */ public int getWeekOfday(String dateStr) { int year =Integer.parseInt(dateStr.substring(0, 4)) ; int month =Integer.parseInt(dateStr.substring(4, 6)) ; int day =Integer.parseInt(dateStr.substring(6, 8)) ; return getWeekOfday(year, month, day); } /** * function: 返回星期幾這樣的表示 * @param weekday * @return */ public String getWeekDayStr(int weekday) { return weekDayStrs[weekday - Calendar.SUNDAY]; } String[] weekDayStrs = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"}; /** * function: 兩個(gè)日期的相差天數(shù) * @param dateStr1 20100205這樣的格式 * @param dateStr2 20100205這樣的格式 * @return */ public int daysxiangcha(String dateStr1,String dateStr2) { int year1 =Integer.parseInt(dateStr1.substring(0, 4)) ; int month1 =Integer.parseInt(dateStr1.substring(4, 6)) ; int day1 =Integer.parseInt(dateStr1.substring(6, 8)) ; int year2 =Integer.parseInt(dateStr2.substring(0, 4)) ; int month2 =Integer.parseInt(dateStr2.substring(4, 6)) ; int day2 =Integer.parseInt(dateStr2.substring(6, 8)) ; Calendar c1 = Calendar.getInstance(); c1.set(Calendar.YEAR, year1); c1.set(Calendar.MONTH, month1 -1); c1.set(Calendar.DAY_OF_MONTH, day1); Calendar c2 = Calendar.getInstance(); c2.set(Calendar.YEAR, year2); c2.set(Calendar.MONTH, month2 - 1); c2.set(Calendar.DAY_OF_MONTH, day2); long mills = c1.getTimeInMillis() c2.getTimeInMillis()? c1.getTimeInMillis() - c2.getTimeInMillis(): c2.getTimeInMillis() - c1.getTimeInMillis(); return (int)(mills/1000/3600/24); } /** * @param year * @param month * @return 某年某月的天數(shù) */ public static int days(int year,int month) { int days = 0; if(month!=2) { switch(month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12:days = 31 ;break; case 4: case 6: case 9: case 11:days = 30; } } else { if(year%4==0 year%100!=0 || year%400==0) days = 29; else days = 28; } return days; } /** * function: 查詢某月的天數(shù) 默認(rèn)是當(dāng)前年份 * @param month * @return */ public static int days(int month) { Calendar c = Calendar.getInstance(); c.setTime(new Date()); int year = c.get(Calendar.YEAR); return days(year, month); } }
如果你用上了 Java8:
import?java.time.LocalDate;
import?java.util.Scanner;
public?class?Demo?{
public?static?void?main(String[]?args)?throws?Exception?{
try?(Scanner?in?=?new?Scanner(System.in))?{
System.out.println("輸入第一個(gè)日期的年月日,并用空格分開:");
LocalDate?date1?=?LocalDate.of(in.nextInt(),?in.nextInt(),?in.nextInt());
System.out.println("輸入第二個(gè)日期的年月日,并用空格分開:");
LocalDate?date2?=?LocalDate.of(in.nextInt(),?in.nextInt(),?in.nextInt());
long?days?=?Math.abs(date1.toEpochDay()?-?date2.toEpochDay());
System.out.printf("這個(gè)兩個(gè)日期相差了?%d?天\n",?days);
}
}
}