public class Day01 {
public static void main(String[] args) {
// TODO Auto-generated method stub
/* int year =2022;
if(isLeapYear(year)){
System.out.println("闰年");
}else{
System.out.println("平年");
}*/
int beginYear = 1999;
int beginMonth = 9;
int beginDay = 9;
int endYear = 2022;
int endMonth = 3;
int endDay = 18;
int yearAllDays = yearAllDays(beginYear,beginMonth,beginDay,endYear,endMonth,endDay);
//总数对五除余
int allDays = yearAllDays%5;
//根据余数判断打鱼还是晒网
if(allDays <=3){
System.out.println("打鱼");
}else{
System.out.println("晒网");
}
/*
*int beginYear = 1999;
int endYear = 2021;
int sumDay = yearDays(beginYear,endYear);
System.out.println(sumDay);
*/
/*for(int i=beginYear;i<=endYear;i++){
if(isLeapYear(i)){
sumDay =366;
else
sumDay =365;
}
System.out.println(sumDay);*/
}
public static int yearAllDays(int beginYear,int beginMonth,int beginDay,int endYear,int endMonth,int endDay){
//1:求出总共有多少天
//a 开始日期到年底的天数
int days1 = mouthDays(beginYear,beginMonth,beginDay,12,31);
//b 中间年份的天数
int days2 = yearDays(beginYear,endYear);
//c最后一年第一天到当前结束日期的天数
int days3 = mouthDays(endYear,1,1,endMonth,endDay);
//d把这些天数相加
int sumDays = days1+days2+days3;
return sumDays;
}
/**
* 判断不跨年月份之间的天数
* @param year
* @param startMonth
* @param startDay
* @param endMonth
* @param endDay
* @return
*/
public static int mouthDays(int year,int startMonth,int startDay,int endMonth,int endDay){
int[] monthDayArr = {0,31,28,31,30,31,30,31,31,30,31,30,31};
int days = 0;
if(startMonth == endMonth){
// 1.不跨月
days = endDay-startDay +1;
}else{
//2.跨月
//a 当前月份有几天
days += (monthDayArr[startMonth] - startDay +1);
//b 中间月份有几天
for(int i = startMonth +1;i < endMonth;i++){
days+=monthDayArr[i];
}
//c 最后一月天数
days+=endDay;
//d 判断是否闰年 是+1 不是不管
//1.startMonth<2,endMonth<=2 不考虑
//2.startMonth<2,endMonth>2 考虑
//3.startMonth=2,endMonth>2 考虑
//4.startMonth>2,endMonth>2 不考虑
if(isLeapYear(year) && (startMonth <=2 && endMonth >2))
days +=1;
}
return days;
}
/**
* 判断两年之间的天数
* @param beginYear 开始年份
* @param endYear 结束年份
* @param sumDay
* @return 总天数
*/
public static int yearDays(int beginYear,int endYear){
int sumDay =0;
for(int i= beginYear;i <= endYear;i++){
if(isLeapYear(i))
sumDay +=366;
else
sumDay +=365;
}
return sumDay;
}
/**
* 是否为闰年
* @param year 年份
* @return 是否是闰年结果
*/
public static boolean isLeapYear(int year) {
if((year%4 ==0 && year%100 !=0) || year%400 ==0){
return true;
}else{
return false;
}
}
}
}
}
//用毫秒计算两个日期间的天数
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateTest {undefined
private int getTermDays2() throws Exception{undefined
String sEndDate = "2018-03-01";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = new Date();
Date date2 = sdf.parse(sEndDate);
long days = (date2.getTime() - date1.getTime()) / (24*3600*1000);
long yushu = (date2.getTime() - date1.getTime()) % (24*3600*1000);
System.out.println("days:" + days + ",yushu:" + yushu);
//规整方法1
date1.setHours(0);
date1.setMinutes(0);
date1.setSeconds(0);
long days2 = (date2.getTime() - date1.getTime()) / (24*3600*1000);
long yushu2 = (date2.getTime() - date1.getTime()) % (24*3600*1000);
System.out.println("days2:" + days2 + ",yushu2:" + yushu2);
//规整方法2
String sdate1 = sdf.format(date1);
date1 = sdf.parse(sdate1);
long days3 = (date2.getTime() - date1.getTime()) / (24*3600*1000);
long yushu3 = (date2.getTime() - date1.getTime()) % (24*3600*1000);
System.out.println("days3:" + days3 + ",yushu3:" + yushu3);
return (int)days;
}
public static void main(String[] args) {undefined
DateTest dt = new DateTest();
try {undefined
dt.getTermDays2();
} catch (Exception e) {undefined
e.printStackTrace();
}
}
}
03-26
273
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)