java獲取當前月第一天和最後一天,上個月第一天和最後一天
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); java
//獲取前月的第一天學習
Calendar cal_1=Calendar.getInstance();//獲取當前日期 spa
cal_1.add(Calendar.MONTH, -1);orm
cal_1.set(Calendar.DAY_OF_MONTH,1);//設置爲1號,當前日期既爲本月第一天 字符串
firstDay = format.format(cal_1.getTime());get
System.out.println("-----1------firstDay:"+firstDay);io
//獲取前月的最後一天ast
Calendar cale = Calendar.getInstance(); form
cale.set(Calendar.DAY_OF_MONTH,0);//設置爲1號,當前日期既爲本月第一天 學習資料
lastDay = format.format(cale.getTime());
System.out.println("-----2------lastDay:"+lastDay);
//獲取當前月第一天:
Calendar c = Calendar.getInstance();
c.add(Calendar.MONTH, 0);
c.set(Calendar.DAY_OF_MONTH,1);//設置爲1號,當前日期既爲本月第一天
String first = format.format(c.getTime());
System.out.println("===============first:"+first);
//獲取當前月最後一天
Calendar ca = Calendar.getInstance();
ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
String last = format.format(ca.getTime());
System.out.println("===============last:"+last);
/**
* 字符串的日期格式的計算
*/
public static int daysBetween(String smdate,String bdate) throws ParseException{
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
cal.setTime(sdf.parse(smdate));
long time1 = cal.getTimeInMillis();
cal.setTime(sdf.parse(bdate));
long time2 = cal.getTimeInMillis();
long between_days=(time2-time1)/(1000*3600*24);
return Integer.parseInt(String.valueOf(between_days));
}
喜歡這樣文章的能夠關注我,我會持續更新,大家的關注是我更新的動力!須要更多java學習資料的也能夠私信我!
祝關注個人人都:身體健康,財源廣進,福如東海,壽比南山,早生貴子,從不掉髮!