时间工具类
public class DateUtils {
public static String getTime(){
Date date = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
String time = simpleDateFormat.format(date);
return time;
}
public static int compareTime(String date1,String date2) throws ParseException {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date time1 = simpleDateFormat.parse(date1);
Date time2 = simpleDateFormat.parse(date2);
return date1.compareTo(date2);
}
public static String getLastDate(String date) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, Integer.parseInt(date.substring(0,4)));
calendar.set(Calendar.MONTH, Integer.parseInt(date.substring(5))-1);
calendar.set(Calendar.DAY_OF_MONTH,1);
calendar.roll(Calendar.DAY_OF_MONTH,-1);
String lastDate = simpleDateFormat.format(calendar.getTime());
return lastDate;
}
public static String diffDate(String date1,String date2) throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
long time1 = format.parse(date1).getTime();
long time2 = format.parse(date2).getTime();
long diffDate = (time2-time1)/1000/60/60/24;
return String.valueOf(Math.abs(diffDate));
}
public static String getDateData(String date,int i){
String s;
LocalDate localDate = LocalDate.of(Integer.parseInt(date.substring(0,4)),Integer.parseInt(date.substring(5,7)),Integer.parseInt(date.substring(8)));
switch (i){
case 1:
s = String.valueOf(localDate.getYear());
break;
case 2:
s = String.valueOf(localDate.getMonth());
break;
case 3:
s = String.valueOf(localDate.getDayOfMonth());
break;
case 4:
s = String.valueOf(localDate.getDayOfWeek());
break;
case 5:
s = String.valueOf(localDate.lengthOfMonth());
break;
case 6:
s = String.valueOf(localDate.isLeapYear());
break;
default:
System.out.println("暂无你要的方法");
return null;
}
return s;
}
}