java得到几天前和几天后时间,DateUtil时间工具类
分类:面向对象

import java.util.Calendar;

/** * 得到当前月的上一个月,返回2016-06这样的字符串 */ 

DateUtil时间工具类,dateutil工具类

package com.ruisi.core.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public final class DateUtil{

/** 默认的年月日 */
public static final String defaultPattern = "yyyy-MM-dd";

/** hour12HMSPattern年月日 时分秒 12小时制*/
public static final String hour12HMSPattern = "yyyy-MM-dd hh:mm:ss";

/** hour12HMPattern年月日 时分 12小时制*/
public static final String hour12HMPattern = "yyyy-MM-dd hh:mm";

/** hour12HPattern年月日 时 12小时制*/
public static final String hour12HPattern = "yyyy-MM-dd hh";

/** hour24HMSPattern年月日 时分秒 24小时制 */
public static final String hour24HMSPattern = "yyyy-MM-dd HH:mm:ss";

/** hour24HMPattern年月日 时分 24小时制*/
public static final String hour24HMPattern = "yyyy-MM-dd HH:mm";

/** hour24HPattern年月日 时 24小时制*/
public static final String hour24HPattern = "yyyy-MM-dd HH";

public static String daysBetween(Date startDate, Date endDate) {
float d = endDate.getTime() - startDate.getTime();
float dd = d / 86400000f;
int ddd = (int)dd;

float hh = (dd - ddd) * 24;
int hhh = (int)hh;

float mm = (hh - hhh) * 60;
int mmm = (int)mm;

float ss = (mm - mmm) * 60;
int sss = (int)ss;
return ddd + "天" + hhh + "小时" + mmm + "分" + sss + "秒";
}

/**
* 返回预设Format的当前日期字符串
*/
public static String getToday() {
Date today = new Date();
return format(today);
}

public static String getYestoday() {
Calendar cal1 = Calendar.getInstance();
cal1.setTime(new Date());
cal1.add(Calendar.DATE, -1);
return format(cal1.getTime());
}

public static String getTheDayBeforeYestoday() {
Calendar cal1 = Calendar.getInstance();
cal1.setTime(new Date());
cal1.add(Calendar.DATE, -2);
return format(cal1.getTime());
}

public static String getPreviousDay3() {
Calendar cal1 = Calendar.getInstance();
cal1.setTime(new Date());
cal1.add(Calendar.DATE, -3);
return format(cal1.getTime());
}

public static String getPreviousDay4() {
Calendar cal1 = Calendar.getInstance();
cal1.setTime(new Date());
cal1.add(Calendar.DATE, -4);
return format(cal1.getTime());
}

/**
* 用预设Format格式化Date成字符串
*/
public static String format(Date date) {
return format(date, defaultPattern);
}

/**
* 使用参数Format格式化Date成字符串
*/
public static String format(Date date, String pattern) {
String returnValue = "";

if (date != null) {
SimpleDateFormat df = new SimpleDateFormat(pattern);
returnValue = df.format(date);
}

return (returnValue);
}

/**
* 使用预设格式将字符串转为Date
*/
public static Date parse(String strDate) throws ParseException {
return parse(strDate, defaultPattern);
}

public static Date parse(String strDate, String pattern)
throws ParseException {
SimpleDateFormat df = new SimpleDateFormat(pattern);
return df.parse(strDate);
}

/**
* 在日期上增加数个整月
*/
public static Date addMonth(Date date, int n) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.MONTH, n);
return cal.getTime();
}

/**
* 在日期上增加日
*/
public static Date addDay(Date date, int n) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, n);
return cal.getTime();
}

public static String formatDateTime(Date date) {
SimpleDateFormat outFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return outFormat.format(date);
}

@SuppressWarnings("static-access")
public static String getEndOfMonth(String year, String month) {
Calendar cal = Calendar.getInstance();
cal.set(cal.YEAR, Integer.parseInt(year));
cal.set(cal.MONTH, Integer.parseInt(month) - 1);
return cal.getActualMaximum(cal.DAY_OF_MONTH) + "";
}

public static String addDays(String sdate, int n) throws ParseException
{
Calendar cal1 = Calendar.getInstance();
cal1.setTime(parse(sdate, defaultPattern));
cal1.add(Calendar.DATE, n);
return format(cal1.getTime());

}

public static String getFirstOfMonth() throws ParseException {
Calendar cal1 = Calendar.getInstance();
cal1.setTime(new Date());
cal1.set(5, 1);
return format(cal1.getTime());

}

public static String getFirstOfMonth(String sDate) throws ParseException {
Calendar cal1 = Calendar.getInstance();
cal1.setTime(parse(sDate, defaultPattern));
cal1.set(5, 1);
return format(cal1.getTime());

}

/**
* 获取年
*
* @param sdate
* @return String
*/
public static String getYear(String sdate) {
String[] date = sdate.split("-");
return date[0];
}

/**
* 获取月
*
* @param sdate
* @return String
*/
public static String getMonth(String sdate) {
String[] date = sdate.split("-");
return date[1];
}

public static String getCurrentYear() {
Calendar cale = Calendar.getInstance();
return Integer.toString(cale.get(Calendar.YEAR));
}

public static String getCurrentMonth() {
Calendar cale = Calendar.getInstance();
int month = cale.get(Calendar.MONTH);
month++;
String sMonth = Integer.toString(month);
if (month < 10)
sMonth = "0" + month;
return sMonth;
}

/**
* 获取天
*
* @param sdate
* @return String
*/
public static String getDay(String sdate) {
String[] date = sdate.split("-");
return date[2];
}

public static String getFullDate(String date) {
if (date != null && date.length() == 1)
return "0" + date;
return date;
}

public static String getSimpleDateString(String sdate) {
return sdate.replace("-", "");
}

//把日期从字符弄转成日期型
public static Date convertStringToDate(String pattern, String strDate)
throws ParseException {
Date aDate = null;
aDate = parse(strDate, pattern);
return aDate;
}

//根据指定格式得到当前日期的字符串
public static String getTodayDate(String aMask){
Date date = new Date();
return getDateTime(aMask,date);
}

//根据指定格式得到指定日期的字符串
public static String getDateTime(String aMask, Date aDate) {
SimpleDateFormat df = null;
String returnValue = "";
df = new SimpleDateFormat(aMask);
returnValue = df.format(aDate);
return (returnValue);
}

public static int getYear() {
Calendar c = Calendar.getInstance();
int yy = c.get(Calendar.YEAR);
return yy;
}

public static int getMonth() {
Calendar c = Calendar.getInstance();
int month = c.get(Calendar.MONTH);
return month+1;
}

public static int getDate() {
Calendar c = Calendar.getInstance();
int date = c.get(Calendar.DATE);
return date;
}

public static int getHour() {
Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
return hour;
}

public static int getSecond() {
Calendar c = Calendar.getInstance();
int second = c.get(Calendar.SECOND);
return second;
}

public static int getMinute() {
Calendar c = Calendar.getInstance();
int minute = c.get(Calendar.MINUTE);
return minute;
}

public static String getCurDateTime() {
return getCurDate() + " " + getCurTime();
}

public static String getCurDate() {
String fullDate = getCurYearMonth();
int temp = getDate();
if(temp < 10){
fullDate += "-0" + temp;
}else{
fullDate += "-" + temp;
}
return fullDate;
}

public static String getCurYearMonth() {
String fullDate = String.valueOf(getYear());
int temp = getMonth();
if(temp < 10){
fullDate += "-0" + temp;
}else{
fullDate += "-" + temp;
}
return fullDate;
}

public static String getCurTime() {
String time = getCurHourMinute();
int temp = getSecond();
if(temp < 10){
time += ":0" + temp;
}else{
time += ":" + temp;
}
return time;
}

public static String getCurHourMinute() {
String time;
int temp = getHour();
if(temp < 10){
time = "0" + temp + ":";
}else{
time = temp + ":";
}
temp = getMinute();
if(temp < 10){
time += "0" + temp;
}else{
time += temp;
}
return time;
}
/**
*
* @Description: 获取当前时间
* @param @return
* @return StringBuffer
* @throws
* @author ningpeng
* @date 2016年1月8日
*/
public static StringBuffer getNow()
{
Date date = new Date();
Calendar c = new GregorianCalendar();
c.setTime(date);
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH)+1;
int day = c.get(Calendar.DATE);
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
int seconds = c.get(Calendar.SECOND);
StringBuffer buff = new StringBuffer();
buff.append(year).append(month).append(day).append(hour).append(minute).append(seconds).toString();
return buff;
}

public static String DateFormat(Date date) {
Calendar c=Calendar.getInstance();
c.setTime(date);
c.get(java.util.Calendar.HOUR_OF_DAY);
String pattern="yyyy-MM-dd";
if(c.get(java.util.Calendar.HOUR_OF_DAY)!=0){
pattern="yyyy-MM-dd hh";
}else if(c.get(java.util.Calendar.MINUTE)!=0){
pattern="yyyy-MM-dd hh:mm";
}else if(c.get(java.util.Calendar.SECOND)!=0){
pattern="yyyy-MM-dd hh:mm:ss";
}
return new SimpleDateFormat(pattern).format(date);
}

public static void main(String[] args){
System.out.println(getMonth());
}
}

package com.ruisi.core.utils; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java...

/** 

import java.util.Date;
import java.util.GregorianCalendar;

 public static String getLastMonth() {

   * 得到几天前的时间 

public class ZCalendarUtils {

 Calendarcal=Calendar.getInstance();

   * @param d 

/**
* 取去年的今天
* @return
*/
public static String getLastYear() {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.YEAR, -1);
Date d = cal.getTime();
return DateTimeUtils.formatToDateTime(d);
}

 Calendar cal = Calendar.getInstance();

   * @param day 

/**
* 获取某一天是星期几
* @param dt
* @return
*/
public static String getWeekOfDate(Date dt) {
String[] weekDays = {"7", "1", "2", "3", "4", "5", "6"};
Calendar cal = Calendar.getInstance();
cal.setTime(dt);
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0){
w = 0;
}
return weekDays[w];
}
/**
* 取当前是星期几
* @return
*/
public static String getCurrentWeek() {
String day = getWeekOfDate(new Date());
return day;
}

 Calendar cal = Calendar.getInstance(); 

   * @return 

/**
* 取当前月第一天
* @return
*/
public static String getFirstDayOfCurrentMonth() {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_MONTH, 1);
Date d = cal.getTime();
return DateTimeUtils.formatToDate(d);
}

 cal.add(Calendar.MONTH, -1);

   */  

/**
* 取昨天
* @return
*/
public static String getYesterday() {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_MONTH, -1);
Date d = cal.getTime();
return DateTimeUtils.formatToDate(d);
}

 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");

  public static Date getDateBefore(Date d,int day){  

/**
* 取当前月最后一天
* @return
*/
public static String getLastDayOfCurrentMonth() {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MONTH, 1);
cal.set(Calendar.DAY_OF_MONTH, 1);
cal.add(Calendar.DAY_OF_MONTH, -1);
Date d = cal.getTime();
return DateTimeUtils.formatToDate(d);
}
/**
* 取上月第一天
* @return
*/
public static String getFirstDayOfLastMonth() {
Calendar cal = Calendar.getInstance();
cal.add(cal.MONTH, -1);
cal.set(Calendar.DAY_OF_MONTH, 1);
Date d = cal.getTime();
return DateTimeUtils.formatToDate(d);
}
/**
* 取上月最后一天
* @return
*/
public static String getLastDayOfLastMonth() {
Calendar cal = Calendar.getInstance();
//取得系统当前时间所在月第一天时间对象
cal.set(Calendar.DAY_OF_MONTH, 1);
//日期减一,取得上月最后一天时间对象
cal.add(Calendar.DAY_OF_MONTH, -1);
Date d = cal.getTime();
return DateTimeUtils.formatToDate(d);
}

 String date = format.format(cal.getTime());

   Calendar now =Calendar.getInstance();  

/**
* 取当前是本月第几周
* @return
*/
public static int getWeekNumber(){
Calendar calendar = Calendar.getInstance();
calendar.setFirstDayOfWeek(Calendar.MONDAY);
calendar.setTime(new Date());
return calendar.get(Calendar.WEEK_OF_MONTH) - 1;
}
/**
* 取给定日期所在月份的第几周
* @param newDate
* @param week
* @return
*/
public static Calendar getCalendar(Date newDate, int week) {
Calendar caleNew = Calendar.getInstance();
caleNew.setTime(newDate);
caleNew.add(Calendar.WEEK_OF_MONTH, week -1 );
return caleNew;
}
/**
* 获上星期的第一天
*
* @return
*/
public static String getFirstDayOfLastWeek() {
int currentWeekNo = ZCalendarUtils.getWeekNumber();
Date date = ZCalendarUtils.getFirstDayOfWeek(new Date(), currentWeekNo -1);
return DateTimeUtils.formatToDate(date);
}
/**
* 获取当前星期的第一天
*
* @return
*/
public static String getFirstDayOfCurrentWeek() {
int currentWeekNo = ZCalendarUtils.getWeekNumber();
Date date = ZCalendarUtils.getFirstDayOfWeek(new Date(), currentWeekNo);
return DateTimeUtils.formatToDate(date);
}
/**
* 获取上星期的最后一天
*
* @return
*/
public static String getLastDayOfLastWeek() {
int currentWeekNo = ZCalendarUtils.getWeekNumber();
Date date = ZCalendarUtils.getLastDayOfWeek(new Date(), currentWeekNo -1);
return DateTimeUtils.formatToDate(date);
}
/**
* 获取当前星期的最后一天
*
* @return
*/
public static String getLastDayOfCurrentWeek() {
int currentWeekNo = ZCalendarUtils.getWeekNumber();
Date date = ZCalendarUtils.getLastDayOfWeek(new Date(), currentWeekNo);
return DateTimeUtils.formatToDate(date);
}

 return date;

   now.setTime(d);  

/**
* 获取给定日期所在月的第n星期的第一天
*
* @return
*/
private static Date getFirstDayOfWeek(Date month, int week) {
Calendar myCale = Calendar.getInstance();
try{
GregorianCalendar gc = (GregorianCalendar)getCalendar(month, week );
myCale.setTime(gc.getTime());
myCale.set(Calendar.DATE, gc.get(Calendar.DATE) - gc.get(Calendar.DAY_OF_WEEK) + 2);
}catch (Exception e) {
e.printStackTrace();
}
return myCale.getTime();
}

 } 

   now.set(Calendar.DATE,now.get(Calendar.DATE)-day);  

/**
* 获取给定日期所在月的第n星期的最后一天
*
* @return
*/
private static Date getLastDayOfWeek(Date month, int week) {
Calendar myCale = Calendar.getInstance();
try{
GregorianCalendar gc = (GregorianCalendar)getCalendar(month, week);
myCale.setTime(gc.getTime());
myCale.set(Calendar.DATE, gc.get(Calendar.DATE) + 8 - gc.get(Calendar.DAY_OF_WEEK));
}catch (Exception e) {
e.printStackTrace();
}
return myCale.getTime();
}

 /** * 得到当前月的第一天,返回2016-06-01这样的字符串 * * 

   return now.getTime();  

public static int getYearOfDate(Date date) {
if (date == null) {
return 1990;
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.YEAR);
}

@return */

  }  

public static int getMonthOfDate(Date date) {
if (date == null) {
return 1;
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.MONTH) + 1;
}

 public static String getCurrentMonthFirstDay() { 

    

public static int getDayOfDate(Date date) {
if (date == null) {
return 1;
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.DAY_OF_MONTH);
}

 Calendar c = Calendar.getInstance();

  /** 

public static int getCurrentMonth() {
return getMonthOfDate(new Date());
}

 c.add(Calendar.MONTH, 0);

   * 得到几天后的时间 

public static String getCurrentMonthStr() {
return getCurrentMonth() + "";
}

 c.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天

   * @param d 

public static String getMonthOfDateStr(Date date) {
return getMonthOfDate(date) + "";
}

 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 

   * @param day 

/**
* 获取某日期所在月份的最大天数
*
* @param date
* @return
*/
public static int getMaxDaysOfMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
}

 String first = format.format(c.getTime()); 

   * @return 

}

 return first; 

   */  

 } 

  public static Date getDateAfter(Date d,int day){  

 public static String getCurrentMonthTheLastDay() {

   Calendar now =Calendar.getInstance();  

 Calendar c = Calendar.getInstance(); 

   now.setTime(d);  

 c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH)); 

   now.set(Calendar.DATE,now.get(Calendar.DATE)+day);  

 return dateToStr(c.getTime(), "yyyy-MM-dd"); 

   return now.getTime();  

 } 

  } 

 /** * 获取当前年的第一个月,返回2016-01这样的数据 * * @return */ 

 public static String getCurrentYearFirstMonth() {

 Calendar c = Calendar.getInstance(); 

 c.add(Calendar.YEAR, 0); 

 c.set(Calendar.DAY_OF_YEAR, 1); 

 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM"); 

 String first = format.format(c.getTime());

 return first; } 

 /** * 获取当前年已过的所有季度的集合(季度包含的月份未完则也算入)*/

 public static ListgetAllQuarterOfThisYear() { 

 Listquarters = new ArrayList();

 Calendar cal = Calendar.getInstance(); 

 int year = cal.get(Calendar.YEAR);

 int month = cal.get(Calendar.MONTH) + 1;

 if (month > 9) {

 quarters.add(year + "-" + 1);

 quarters.add(year + "-" + 2);

 quarters.add(year + "-" + 3); 

 quarters.add(year + "-" + 4);

 return quarters; 

 } if (month > 6 && month <= 9) { 

 quarters.add(year + "-" + 1);

 quarters.add(year + "-" + 2);

 quarters.add(year + "-" + 3); 

 return quarters; 

 } if (month > 3 && month <= 6) {

 quarters.add(year + "-" + 1); 

 quarters.add(year + "-" + 2);

 return quarters;

 } if (month >= 1 && month <= 3) {

 quarters.add(year + "-" + 1); 

 return quarters;

 }

 return null; 

 } 

 /** * 获取昨天 * * @return */ 

 public static String getYesterDay() { 

 Calendar cal = Calendar.getInstance();

 cal.add(Calendar.DATE, -1);

 String yesterday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());

 return yesterday;

 }

 /** * 获取指定时间范围内的所有日期 * * @param startDate * (format="2016-06-01") * @param endDate * (format="2016-07-12") * @return * @throws ParseException */

 public static ListgetDaysBetween(String startDate, String endDate) {

 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 

 Listresult = new ArrayList(); 

 result.add(startDate);

 try { 

 Calendar calBegin = Calendar.getInstance(); // 使用给定的 Date 设置此 Calendar 的时间 Date begin = format.parse(startDate); 

 calBegin.setTime(begin);

 Calendar calEnd = Calendar.getInstance(); // 使用给定的 Date 设置此 Calendar 的时间 Date end = format.parse(endDate); calEnd.setTime(end); // 测试此日期是否在指定日期之后 while (end.after(calBegin.getTime())) { // 根据日历的规则,为给定的日历字段添加或减去指定的时间量 calBegin.add(Calendar.DAY_OF_MONTH, 1);

 result.add(format.format(calBegin.getTime()));

 } 

 } catch (Exception e) {

 e.printStackTrace(); 

 }

 return result; 

 } 

/* * 获取指定时间范围内的所有月份 * * @param startDate(format="2016-06") * @param endDate(format="2016-08") * @return * @throws ParseException */

 public static ListgetMonthsBetween(String startDate, String endDate) {

 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");

 Listresult = new ArrayList();

 try { Calendar calBegin = Calendar.getInstance();

 Calendar calEnd = Calendar.getInstance();

 calBegin.setTime(sdf.parse(startDate)); 

 calBegin.set(calBegin.get(Calendar.YEAR), calBegin.get(Calendar.MONTH), 1); calEnd.setTime(sdf.parse(endDate)); 

 calEnd.set(calEnd.get(Calendar.YEAR), calEnd.get(Calendar.MONTH), 2); 

 Calendar curr = calBegin;

 while (curr.before(calEnd)) { 

 result.add(sdf.format(curr.getTime()));

curr.add(Calendar.MONTH, 1); 

 } 

 } catch (Exception e) { 

 e.printStackTrace(); 

 }

 return result; } 

 /** * 获取指定日期范围的所有周的周一~周日,格式为周一~周日,如2016-09-19为周一,2016-09-25为周天,返回结果为[2016-09-19 * ~2016-09-25] * * @param startDate * 起始时间(2016-09-19) * @param endDate * 结束时间(2016-09-25) * @return * @throws ParseException */

 public static ListgetWeeksBetween(String startDate, String endDate) { 

 // startDate不能大于endDate 

 Listresult = new ArrayList(); 

 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 设置时间格式 

 try {

 Calendar calBegin = Calendar.getInstance(); // 使用给定的 Date 设置此 Calendar 的时间 Date begin = sdf.parse(startDate); calBegin.setTime(begin);

 int dayWeek = calBegin.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天 

 if (1 == dayWeek) { calBegin.add(Calendar.DAY_OF_MONTH, -1); } calBegin.setFirstDayOfWeek(Calendar.MONDAY);// 设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一 

 Calendar calEnd = Calendar.getInstance(); // 使用给定的 Date 设置此 Calendar 的时间 Date end = sdf.parse(endDate); 

 calEnd.setTime(end); // 测试此日期是否在指定日期之后 

 while (end.after(calBegin.getTime())) { // 根据日历的规则,为给定的日历字段添加或减去指定的时间量 calBegin.add(Calendar.DAY_OF_MONTH, 1); 

 int day = calBegin.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天 calBegin.add(Calendar.DATE, calBegin.getFirstDayOfWeek() - day);// 根据日历的规则,给当前日期减去星期几与一个星期第一天的差值

 // 获取周一

 String monday = sdf.format(calBegin.getTime());

 calBegin.add(Calendar.DATE, 6);

 // 获取周日 

 String sunday = sdf.format(calBegin.getTime()); result.add(monday + "~"

  • sunday); } 

 } catch (Exception e) {

 e.printStackTrace();

 }

 return result;

 } 

 /** * 获取当前日期的前index天(以当前日期开始计算) index = -6表示当天日期的前7天 index = -29表示当前日期的前30天 * * @return */

 public static String getLastIndexDayStr(int index) { 

 Date now = new Date(); 

 Calendar c = Calendar.getInstance(); 

 c.setTime(now); 

 c.add(Calendar.DAY_OF_MONTH, index); 

 return dateToStr(c.getTime(), "yyyy-MM-dd"); 

 } 

 /** * 获取当天 */

 public static String getTodayStr() {

 Date now = new Date(); 

 return dateToStr(now, "yyyy-MM-dd"); } 

 /** * 获取最近3个月开始日期(包含当前月所有月) * * @return */ 

 public static String getLast3MonthsStartDateStr() { 

 Date now = new Date(); Calendar c = Calendar.getInstance(); c.setTime(now); c.add(Calendar.MONTH, -2); 

 return dateToStr(c.getTime(), "yyyy-MM") + "-01"; 

 }

 /** * 获取星期几 * @param dateStr * @return */

 public static String getWeekNameOfDate(String dateStr) {

 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

 Calendar c = Calendar.getInstance(); 

 try { 

 c.setTime(format.parse(dateStr)); 

 } catch (ParseException e) {

 e.printStackTrace(); } 

 int dayForWeek = 0;

 if (c.get(Calendar.DAY_OF_WEEK) == 1) { 

 dayForWeek = 7; 

 } else { 

 dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;

 }

 String result = ""; 

 switch (dayForWeek) {

 case 1: result = "星期一"; break; 

 case 2: result = "星期二"; break;

 case 3: result = "星期三"; break; 

 case 4: result = "星期四"; break; 

 case 5: result = "星期五"; break; 

 case 6: result = "星期六"; break; 

 case 7: result = "星期天"; break; } 

 return result; } 

 /** * 获取指定日期所在月的第一天 * * @param date * @return yyyy-MM-dd */

 public static String getMonthStartDate(String date) { 

 try { 

 Calendar calendar = Calendar.getInstance();

 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); calendar.setTime(format.parse(date));

 calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMinimum(Calendar.DAY_OF_MONTH));

 return format.format(calendar.getTime()); } 

catch (ParseException e) {

 e.printStackTrace(); } 

 return null; }

 /** * 获取指定日期所在月的最后一天 * * @param date * @return yyyy--MM-dd */

 public static String getMonthEndDate(String date) {

 try {

 Calendar calendar = Calendar.getInstance(); 

 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); calendar.setTime(format.parse(date));

 calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMaximum(Calendar.DAY_OF_MONTH));

 return format.format(calendar.getTime()); 

 } catch (ParseException e) {

 e.printStackTrace(); }

 return null; } 

 /** * 获取指定日期所在周的周一 * @param date * @return yyyy-MM-dd */ 

 public static String getMondayOfDate(String date) { 

 try { 

 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

 // 设置时间格式 

 Calendar cal = Calendar.getInstance(); cal.setTime(sdf.parse(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 sdf.format(cal.getTime());

 } catch (ParseException e) { 

 e.printStackTrace(); } 

 return null; } 

 /** * 获取指定日期所在周的周日 * @param date * @return yyyy-MM-dd */ 

 public static String getSundayOfDate(String date) { 

 try {

 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 设置时间格式 Calendar cal = Calendar.getInstance(); 

 cal.setTime(sdf.parse(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);//根据日历的规则,给当前日期减去星期几与一个星期第一天的差值 

 cal.add(Calendar.DATE, 6); 

 return sdf.format(cal.getTime()); 

 } catch (ParseException e) {

 e.printStackTrace(); }

 return null; } 

 [java] view plain copy** * 获取月末到当前时间的秒数 */

 public static long getSecondsBetweenEndMothToNow() { 

 Calendar c = Calendar.getInstance(); 

 c = Calendar.getInstance(); 

 c.add(Calendar.MONTH, 1); 

 c.set(Calendar.DAY_OF_MONTH, 0); 

 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

 String lastday = format.format(c.getTime());

 lastday = lastday + " 23:59:59"; 

 Date last = null; long sec = 0;

 try { last = format.parse(lastday); 

 long diff = last.getTime() - System.currentTimeMillis();

 sec = diff / 1000;

 } catch (ParseException e) {

 e.printStackTrace();

 } return sec; }

本文由10bet手机官网发布于面向对象,转载请注明出处:java得到几天前和几天后时间,DateUtil时间工具类

上一篇:没有了 下一篇:没有了
猜你喜欢
热门排行
精彩图文