1. 程式人生 > >日期格式化:SimpleDateFormat,以及獲取當前周的週一和週日的日期,當前月第一個和最後一天的日期

日期格式化:SimpleDateFormat,以及獲取當前周的週一和週日的日期,當前月第一個和最後一天的日期

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
/**
 * @author 作者:吳林飛
 * @version 建立時間:2018年4月22日 下午5:10:43
 * 類說明 :以及獲取當前周的週一和週日的日期,當前月第一個和最後一天的日期
 * 日期格式化:格式引數
  G 年代標誌符  y 年M 月 d 日    h 時 在上午或下午 (1~12)   H 時 在一天中 (0~23)   m 分   s 秒    S 毫秒
  E 星期 D 一年中的第幾天        F 一月中第幾個星期幾w 一年中第幾個星期W 一月中第幾個星期
  a 上午 / 下午 標記符 k 時 在一天中 (1~24)K 時 在上午或下午 (0~11)z 時區
 */
public class  SimpleDateFormatUtill{
public static void main(String[] args) throws ParseException {
//獲取Calendar物件
Calendar cal = Calendar.getInstance();
String forma2t=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss:S E D F w W z ").format(cal.getTime());
System.err.println(forma2t);
//每週週一的日期
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
String forma2 = new SimpleDateFormat("yyyyMMdd").format(cal
.getTime());
System.err.println(forma2);
//每週週日的日期,獲取當前周的週一日期,然後往後加6天就是週日所在日期
cal.add(Calendar.DATE, 6);
String forma = new SimpleDateFormat("yyyyMMdd").format(cal
.getTime());
System.err.println(forma);
//獲取本月最後一天的日期
cal.set(Calendar.DAY_OF_MONTH,cal.getActualMaximum(Calendar.DAY_OF_MONTH));
String forma3 = new SimpleDateFormat("yyyyMMdd").format(cal
.getTime());
System.err.println(forma3);
//獲取本月第一天的日期
cal.set(Calendar.DAY_OF_MONTH,cal.getActualMinimum(Calendar.DAY_OF_MONTH));
String formaa=new SimpleDateFormat("yyyyMMdd").format(cal.getTime());
System.err.println(formaa);
}
}