java获取年 、月、日
import Java.util.Calendar;
public class Main {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
int day = cal.get(Calendar.DATE);
int month = cal.get(Calendar.MONTH) + 1;
int year = cal.get(Calendar.YEAR);
int dow = cal.get(Calendar.DAY_OF_WEEK);
int dom = cal.get(Calendar.DAY_OF_MONTH);
int doy = cal.get(Calendar.DAY_OF_YEAR);
System.out.println("Current Date: " + cal.getTime());
System.out.println("Day: " + day);
System.out.println("Month: " + month);
System.out.println("Year: " + year);
System.out.println("Day of Week: " + dow);
System.out.println("Day of Month: " + dom);
System.out.println("Day of Year: " + doy);
}
}
java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
java.util.Date currTime = new java.util.Date();
int year = currTime.getYear();//年
int month = currTime.getMonth()+1;//月
int week = currTime.getDay(); //星期几
int day = currTime.getDate();//日
String curTime = formatter.format(currTime);
获取当前日期:
public class TestDate {
public static void main(String[] args) {
Date now = new Date();
Calendar cal = Calendar.getInstance();
}
获取当月最后一天:
public static boolean isMonthEnd(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.DATE) == calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
}
//求日期天数差
public static long getCompareDate(String startDate,String endDate) {
SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd");
Date date1 = null,date2 = null;
try {
date1 = formatter.parse(startDate);
date2=formatter.parse(endDate);
} catch (ParseException e) {
e.printStackTrace();
}
long l = date2.getTime() - date1.getTime();
long d = l/(24*60*60*1000);
return d;
}