get current wee

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;
}
}

posted @ 2018-02-06 14:37  applekingghfhfhbr  阅读(110)  评论(0编辑  收藏  举报