package com.mall.common;

 import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List; import org.apache.log4j.Logger; public class DateUtils {
private static Logger log=Logger.getLogger(DateUtils.class); /**
* 获取昨天日期
*
* @param date
* @return
*/
public static String getYesterDay(Date date) { java.text.Format formatter = new java.text.SimpleDateFormat(
"yyyy-MM-dd"); long beforeTime = (date.getTime() / 1000) - 24 * 60 * 60; date.setTime(beforeTime * 1000); return formatter.format(date); } /**
* 获取昨天日期(返回数值)
*
* @param date
* @return num
*/
public static Integer getYesterDayNum(Date date) { java.text.Format formatter = new java.text.SimpleDateFormat("yyyyMMdd"); long beforeTime = (date.getTime() / 1000) - 24 * 60 * 60; date.setTime(beforeTime * 1000); return Integer.parseInt(formatter.format(date)); } /**
* 获取一周前的日期(当前日期往前推7天)
*
* @param date
* @return
*/
public static String getWeekdayBeforeDate(Date date) { java.text.Format formatter = new java.text.SimpleDateFormat(
"yyyy-MM-dd"); long beforeTime = (date.getTime() / 1000) - 24 * 60 * 60 * 7; date.setTime(beforeTime * 1000); return formatter.format(date); } /**
* 获取一周前的日期(当前日期往前推7天)(返回数值)
*
* @param date
* @return
*/
public static Integer getWeekdayBeforeDateNum(Date date) { java.text.Format formatter = new java.text.SimpleDateFormat("yyyyMMdd"); long beforeTime = (date.getTime() / 1000) - 24 * 60 * 60 * 7; date.setTime(beforeTime * 1000); return Integer.parseInt(formatter.format(date)); } /**
* 获取一月前的日期(当前日期往前推30天)
*
* @param date
* @return
*/
public static String getMonthBeforeDate(Date date) { java.text.Format formatter = new java.text.SimpleDateFormat(
"yyyy-MM-dd"); long beforeTime = (date.getTime() / 1000) - 24 * 60 * 60 * 30; date.setTime(beforeTime * 1000); return formatter.format(date); } /**
* 获取一月前的日期(当前日期往前推30天)(返回数值)
*
* @param date
* @return
*/
public static Integer getMonthBeforeDateNum(Date date) { java.text.Format formatter = new java.text.SimpleDateFormat("yyyyMMdd"); long beforeTime = (date.getTime() / 1000) - 24 * 60 * 60 * 30; date.setTime(beforeTime * 1000); return Integer.parseInt(formatter.format(date)); } /**
* 获取三月前的日期(当前日期往前推90天)
*
* @param date
* @return
*/
public static String get3MonthBeforeDate(Date date) { java.text.Format formatter = new java.text.SimpleDateFormat(
"yyyy-MM-dd"); long beforeTime = (date.getTime() / 1000) - 24 * 60 * 60 * 30 * 3; date.setTime(beforeTime * 1000); String d = formatter.format(date);
return d;
} /**
* 获取三月前的日期(当前日期往前推30天)(返回数值)
*
* @param date
* @return
*/
public static Integer get3MonthBeforeDateNum(Date date) { java.text.Format formatter = new java.text.SimpleDateFormat("yyyyMMdd"); long beforeTime = (date.getTime() / 1000) - 24 * 60 * 60 * 30 * 3; date.setTime(beforeTime * 1000); return Integer.parseInt(formatter.format(date)); } /**
* 获取一年后的日期
*
* @return
*/
public static String getNextYear(int chooseYear, Date date) { java.text.Format formatter = new java.text.SimpleDateFormat(
"yyyy-MM-dd"); long beforeTime = (date.getTime() / 1000) + 60 * 60 * 24 * 365
* chooseYear; date.setTime(beforeTime * 1000); return formatter.format(date);
} /**
* 日期转换成字符串
*
* @param date
* @return
*/
public static String convertDateToStr(Date date) {
java.text.Format formatter = new java.text.SimpleDateFormat(
"yyyy-MM-dd");
return formatter.format(date); } /**
* 日期转换成字符串 yyyy-MM-dd HHmmss
*
* @param date
* @return
*/
public static String convertTimeToStr(Date date) {
java.text.Format formatter = new java.text.SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
return formatter.format(date); } /**
* 将日期转为数值
*
* @param date
* @return
*/
public static Integer convertDateToNum(Date date) {
java.text.Format formatter = new java.text.SimpleDateFormat("yyyyMMdd");
return Integer.parseInt(formatter.format(date));
}
/**
* 将字符串日期转为数值
*
* @param date
* @return
*/
public static Integer convertStrToNum(String date) { if (date.contains("-")) {
date = date.replace("-", "");
} else if (date.contains(".")) {
date = date.replace(".", "");
} else if (date.contains("/")) {
date = date.replace("/", "");
} return Integer.parseInt(date);
} /**
* 时间转换器 第一个参数 要转化的数据类型 --- java.util.Date 第二个参数 要转化的数据 --- "2010-12-12"
*
*/
public static Date convertStrTODate( String str,Class<Date> type,String datePattern) { if (str == null) {
return null;
} else {
if (type == java.util.Date.class) {
if (str instanceof String) {
try {
SimpleDateFormat sdf = new SimpleDateFormat(datePattern);
return sdf.parse(str);
} catch (ParseException e) {
throw new RuntimeException("您输入的数据格式不对");
}
} else {
throw new RuntimeException("您要转化的数据输入不是String类型");
}
} else {
throw new RuntimeException("您要转化的数据类型不对");
}
}
} /**
* 根据生日计算年龄
*
* @param birthDay
* @return
* @throws Exception
*/
public static int getAge(Date birthDay) throws Exception {
Calendar cal = Calendar.getInstance(); if (cal.before(birthDay)) {
throw new IllegalArgumentException(
"The birthDay is before Now.It's unbelievable!");
}
int yearNow = cal.get(Calendar.YEAR);
int monthNow = cal.get(Calendar.MONTH);
int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
cal.setTime(birthDay); int yearBirth = cal.get(Calendar.YEAR);
int monthBirth = cal.get(Calendar.MONTH);
int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH); int age = yearNow - yearBirth; if (monthNow <= monthBirth) {
if (monthNow == monthBirth) {
// monthNow==monthBirth
if (dayOfMonthNow < dayOfMonthBirth) {
age--;
} else {
// do nothing
}
} else {
// monthNow>monthBirth
age--;
}
} else {
// monthNow<monthBirth
// donothing
} return age;
}
///////////////////////////////////////根据时间段获取时间的集合//////////////////////////////////////////////////////////////////
public static List<String> getDateListBydates(String s1,String s2,String format) throws ParseException{
List<String>list=new ArrayList<String>();
//String s1 = "2012-02-01";
//String s2 = "2012-04-04";
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date begin=sdf.parse(s1);
Date end=sdf.parse(s2);
double between=(end.getTime()-begin.getTime())/1000;//除以1000是为了转换成秒
double day=between/(24*3600);
for(int i = 0;i<=day;i++){
Calendar cd = Calendar.getInstance();
cd.setTime(sdf.parse(s1));
cd.add(Calendar.DATE, i);//增加一天
//cd.add(Calendar.MONTH, n);//增加一个月
log.info(sdf.format(cd.getTime()));
list.add(sdf.format(cd.getTime()));
}
return list;
}
//获取指定年月的总天数
public static int getLastDay(int year, int month) {
int day = 1;
Calendar cal = Calendar.getInstance();
cal.set(year,month - 1,day);
int last = cal.getActualMaximum(Calendar.DATE);
System.out.println(last);
return last;
}
//获取指定年月的日期
@SuppressWarnings("unchecked")
public static List<String> getDatesByMonth(int year, int month){
List<String> list=new ArrayList();
String yyyy=year+"";
String mm=month+"";
String dd="01";
if(month<10){
mm="0"+month;
}
int num=getLastDay(year, month);
for(int i=1;i<=num;i++){
if(i<10){
dd="0"+i;
}else{
dd=i+"";
}
list.add(yyyy+"-"+mm+"-"+dd);
System.out.println(yyyy+"-"+mm+"-"+dd);
}
return list;
}
/**
*
* @param datestr 解析字符串 如2014-04-1716:38:57
* @param sourceDateformat 源日期格式 如yyyy-MM-ddHH:mm:ss
* @param formatStr 要转换的日期格式 如(yyyy-MM-dd HH:mm:ss)
* @return
*/
public static String strTOdateTOStr(String datestr,String sourceDateformat,String targetDateformat){
// String str="2014-04-1716:38:57";
try {
SimpleDateFormat sourceFormat = new SimpleDateFormat(sourceDateformat);
SimpleDateFormat targetFormat = new SimpleDateFormat(targetDateformat);
return targetFormat.format(sourceFormat.parse(datestr));
} catch (Exception e) {
log.info("strTOdateTOStr:"+e);
}
return null; }
///////////////////////////////////////////////////////////////////////////////////////////////////////// public static void main(String[] args) throws Exception{
//getDatesByMonth(2013,2);
getDateListBydates("20130517","20130519","yyyyMMdd"); } }

最新文章

  1. 【BZOJ】3835: [Poi2014]Supercomputer
  2. 如何提高nodejs程序的稳定性,健壮性
  3. Surface Shader
  4. OC基础数据类型-NSData
  5. [转]Linux下的暴力密码破解工具Hydra详解
  6. VSTO安装部署(完美解决XP+2007)
  7. ubuntu中使用nginx把本地80端口转到其他端口
  8. Oracle 搜集统计信息
  9. 针对苹果最新审核要求为应用兼容IPv6-备用
  10. js类方法,对象方法,原型的理解(转)
  11. iOS 如何创建单例对象
  12. 宽客的人&amp;amp;&amp;amp;事件映射
  13. Phonegap开发相关问题
  14. class not found: org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
  15. Winform系列——好看的DataGridView折叠控件
  16. 解决tab标签页,相同id时切换失灵的问题
  17. Ubuntu中VisualBox无法识别USB设备
  18. CSS样式总结(作业六)
  19. 奇异分解(SVD)
  20. (使用STL自带的排序功能进行排序7.3.2)POJ 2092 Grandpa is Famous(结构体排序)

热门文章

  1. npm设置仓库
  2. [转载]java正则表达式
  3. CentOS怎样安装Python3.6
  4. 使用peach工具进行fuzz测试
  5. cousera 吴恩达 深度学习 第一课 第二周 作业 过拟合的表现
  6. 20155226 2016-2017-2 《Java程序设计》第8周学习总结
  7. Linux进程间通信——使用信号量(转)
  8. Spring AOP 实现读写分离
  9. Lucene/Solr搜索引擎开发笔记 - 写作方向调整
  10. 64位windows 2003和windows xp