package temp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Cal_days {
public static final String[] month_list= {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
public static final String fail_refs_fileName= "/Result_fail_Refs.xlsx";
public static void main(String[] args) {
get();
Calendar cld = Calendar.getInstance();
System.out.println(cld.YEAR);
System.out.println(get_month_number("Dec"));
//SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date(System.currentTimeMillis());
System.out.println(formatter.format(date));
String s = formatter.format(date);
s= s.substring(0,s.length()-2);
s = s+"01";
System.out.println(s);
int a =(int)(Math.random()*1000);
System.out.println("a::::::::::: "+a);
}
public static String get_month_number(String M) {
for(int i=0;i<month_list.length;i++) {
if(month_list[i].equals(M)) {
int index = i+1;
if(index>=10) {
return index+"";
}else {
return "0"+index;
}
}
}
return "NONE";
}
public static void get() {
DateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
try {
Date star = dft.parse("2021-02-03");//开始时间
Date endDay=dft.parse("2021-07-02");//结束时间
Date nextDay=star;
int i=0;
while(nextDay.before(endDay)){//当明天不在结束时间之前是终止循环
Calendar cld = Calendar.getInstance();
cld.setTime(star);
cld.add(Calendar.DATE, 1);
star = cld.getTime();
//获得下一天日期字符串
nextDay = star;
i++;
}
System.out.println("相差天数为:"+i);
} catch (Exception e) {
e.printStackTrace();
}
}
}
计算工作日
package temp;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;
import java.util.Calendar;
import java.util.Scanner;
public class Cal_workday {
private String startDate;
private String endDate;
public static void main(String[] args){
get();
//Cal_workday daysCounter=new Cal_workday();
//daysCounter.calculateDate();
}
public static void get() {
TemporalAdjuster nextWorkingDay = TemporalAdjusters.ofDateAdjuster(temporal -> {
DayOfWeek dow =DayOfWeek.of(temporal.get(ChronoField.DAY_OF_WEEK));
int dayToAdd = 2;
if (dow ==DayOfWeek.THURSDAY){dayToAdd = 4;}
if (dow ==DayOfWeek.FRIDAY){dayToAdd = 4;}
if (dow ==DayOfWeek.SATURDAY){dayToAdd = 3;}
System.out.println(temporal.plus(dayToAdd, ChronoUnit.DAYS));
return temporal.plus(dayToAdd, ChronoUnit.DAYS);
});
LocalDate localDate = LocalDate.now().with(nextWorkingDay);//测试
}
public void calculateDate(){
@SuppressWarnings("resource")
Scanner in=new Scanner(new InputStreamReader(System.in));
System.out.println("Enter the starting date (DD/MM/YY) :");
startDate=in.next();
System.out.println("Enter the End date (DD/MM/YY) :");
endDate=in.next();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
try{
Calendar start = Calendar.getInstance();
start.setTime(sdf.parse(startDate));
Calendar end = Calendar.getInstance();
end.setTime(sdf.parse(endDate));
int workingDays = 0;
while(!start.after(end)){
int day = start.get(Calendar.DAY_OF_WEEK);
if ((day != Calendar.SATURDAY) && (day != Calendar.SUNDAY))
workingDays++;
start.add(Calendar.DATE, 1);
}
System.out.println(workingDays);
}catch(Exception e){
e.printStackTrace();
}
}
}