大橙子网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

日期推算程序java代码,java时间换算程序

请问如何编写以下一个计算日期的Java代码。

package com.cn.gjy;

成都创新互联2013年开创至今,先为海珠等服务建站,海珠等地企业,进行企业商务咨询服务。为海珠企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

import java.util.Scanner;

/*

说明:1这个应该没有问题,package是我编辑用的包名,你去掉;

2,纯自己手打的

3刚刚看到你这个不是从外面输入,那更简单,你加一个set方法就OK了

public void setDay(int d){}然后调用一下就OK了

* 要用下列六种METHOD

* validateInput (判定用户输入是否正确)

* getMonth (拿月份名称)

* isLeapYear (判定是否闰年)

* daysInMonth (确定一个月有多少天)

* computeTotalDays (计算从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);

//输入错误则推出程序

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("输入错误:超出范围");

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("输入错误:超出范围");

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("输入错误:超出范围");

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("输入错误:超出范围");

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("这个月是" + month + "月有31天");

}

else if(month == 2){

System.out.println("这个月是二月有29天");

}else{

System.out.println("这个月是" + 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("这个月是" + month + "月有31天");

}

else if(month == 2){

System.out.println("这个月是二月有28天");

}else{

System.out.println("这个月是" + 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 (确定一个月有多少天)

* computeTotalDays (计算从1月1日开始过去了多少天)

* nextDay (明天是几号.)*/

}

}

用Java编制一个日期计算程序

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: 打印函数 包含对当前输入的月历的打印 以及输入和输出的打印 */ public void print(){ //当月天数 //输入你的查询年月 如果直接输入月 默认是当前年 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: 通过准确的年月日查询星期几的整数表示 * @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: 通过输入月日 查询星期几的整数表示 默认是当前年 * @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: 通过日期字符串获得星期几的整数表示 * @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: 两个日期的相差天数 * @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 某年某月的天数 */ 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: 查询某月的天数 默认是当前年份 * @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); } }

从键盘输入两个日期算出这两个日期相差多少天。求java程序代码!

如果你用上了 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("输入第一个日期的年月日,并用空格分开:");

LocalDate date1 = LocalDate.of(in.nextInt(), in.nextInt(), in.nextInt());

System.out.println("输入第二个日期的年月日,并用空格分开:");

LocalDate date2 = LocalDate.of(in.nextInt(), in.nextInt(), in.nextInt());

long days = Math.abs(date1.toEpochDay() - date2.toEpochDay());

System.out.printf("这个两个日期相差了 %d 天\n", days);

}

}

}


文章名称:日期推算程序java代码,java时间换算程序
网站URL:http://dzwzjz.com/article/hspghd.html
在线咨询
服务热线
服务热线:028-86922220
TOP