package com.gameqp.common;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* @ClassName: DateUtils
* @Description: 日期工具類
* @author duhai
* @date 2017年10月26日 上午11:24:37
*/
public class MyDateUtils {
/**
* 獲取上週一
*
* @param date
* @return
*/
public static Date geLastWeekMonday(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(getThisWeekMonday(date));
cal.add(Calendar.DATE, -7);
return cal.getTime();
}
/**
* 獲取本週一
*
* @param date
* @return
*/
public static Date getThisWeekMonday(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
// 獲得當前日期是一個星期的第幾天
int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
if (1 == dayWeek) {
cal.add(Calendar.DAY_OF_MONTH, -1);
}
// 設定一個星期的第一天,按中國的習慣一個星期的第一天是星期一
cal.setFirstDayOfWeek(Calendar.MONDAY);
// 獲得當前日期是一個星期的第幾天
int day = cal.get(Calendar.DAY_OF_WEEK);
// 根據日曆的規則,給當前日期減去星期幾與一個星期第一天的差值
cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
return cal.getTime();
}
/**
* 獲取下週一
*
* @param date
* @return
*/
public static Date getNextWeekMonday(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(getThisWeekMonday(date));
cal.add(Calendar.DATE, 7);
return cal.getTime();
}
/**
* 獲取上週一
*
* @param dateString
* 日期字串 比如:2017-10-26
* @return
* @throws ParseException
*/
public static Date geLastWeekMonday(String dateString) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = sdf.parse(dateString + " 00:00:00");
Calendar cal = Calendar.getInstance();
cal.setTime(getThisWeekMonday(date));
cal.add(Calendar.DATE, -7);
return cal.getTime();
}
/**
* 獲取本週一
*
* @param dateString
* 日期字串 比如:2017-10-26
* @return
* @throws ParseException
*/
public static Date getThisWeekMonday(String dateString) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = sdf.parse(dateString + " 00:00:00");
Calendar cal = Calendar.getInstance();
cal.setTime(date);
// 獲得當前日期是一個星期的第幾天
int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
if (1 == dayWeek) {
cal.add(Calendar.DAY_OF_MONTH, -1);
}
// 設定一個星期的第一天,按中國的習慣一個星期的第一天是星期一
cal.setFirstDayOfWeek(Calendar.MONDAY);
// 獲得當前日期是一個星期的第幾天
int day = cal.get(Calendar.DAY_OF_WEEK);
// 根據日曆的規則,給當前日期減去星期幾與一個星期第一天的差值
cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
return cal.getTime();
}
/**
* 獲取下週一
*
* @param dateString
* 日期字串 比如:2017-10-26
* @return
*/
public static Date getNextWeekMonday(String dateString) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = sdf.parse(dateString + " 00:00:00");
Calendar cal = Calendar.getInstance();
cal.setTime(getThisWeekMonday(date));
cal.add(Calendar.DATE, 7);
return cal.getTime();
}
/**
* 按照型別獲取時間
*
* @param type
* 0:上週一 1:本週一 2:下週一
* @return
* @throws ParseException
*/
public static Date getWeekMondayByType(Integer type) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = sdf.format(new Date()).substring(0, 10);
Date date = null;
if (type.equals(0)) {
date = geLastWeekMonday(dateString);
} else if (type.equals(1)) {
date = getThisWeekMonday(dateString);
} else if (type.equals(2)) {
date = getNextWeekMonday(dateString);
}
return date;
}
public static void main(String[] args) {
try {
System.out.println("上週一" + getWeekMondayByType(0));
System.out.println("本週一" + getWeekMondayByType(1));
System.out.println("下週一" + getWeekMondayByType(2));
} catch (ParseException e) {
e.printStackTrace();
}
}
public static void main2(String[] args) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
String dateString = "2017-10-26";
System.out.println("今天是" + dateString);
System.out.println("上週一" + sdf.format(geLastWeekMonday(dateString)));
System.out.println("本週一" + sdf.format(getThisWeekMonday(dateString)));
System.out.println("下週一" + sdf.format(getNextWeekMonday(dateString)));
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main1(String[] args) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date date = sdf.parse("2017-10-26 00:00:00");
System.out.println("今天是" + sdf.format(date));
System.out.println("上週一" + sdf.format(geLastWeekMonday(date)));
System.out.println("本週一" + sdf.format(getThisWeekMonday(date)));
System.out.println("下週一" + sdf.format(getNextWeekMonday(date)));
} catch (Exception e) {
e.printStackTrace();
}
}
}