獲取上月:
Calendar c = Calendar.getInstance();
c.add(Calendar.MONTH, -1);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
String time = format.format(c.getTime());
//得到一個月最後一天日期(31/30/29/28)
int MaxDay=c.getActualMaximum(Calendar.DAY_OF_MONTH);
//按你的要求設定時間
c.set( c.get(Calendar.YEAR), c.get(Calendar.MONTH), MaxDay, 23, 59, 59);
//按格式輸出
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String gtime = sdf.format(c.getTime()); //上月最後一天
/**
* 獲取本日日期
* @return
*/
public static String getToDay(){
Calendar c = Calendar.getInstance();
String time = getTime(c);
System.out.println(time);
return time;
}
/**
* 獲取本週一日期
* @return
*/
public static String getWeekA(){
Calendar cal =Calendar.getInstance();
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
// System.out.println(cal.getTime());
String time = getTime(cal)+" 00:00:00";
System.out.println(time);
return time;
}
/**
* 獲取本週日日期
* @return
*/
public static String getWeekB(){
Calendar cal =Calendar.getInstance();
// System.out.println(cal.getTime());
//這種輸出的是上個星期週日的日期,因為老外那邊把週日當成第一天
cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
// System.out.println(cal.getTime());
//增加一個星期,才是我們中國人理解的本週日的日期
cal.add(Calendar.WEEK_OF_YEAR, 1);
// System.out.println(cal.getTime()); //本週日
String time = getTime(cal)+" 23:59:59";
System.out.println(time);
return time;
}
/**
* 獲取本月日期
* @return
*/
public static String getMonth(){
Calendar c = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
String time = format.format(c.getTime());
System.out.println(time);
return time;
}