1. 程式人生 > >va獲取當月第一天的零點零分和最後一天的23點59分

va獲取當月第一天的零點零分和最後一天的23點59分

public static void main(){ SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd hh:mm:ss”); Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.DAY_OF_MONTH, 1); //將小時至0 calendar.set(Calendar.HOUR_OF_DAY, 0); //將分鐘至0 calendar.set(Calendar.MINUTE, 0); //將秒至0 calendar.set(Calendar.SECOND,0); //將毫秒至0 calendar.set(Calendar.MILLISECOND, 0); //獲得當前月第一天 Date startDate = calendar.getTime(); //將當前月加1; calendar.add(Calendar.MONTH, 1); //在當前月的下一月基礎上減去1毫秒 calendar.add(Calendar.MILLISECOND, -1); //獲得當前月最後一天 Date endDate = calendar.getTime();

	System.out.println("The first day of month=="+startDate .toString());  
	System.out.println("first day:"+startDate .getTime());
	System.out.println("The last day of month=="+endDate .toString());  
	System.out.println("last day:"+endDate .getTime());
	
	System.out.println("The first day of month=="+sdf.format(startDate ););  
	System.out.println("The last day of month=="+sdf.format(endDate ););  

}