日期处理的方法
来源:互联网 发布:华为网盘网络错误 编辑:程序博客网 时间:2024/04/28 22:13
public class DateUtil {
/**
* @fields SDFF : yyyy-MM-dd HH:mm:ss 格式的日期格式
*/
public static SimpleDateFormat SDFF=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
/**
* @fields SDF : yyyy-MM-dd格式的日期格式
*/
public static SimpleDateFormat SDF=new SimpleDateFormat("yyyy-MM-dd");
/**
* @title getLastDayOfMonth
* @description 操作工具类
* @param year
* @param month
* @return int 某年某月的最后一天
*/
@SuppressWarnings("static-access")
public static Integer getLastDayOfMonth(Integer year, Integer month) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR,year);
cal.set(Calendar.MONTH,month-1);
cal.set(Calendar.DATE,1);
// 当前月+1,即下个月
cal.add(cal.MONTH, 1);
// 将下个月1号作为日期初始zhii
cal.set(cal.DATE, 1);
// 下个月1号减去一天,即得到当前月最后一天
cal.add(cal.DATE, -1);
return cal.get(Calendar.DAY_OF_MONTH);
}
/**
* @title getWeekDayOfDate
* @description 获得星期几
* @param date
* @return 汉字的 一二三四五六日
*/
public static String getWeekDayOfDate(Date date){
Calendar calendar=Calendar.getInstance();
calendar.setTime(date);
int weekint=calendar.get(Calendar.DAY_OF_WEEK);
String weekday="一";
if(weekint==2){
weekday="一";
}
if(weekint==3){
weekday="二";
}
if(weekint==4){
weekday="三";
}
if(weekint==5){
weekday="四";
}
if(weekint==6){
weekday="五";
}
if(weekint==7){
weekday="六";
}
if(weekint==1){
weekday="日";
}
return weekday;
}
/**
* @title getMonthOfDate
* @description 返回日期中月份
* @param date
* @return int 日期中月份
*/
public static int getMonthOfDate(Date date){
Calendar calendar=Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.MONTH)+1;
}
/**
* @title getDateByStringDate
* @description 根据“yyyy-MM-dd”获得日期值
* @param dateString
* @return Date
* @throws ParseException
*/
public static Date getDateByStringDate(String dateString) throws ParseException{
return SDF.parse(dateString);
}
/**
* @title getDateByStringDate
* @description 根据“yyyy-MM-dd”获得日期值
* @param dateString
* @return Timestamp
* @throws ParseException
*/
public static Timestamp getTimestampByStringDate(String dateString){
Timestamp time=null;
try{
time=new Timestamp(SDF.parse(dateString).getTime());
}catch(Exception ex){
throw new RuntimeException(ex);
}
return time;
}
/**
* @title getFormatDate
* @description 根据“yyyy-MM-dd”获得日期
* @param dateString
* @return Timestamp
* @throws ParseException
*/
public static Timestamp getFormatDate(Date date){
Timestamp time=null;
try{
time=new Timestamp(SDF.parse(SDF.format(date)).getTime());
}catch(Exception ex){
throw new RuntimeException(ex);
}
return time;
}
/**
* @title getDaysFromDate
* @description 求的两个日期间天数(带绝对值的)
* @param maxdate
* @param mindate
* @return 天数
*/
public static int getDaysFromDate(Date maxdate,Date mindate){
try {
long maxtimes;
maxtimes = SDF.parse(SDF.format(maxdate)+" 00:00:00").getTime();
long mintimes=SDF.parse(SDF.format(mindate)+" 00:00:00").getTime();
SDF.format(mindate);
long times=Math.abs(maxtimes-mintimes);
long time=times/86400000;
return Integer.parseInt(time+"");
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
/**
* @title getDaysFromDate
* @description 求的两个日期间天数(不带绝对值的)
* @param maxdate
* @param mindate
* @return 天数
*/
public static int getDaysFromDateNoABS(Date maxdate,Date mindate){
try {
long maxtimes;
maxtimes = SDF.parse(SDF.format(maxdate)+" 00:00:00").getTime();
long mintimes=SDF.parse(SDF.format(mindate)+" 00:00:00").getTime();
SDF.format(mindate);
long times=maxtimes-mintimes;
long time=times/86400000;
return Integer.parseInt(time+"");
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
/**
* @title getStringDateFromDate
* @description 将string 类型转换为'yyyy-MM-dd'
* @param dateString
* @return String 'yyyy-MM-dd'
*/
public static String getStringDateFromDate(String dateString){
try {
return SDF.parse(dateString).toString();
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
/**
* @title getDateFromDateAndNumber
* @description 根据初始日期加上天数,计算新的日期
* @param date
* @param num
* @return Date
*/
public static Date getDateFromDateAndNumber(Date date,int num){
GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
gc.setTime(date);
gc.add(5, num);
return gc.getTime();
}
/**
* @title getYearFromDate
* @description 获取日期的年份
* @param date
* @return
*/
public static int getYearFromDate(Date date){
Calendar gc = Calendar.getInstance();
gc.setTime(date);
return gc.get(Calendar.YEAR);
}
/**
* @title getDate
* @description 根据年月日获取统一格式的日期
* @param year
* @param month
* @param day
* @return Date
*/
public static Date getDate(int year,int month,int day){
Calendar gc = Calendar.getInstance();
gc.set(year, month-1, day);
Date date = null;
try {
date = SDF.parse(SDF.format(gc.getTime()));
} catch (ParseException e) {
throw new RuntimeException(e);
}
return date;
}
}
/**
* @fields SDFF : yyyy-MM-dd HH:mm:ss 格式的日期格式
*/
public static SimpleDateFormat SDFF=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
/**
* @fields SDF : yyyy-MM-dd格式的日期格式
*/
public static SimpleDateFormat SDF=new SimpleDateFormat("yyyy-MM-dd");
/**
* @title getLastDayOfMonth
* @description 操作工具类
* @param year
* @param month
* @return int 某年某月的最后一天
*/
@SuppressWarnings("static-access")
public static Integer getLastDayOfMonth(Integer year, Integer month) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR,year);
cal.set(Calendar.MONTH,month-1);
cal.set(Calendar.DATE,1);
// 当前月+1,即下个月
cal.add(cal.MONTH, 1);
// 将下个月1号作为日期初始zhii
cal.set(cal.DATE, 1);
// 下个月1号减去一天,即得到当前月最后一天
cal.add(cal.DATE, -1);
return cal.get(Calendar.DAY_OF_MONTH);
}
/**
* @title getWeekDayOfDate
* @description 获得星期几
* @param date
* @return 汉字的 一二三四五六日
*/
public static String getWeekDayOfDate(Date date){
Calendar calendar=Calendar.getInstance();
calendar.setTime(date);
int weekint=calendar.get(Calendar.DAY_OF_WEEK);
String weekday="一";
if(weekint==2){
weekday="一";
}
if(weekint==3){
weekday="二";
}
if(weekint==4){
weekday="三";
}
if(weekint==5){
weekday="四";
}
if(weekint==6){
weekday="五";
}
if(weekint==7){
weekday="六";
}
if(weekint==1){
weekday="日";
}
return weekday;
}
/**
* @title getMonthOfDate
* @description 返回日期中月份
* @param date
* @return int 日期中月份
*/
public static int getMonthOfDate(Date date){
Calendar calendar=Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.MONTH)+1;
}
/**
* @title getDateByStringDate
* @description 根据“yyyy-MM-dd”获得日期值
* @param dateString
* @return Date
* @throws ParseException
*/
public static Date getDateByStringDate(String dateString) throws ParseException{
return SDF.parse(dateString);
}
/**
* @title getDateByStringDate
* @description 根据“yyyy-MM-dd”获得日期值
* @param dateString
* @return Timestamp
* @throws ParseException
*/
public static Timestamp getTimestampByStringDate(String dateString){
Timestamp time=null;
try{
time=new Timestamp(SDF.parse(dateString).getTime());
}catch(Exception ex){
throw new RuntimeException(ex);
}
return time;
}
/**
* @title getFormatDate
* @description 根据“yyyy-MM-dd”获得日期
* @param dateString
* @return Timestamp
* @throws ParseException
*/
public static Timestamp getFormatDate(Date date){
Timestamp time=null;
try{
time=new Timestamp(SDF.parse(SDF.format(date)).getTime());
}catch(Exception ex){
throw new RuntimeException(ex);
}
return time;
}
/**
* @title getDaysFromDate
* @description 求的两个日期间天数(带绝对值的)
* @param maxdate
* @param mindate
* @return 天数
*/
public static int getDaysFromDate(Date maxdate,Date mindate){
try {
long maxtimes;
maxtimes = SDF.parse(SDF.format(maxdate)+" 00:00:00").getTime();
long mintimes=SDF.parse(SDF.format(mindate)+" 00:00:00").getTime();
SDF.format(mindate);
long times=Math.abs(maxtimes-mintimes);
long time=times/86400000;
return Integer.parseInt(time+"");
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
/**
* @title getDaysFromDate
* @description 求的两个日期间天数(不带绝对值的)
* @param maxdate
* @param mindate
* @return 天数
*/
public static int getDaysFromDateNoABS(Date maxdate,Date mindate){
try {
long maxtimes;
maxtimes = SDF.parse(SDF.format(maxdate)+" 00:00:00").getTime();
long mintimes=SDF.parse(SDF.format(mindate)+" 00:00:00").getTime();
SDF.format(mindate);
long times=maxtimes-mintimes;
long time=times/86400000;
return Integer.parseInt(time+"");
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
/**
* @title getStringDateFromDate
* @description 将string 类型转换为'yyyy-MM-dd'
* @param dateString
* @return String 'yyyy-MM-dd'
*/
public static String getStringDateFromDate(String dateString){
try {
return SDF.parse(dateString).toString();
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
/**
* @title getDateFromDateAndNumber
* @description 根据初始日期加上天数,计算新的日期
* @param date
* @param num
* @return Date
*/
public static Date getDateFromDateAndNumber(Date date,int num){
GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
gc.setTime(date);
gc.add(5, num);
return gc.getTime();
}
/**
* @title getYearFromDate
* @description 获取日期的年份
* @param date
* @return
*/
public static int getYearFromDate(Date date){
Calendar gc = Calendar.getInstance();
gc.setTime(date);
return gc.get(Calendar.YEAR);
}
/**
* @title getDate
* @description 根据年月日获取统一格式的日期
* @param year
* @param month
* @param day
* @return Date
*/
public static Date getDate(int year,int month,int day){
Calendar gc = Calendar.getInstance();
gc.set(year, month-1, day);
Date date = null;
try {
date = SDF.parse(SDF.format(gc.getTime()));
} catch (ParseException e) {
throw new RuntimeException(e);
}
return date;
}
}
- 日期处理的方法
- 常用的日期时间处理方法
- JAVA处理日期时间的方法介绍
- java日期时间的处理方法集合
- java处理日期相关的方法
- 关于oc日期相关处理的方法
- JS处理日期的方法总结
- PHP 处理日期的常用方法
- Postgresql_常用的几个日期处理方法
- Extjs 中处理日期的方法总结
- BeanUtils的populate方法之日期处理
- java处理日期时间的方法
- 日期处理方法加减法
- SQL日期处理方法
- 日期处理方法大全
- 常用日期处理方法
- FLEX日期处理方法
- FLEX日期处理方法
- Eclipse 空心J 的问题
- 四舍五入的公用方法
- 电阻屏工作原理--ADS7843的使用
- 删除文件工具类
- 建立信号基站
- 日期处理的方法
- wyyx面试
- 记之
- ubuntu下配置qt+opengl+opencv
- sitemesh入门(一)
- C语言的32个关键字(整理)
- 社保公积金转移参照
- struts2文件上传类型限制 之 zip和rar文件类型
- 常用API大全