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

用Java编制一个日期计算程序,第1张

import java.util.Calendarimport java.util.Dateimport 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 = 0i <weeks.lengthi++) { System.out.print(weeks[i]) for (int j = 0j <2j++) { System.out.print(" ")} } System.out.println() int weekOfday = getWeekOfday(month,1)//获得第一天的星期 for (int i = 0i <(weekOfday - 1) * 5i++) { System.out.print(" ")} for (int i = 1i <= daysi++,weekOfday +=1) { if(weekOfday == 8) { weekOfday = 1 System.out.println()System.out.print(i) } else { System.out.print(i)} for (int j = 0j <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 = 0if(month!=2) { switch(month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12:days = 31 breakcase 4: case 6: case 9: case 11:days = 30} } else { if(year%4==0 &&year%100!=0 || year%400==0) days = 29else 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)} }

/** 

     * 计算两个日期之间相差的天数 

     * @param date1 

     * @param date2 

     * @return 

 乎凳    */  

    public static int daysBetween(Date date1,Date date2)  

    {  

    配清    Calendar cal = Calendar.getInstance()  

        cal.setTime(date1)  

        long time1 = cal.getTimeInMillis()               

        cal.setTime(date2)  

        long time2 = cal.getTimeInMillis()       

        long between_days=(time2-time1)/(1000*3600*24)  

          

       return Integer.parseInt(String.valueOf(between_days))         

    岁卖旅}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12505565.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-25
下一篇 2023-05-25

发表评论

登录后才能评论

评论列表(0条)

保存