项目中 比较常用的 工具类--日历,封装一下,可以 获取 比如:本周的起始点,本月的起始点,本季度的起始点等
DateUtil.class
主要用到的方法有:
Calendar.add() : 对当前 日历进行 加减 ;SimpleDateFormat.format() :日期格式化
eg1:
Calendar c = Calendar.getInstance();
try {
int weekday = c.get(Calendar.DAY_OF_WEEK) - 2 ;
int gapDay = weekday >= 0 ? weekday : 6;
c.add(Calendar.DATE, -gapDay);
c.setTime(longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00"));
} catch (Exception e) {
e.printStackTrace();
}
return c.getTime();
直接上代码:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateUtil {
private static SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
private static SimpleDateFormat longHourSdf = new SimpleDateFormat("yyyy-MM-dd HH");
private static SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
/**
* 获得本周的第一天,周一
*
* @return
*/
public static Date getCurrentWeekDayStartTime() {
Calendar c = Calendar.getInstance();
try {
int weekday = c.get(Calendar.DAY_OF_WEEK) - 2 ;
int gapDay = weekday >= 0 ? weekday : 6;
c.add(Calendar.DATE, -gapDay);
c.setTime(longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00"));
} catch (Exception e) {
e.printStackTrace();
}
return c.getTime();
}
/**
* 获得本周的最后一天,周日
*
* @return
*/
public static Date getCurrentWeekDayEndTime() {
Calendar c = Calendar.getInstance();
try {
int weekday = 8 - c.get(Calendar.DAY_OF_WEEK);
int gapDay = weekday <= 6 ? weekday : 0;
c.add(Calendar.DATE, gapDay);
c.setTime(longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59"));
} catch (Exception e) {
e.printStackTrace();
}
return c.getTime();
}
/**
* 获得本天的开始时间,即2012-01-01 00:00:00
*
* @return
*/
public static Date getCurrentDayStartTime() {
Date now = new Date();
try {
now = shortSdf.parse(shortSdf.format(now));
} catch (Exception e) {
e.printStackTrace();
}
return now;
}
/**
* 获得本天的结束时间,即2012-01-01 23:59:59
*
* @return
*/
public static Date getCurrentDayEndTime() {
Date now = new Date();
try {
now = longSdf.parse(shortSdf.format(now) + " 23:59:59");
} catch (Exception e) {
e.printStackTrace();
}
return now;
}
/**
* 获得本小时的开始时间,即2012-01-01 23:59:59
*
* @return
*/
public static Date getCurrentHourStartTime() {
Date now = new Date();
try {
now = longHourSdf.parse(longHourSdf.format(now));
} catch (Exception e) {
e.printStackTrace();
}
return now;
}
/**
* 获得本小时的结束时间,即2012-01-01 23:59:59
*
* @return
*/
public static Date getCurrentHourEndTime() {
Date now = new Date();
try {
now = longSdf.parse(longHourSdf.format(now) + ":59:59");
} catch (Exception e) {
e.printStackTrace();
}
return now;
}
/**
* 获得上个月第一天的开始时间
* @return
*/
public static Date getPreviousMonthFirst(){
Calendar c = Calendar.getInstance();
Date d=null;
try {
c.set(5, 1);
c.add(2, -1);
d=shortSdf.parse(shortSdf.format(c.getTime()));
} catch (ParseException e) {
e.printStackTrace();
}
return d;
}
/**
* 获得上个月最后一天结束时间
* @return
*/
public static Date getPreviousMonthLast(){
Calendar c = Calendar.getInstance();
Date d=null;
try {
c.add(2, -1);
c.set(5, 1);
c.roll(5, -1);
d = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59");
} catch (ParseException e) {
e.printStackTrace();
}
return d;
}
/**
* 获得本月的开始时间,即2012-01-01 00:00:00
*
* @return
*/
public static Date getCurrentMonthStartTime() {
Calendar c = Calendar.getInstance();
Date now = null;
try {
c.set(Calendar.DATE, 1);
now = shortSdf.parse(shortSdf.format(c.getTime()));
} catch (Exception e) {
e.printStackTrace();
}
return now;
}
/**
* 当前月的结束时间,即2012-01-31 23:59:59
*
* @return
*/
public static Date getCurrentMonthEndTime() {
Calendar c = Calendar.getInstance();
Date now = null;
try {
c.set(Calendar.DATE, 1);
c.add(Calendar.MONTH, 1);
c.add(Calendar.DATE, -1);
now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59");
} catch (Exception e) {
e.printStackTrace();
}
return now;
}
/**
* 当前年的开始时间,即2012-01-01 00:00:00
*
* @return
*/
public static Date getCurrentYearStartTime() {
Calendar c = Calendar.getInstance();
Date now = null;
try {
c.set(Calendar.MONTH, 0);
c.set(Calendar.DATE, 1);
now = shortSdf.parse(shortSdf.format(c.getTime()));
} catch (Exception e) {
e.printStackTrace();
}
return now;
}
/**
* 当前年的结束时间,即2012-12-31 23:59:59
*
* @return
*/
public static Date getCurrentYearEndTime() {
Calendar c = Calendar.getInstance();
Date now = null;
try {
c.set(Calendar.MONTH, 11);
c.set(Calendar.DATE, 31);
now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59");
} catch (Exception e) {
e.printStackTrace();
}
return now;
}
/**
* 当前季度的开始时间,即2012-01-1 00:00:00
*
* @return
*/
public static Date getCurrentQuarterStartTime() {
Calendar c = Calendar.getInstance();
int currentMonth = c.get(Calendar.MONTH) + 1;
Date now = null;
try {
if (currentMonth >= 1 && currentMonth <= 3)
c.set(Calendar.MONTH, 0);
else if (currentMonth >= 4 && currentMonth <= 6)
c.set(Calendar.MONTH, 3);
else if (currentMonth >= 7 && currentMonth <= 9)
c.set(Calendar.MONTH, 6);
else if (currentMonth >= 10 && currentMonth <= 12)
c.set(Calendar.MONTH, 9);
c.set(Calendar.DATE, 1);
now = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00");
} catch (Exception e) {
e.printStackTrace();
}
return now;
}
/**
* 当前季度的结束时间,即2012-03-31 23:59:59
*
* @return
*/
public static Date getCurrentQuarterEndTime() {
Calendar c = Calendar.getInstance();
int currentMonth = c.get(Calendar.MONTH) + 1;
Date now = null;
try {
if (currentMonth >= 1 && currentMonth <= 3) {
c.set(Calendar.MONTH, 2);
c.set(Calendar.DATE, 31);
} else if (currentMonth >= 4 && currentMonth <= 6) {
c.set(Calendar.MONTH, 5);
c.set(Calendar.DATE, 30);
} else if (currentMonth >= 7 && currentMonth <= 9) {
c.set(Calendar.MONTH, 8);
c.set(Calendar.DATE, 30);
} else if (currentMonth >= 10 && currentMonth <= 12) {
c.set(Calendar.MONTH, 11);
c.set(Calendar.DATE, 31);
}
now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59");
} catch (Exception e) {
e.printStackTrace();
}
return now;
}
/**
* 得到某年某周的第一天
*
* @param year
* @param week
* @return
*/
public static Date getFirstDayOfWeek(int year, int week) {
week = week - 1;
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, Calendar.JANUARY);
calendar.set(Calendar.DATE, 1);
Calendar cal = (Calendar) calendar.clone();
cal.add(Calendar.DATE, week * 7);
return getFirstDayOfWeek(cal.getTime());
}
/**
* 得到某年某周的最后一天
*
* @param year
* @param week
* @return
*/
public static Date getLastDayOfWeek(int year, int week) {
week = week - 1;
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, Calendar.JANUARY);
calendar.set(Calendar.DATE, 1);
Calendar cal = (Calendar) calendar.clone();
cal.add(Calendar.DATE, week * 7);
return getLastDayOfWeek(cal.getTime());
}
/**
* 取得当前日期所在周的第一天
*
* @param date
* @return
*/
public static Date getFirstDayOfWeek(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setFirstDayOfWeek(Calendar.SUNDAY);
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_WEEK,
calendar.getFirstDayOfWeek()); // Sunday
return calendar.getTime();
}
/**
* 取得当前日期所在周的最后一天
*
* @param date
* @return
*/
public static Date getLastDayOfWeek(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setFirstDayOfWeek(Calendar.SUNDAY);
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek() + 6); // Saturday
Date now =null;
try {
now= longSdf.parse(shortSdf.format(calendar.getTime()) + " 23:59:59");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return now;
}
/**
* 取得当前日期所在周的前一周最后一天
*
* @param date
* @return
*/
public static Date getLastDayOfLastWeek(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return getLastDayOfWeek(calendar.get(Calendar.YEAR), calendar.get(Calendar.WEEK_OF_YEAR) - 1);
}
/**
* 返回指定日期的月的第一天
*
* @param year
* @param month
* @return
*/
public static Date getFirstDayOfMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH), 1);
Date now=null;
try {
now= longSdf.parse(shortSdf.format(calendar.getTime()) + " 00:00:00");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return now;
}
/**
* 返回指定年月的月的第一天
*
* @param year
* @param month
* @return
*/
public static Date getFirstDayOfMonth(Integer year, Integer month) {
Calendar calendar = Calendar.getInstance();
if (year == null) {
year = calendar.get(Calendar.YEAR);
}
if (month == null) {
month = calendar.get(Calendar.MONTH);
}
calendar.set(year, month, 1);
return calendar.getTime();
}
/**
* 返回指定日期的月的最后一天
*
* @param year
* @param month
* @return
*/
public static Date getLastDayOfMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), 1);
calendar.roll(Calendar.DATE, -1);
Date now =null;
try {
now= longSdf.parse(shortSdf.format(calendar.getTime()) + " 23:59:59");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return now;
}
/**
* 返回指定年月的月的最后一天
*
* @param year
* @param month
* @return
*/
public static Date getLastDayOfMonth(Integer year, Integer month) {
Calendar calendar = Calendar.getInstance();
if (year == null) {
year = calendar.get(Calendar.YEAR);
}
if (month == null) {
month = calendar.get(Calendar.MONTH);
}
calendar.set(year, month, 1);
calendar.roll(Calendar.DATE, -1);
Date now =null;
try {
now= longSdf.parse(shortSdf.format(calendar.getTime()) + " 23:59:59");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return now;
}
/**
* 返回指定日期的上个月的最后一天
*
* @param year
* @param month
* @return
*/
public static Date getLastDayOfLastMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH) - 1, 1);
calendar.roll(Calendar.DATE, -1);
Date now =null;
try {
now= longSdf.parse(shortSdf.format(calendar.getTime()) + " 23:59:59");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return now;
}
/**
* 返回指定日期的季的第一天
*
* @param year
* @param quarter
* @return
*/
public static Date getFirstDayOfQuarter(Date date) {
Calendar calendar = Calendar.getInstance();
Date d= getFirstDayOfQuarter(calendar.get(Calendar.YEAR),getQuarterOfYear(date));
calendar.setTime(d);
Date now=null;
try {
d=shortSdf.parse(shortSdf.format(calendar.getTime()));
now = longSdf.parse(shortSdf.format(d.getTime()) + " 00:00:00");
} catch (ParseException e) {
e.printStackTrace();
}
return now;
}
/**
* 返回指定年季的季的第一天
*
* @param year
* @param quarter
* @return
*/
public static Date getFirstDayOfQuarter(Integer year, Integer quarter) {
Calendar calendar = Calendar.getInstance();
Integer month = new Integer(0);
if (quarter == 1) {
month = 1 - 1;
} else if (quarter == 2) {
month = 4 - 1;
} else if (quarter == 3) {
month = 7 - 1;
} else if (quarter == 4) {
month = 10 - 1;
} else {
month = calendar.get(Calendar.MONTH);
}
return getFirstDayOfMonth(year, month);
}
/**
* 返回指定日期的季的最后一天
*
* @param year
* @param quarter
* @return
*/
public static Date getLastDayOfQuarter(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
Date d=getLastDayOfQuarter(calendar.get(Calendar.YEAR), getQuarterOfYear(date));
Date now =null;
try {
now= longSdf.parse(shortSdf.format(d.getTime()) + " 23:59:59");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return now;
}
/**
* 返回指定年季的季的最后一天
*
* @param year
* @param quarter
* @return
*/
public static Date getLastDayOfQuarter(Integer year, Integer quarter) {
Calendar calendar = Calendar.getInstance();
Integer month = new Integer(0);
if (quarter == 1) {
month = 3 - 1;
} else if (quarter == 2) {
month = 6 - 1;
} else if (quarter == 3) {
month = 9 - 1;
} else if (quarter == 4) {
month = 12 - 1;
} else {
month = calendar.get(Calendar.MONTH);
}
Date d=getLastDayOfMonth(year, month);
Date now =null;
try {
now= longSdf.parse(shortSdf.format(calendar.getTime()) + " 23:59:59");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return now;
}
/**
* 返回指定日期的上一季的最后一天
*
* @param year
* @param quarter
* @return
*/
public static Date getLastDayOfLastQuarter(Date date) {
Calendar calendar = Calendar.getInstance();
Date d= getLastDayOfLastQuarter(calendar.get(Calendar.YEAR),getQuarterOfYear(date));
try {
calendar.setTime(d);
d=longSdf.parse(shortSdf.format(calendar.getTime()) + " 23:59:59");
} catch (ParseException e) {
e.printStackTrace();
}
return d;
}
/**
* 返回指定年季的上一季的最后一天
*
* @param year
* @param quarter
* @return
*/
public static Date getLastDayOfLastQuarter(Integer year, Integer quarter) {
Calendar calendar = Calendar.getInstance();
Integer month = new Integer(0);
if (quarter == 1) {
month = 12 - 1;
} else if (quarter == 2) {
month = 3 - 1;
} else if (quarter == 3) {
month = 6 - 1;
} else if (quarter == 4) {
month = 9 - 1;
} else {
month = calendar.get(Calendar.MONTH);
}
Date d=getLastDayOfMonth(year, month);
Date now =null;
try {
now= longSdf.parse(shortSdf.format(calendar.getTime()) + " 23:59:59");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return now;
}
/**
* 返回指定日期的季度
*
* @param date
* @return
*/
public static int getQuarterOfYear(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.MONTH) / 3 + 1;
}
/**
* 获取前/后半年的开始时间
*
* @return
*/
public static Date getHalfYearStartTime() {
Calendar c = Calendar.getInstance();
int currentMonth = c.get(Calendar.MONTH) + 1;
Date now = null;
try {
if (currentMonth >= 1 && currentMonth <= 6) {
c.set(Calendar.MONTH, 0);
} else if (currentMonth >= 7 && currentMonth <= 12) {
c.set(Calendar.MONTH, 6);
}
c.set(Calendar.DATE, 1);
now = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00");
} catch (Exception e) {
e.printStackTrace();
}
return now;
}
/**
* 获取前/后半年的结束时间
*
* @return
*/
public static Date getHalfYearEndTime() {
Calendar c = Calendar.getInstance();
int currentMonth = c.get(Calendar.MONTH) + 1;
Date now = null;
try {
if (currentMonth >= 1 && currentMonth <= 6) {
c.set(Calendar.MONTH, 5);
c.set(Calendar.DATE, 30);
} else if (currentMonth >= 7 && currentMonth <= 12) {
c.set(Calendar.MONTH, 11);
c.set(Calendar.DATE, 31);
}
now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59");
} catch (Exception e) {
e.printStackTrace();
}
return now;
}
public static Date getLastYearStart(){
Calendar calendar = Calendar.getInstance();
Date date = getCurrentYearStartTime();
calendar.setTime(date);
calendar.add(Calendar.YEAR, -1);
date = calendar.getTime();
return date;
}
public static Date getLastYearEnd(){
Calendar calendar = Calendar.getInstance();
Date date =getCurrentYearEndTime();
calendar.setTime(date);
calendar.add(Calendar.YEAR, -1);
date = calendar.getTime();
return date;
}
//获取指定日期的后一天时间
public static String getSpecifiedDayAfter(String specifiedDay){
Calendar c = Calendar.getInstance();
Date date=null;
try {
date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);
} catch (ParseException e) {
e.printStackTrace();
}
c.setTime(date);
int day=c.get(Calendar.DATE);
c.set(Calendar.DATE,day+1);
String dayAfter=new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
return dayAfter;
}
/**
* 获取开始时间毫秒值
*/
public static long getStartTime(String v,long endTime){
long startTime = 0;
switch(v){
case "m5":
startTime = endTime - 60*1000*5;
break;
case "m15":
startTime = endTime - 60*1000*15;
break;
case "m30":
startTime = endTime - 60*1000*30;
break;
case "h1":
startTime = endTime - 60*1000*60;
break;
case "h12":
startTime = endTime - 60*1000*60*12;
break;
case "d1":
startTime = endTime - 60*1000*60*24;
break;
case "d7":
startTime = endTime - 60*1000*60*24*7;
break;
case "d15":
startTime = endTime - 60*1000*60*24*15;
break;
default:startTime = endTime - 60*1000*60*24*30;}
return startTime;
}
public static void main(String[] args) {
}
}