转自:http://blog.csdn.net/aiqinhai1016_/article/details/12027459
package com.sangame.uc.front.web.controller.front;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
/**
* Created by on 2017/4/17.
*/
public class DateUtils {
private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
public static void main(String args[]){
System.out.println(getCurrentMonday());
System.out.println(getPreviousSunday());
System.out.println(getMinMonthDate("2017-04-17"));
System.out.println(getMaxMonthDate("2017-04-17"));
}
// 获得本周一与当前日期相差的天数
public static int getMondayPlus() {
Calendar cd = Calendar.getInstance();
int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK);
if (dayOfWeek == 1) {
return -6;
} else {
return 2 - dayOfWeek;
}
}
// 获得当前周- 周一的日期
public static String getCurrentMonday() {
int mondayPlus = getMondayPlus();
GregorianCalendar currentDate = new GregorianCalendar();
currentDate.add(GregorianCalendar.DATE, mondayPlus);
Date monday = currentDate.getTime();
DateFormat df = DateFormat.getDateInstance();
String preMonday = df.format(monday);
return preMonday;
}
// 获得当前周- 周日 的日期
public static String getPreviousSunday() {
int mondayPlus = getMondayPlus();
GregorianCalendar currentDate = new GregorianCalendar();
currentDate.add(GregorianCalendar.DATE, mondayPlus +6);
Date monday = currentDate.getTime();
DateFormat df = DateFormat.getDateInstance();
String preMonday = df.format(monday);
return preMonday;
}
// 获得当前月--开始日期
public static String getMinMonthDate(String date) {
Calendar calendar = Calendar.getInstance();
try {
calendar.setTime(dateFormat.parse(date));
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
return dateFormat.format(calendar.getTime());
} catch (java.text.ParseException e) {
e.printStackTrace();
}
return null;
}
// 获得当前月--结束日期
public static String getMaxMonthDate(String date){
Calendar calendar = Calendar.getInstance();
try {
calendar.setTime(dateFormat.parse(date));
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
return dateFormat.format(calendar.getTime());
} catch (java.text.ParseException e) {
e.printStackTrace();
}
return null;
}
}
package com.sangame.uc.utils;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
/**
* Created by on 2017/4/18.
*/
public class TimeUtil {
private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
public static void main(String args[]){
//本周一
System.out.println(getCurrentMonday());
//本周二
System.out.println(getPreviousSunday());
//上周一
System.out.println(getCurrentMonday(-7));
//上周日
System.out.println(getPreviousSunday(-7));
//本月初
System.out.println(getMinMonthDate("2017-04-17"));
//本月末
System.out.println(getMaxMonthDate("2017-04-17"));
//上月初
System.out.println(getMinMonthDate("2017-03-17",-1));
//上月末
System.out.println(getMaxMonthDate("2017-03-17",-1));
//昨天
System.out.println(getYesteroday());
}
// 获得本周一与当前日期相差的天数
public static int getMondayPlus() {
Calendar cd = Calendar.getInstance();
int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK);
if (dayOfWeek == 1) {
return -6;
} else {
return 2 - dayOfWeek;
}
}
public static int getMondayPlus(int days) {
Calendar cd = Calendar.getInstance();
int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK);
if (dayOfWeek == 1) {
return -6+days;
} else {
return 2 - dayOfWeek+days;
}
}
// 获得当前周- 周一的日期
public static String getCurrentMonday() {
int mondayPlus = getMondayPlus();
GregorianCalendar currentDate = new GregorianCalendar();
currentDate.add(GregorianCalendar.DATE, mondayPlus);
Date monday = currentDate.getTime();
DateFormat df = DateFormat.getDateInstance();
String preMonday = df.format(monday);
return preMonday;
}
// 获得当前周- 周一的日期
public static String getCurrentMonday(int days) {
int mondayPlus = getMondayPlus(days);
GregorianCalendar currentDate = new GregorianCalendar();
currentDate.add(GregorianCalendar.DATE, mondayPlus);
Date monday = currentDate.getTime();
DateFormat df = DateFormat.getDateInstance();
String preMonday = df.format(monday);
return preMonday;
}
// 获得当前周- 周日 的日期
public static String getPreviousSunday() {
int mondayPlus = getMondayPlus();
GregorianCalendar currentDate = new GregorianCalendar();
currentDate.add(GregorianCalendar.DATE, mondayPlus +6);
Date monday = currentDate.getTime();
DateFormat df = DateFormat.getDateInstance();
String preMonday = df.format(monday);
return preMonday;
}
// 获得当前周- 周日 的日期
public static String getPreviousSunday(int days) {
int mondayPlus = getMondayPlus(days);
GregorianCalendar currentDate = new GregorianCalendar();
currentDate.add(GregorianCalendar.DATE, mondayPlus +6);
Date monday = currentDate.getTime();
DateFormat df = DateFormat.getDateInstance();
String preMonday = df.format(monday);
return preMonday;
}
// 获得当前月--开始日期
public static String getMinMonthDate(String date) {
Calendar calendar = Calendar.getInstance();
try {
calendar.setTime(dateFormat.parse(date));
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
return dateFormat.format(calendar.getTime());
} catch (java.text.ParseException e) {
e.printStackTrace();
}
return null;
}
// 获得当前月--开始日期
public static String getMinMonthDate(String date,int month) {
Calendar calendar = Calendar.getInstance();
try {
calendar.setTime(dateFormat.parse(date));
calendar.add(Calendar.MONTH, month);
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
return dateFormat.format(calendar.getTime());
} catch (java.text.ParseException e) {
e.printStackTrace();
}
return null;
}
// 获得当前月--结束日期
public static String getMaxMonthDate(String date){
Calendar calendar = Calendar.getInstance();
try {
calendar.setTime(dateFormat.parse(date));
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
return dateFormat.format(calendar.getTime());
} catch (java.text.ParseException e) {
e.printStackTrace();
}
return null;
}
public static String getMaxMonthDate(String date,int month){
Calendar calendar = Calendar.getInstance();
try {
calendar.setTime(dateFormat.parse(date));
calendar.add(Calendar.MONTH, month);
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
return dateFormat.format(calendar.getTime());
} catch (java.text.ParseException e) {
e.printStackTrace();
}
return null;
}
public static String getYesteroday(){
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, -1);
return dateFormat.format(calendar.getTime());
}
}