您的位置:首页 > 其它

关于date处理的一个util,日期格式化,自定义格式化,日期计算

2016-08-19 11:26 459 查看
工作中用到的一些方法,由于经常使用,就自己整理了一个util类,当用到相关方法时,可以直接调用,方便了很多,大家可以直接拿去扔进项目里,里面注释挺详细的,方便阅读代码和直接使用。当然了,也可以根据自己的需求修改里面的方法,在使用的过程中不断的往里添加常用方法,自己再慢慢完善吧,都挺简单的。好了,代码奉上。

package com.classes.util;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import com.framework.util.ParamUtils;

/**
*
* @annotation 日期工具类,以前写的很随意,今天整理一下尽量做到规范,方便使用。如有不方便理解的注释,请自行修改。
* @Notice Please use the UTF-8 encoding format to read this utility class !
* 日期模板编写参数说明:年(yyyy)、月(MM)、日(dd)、24小时制时(HH)/12小时制时(hh)、分(mm)、秒(ss)、毫秒(SSS)、12小时制上下午(a)、周(E)
* @author GRC
* @foundTime 整理于2016-08-12
*/

public class DateUtil {

/**
*
* @annotation 格式化日期时间,将date参数格式化为字符串:年-月-日 时:分:秒
* @author GRC
* @foundTime 2016-08-12
* @param date Date类型参数
* @return string类型日期时间
*/
public static String format(Date date) {
if(date==null) return null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String s = sdf.format(date);
return s;
}

/**
*
* @annotation 格式化日期时间,将字符串参数格式化为date:年-月-日 时:分:秒
* @author GRC
* @foundTime 2016-08-12
* @param s String格式的日期参数
* @return date
*/
public static Date parse(String s) {
if(!ParamUtils.chkString(s)) return null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = null;
try {
date = sdf.parse(s);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}

/**
*
* @annotation 自定义模板格式化日期时间,将date参数格式化为字符串
* @author GRC
* @foundTime 2016-08-12
* @param date Date类型参数
* @param formatString 用户自定义格式化模板,建议模板:"yyyy-MM-dd HH:mm:ss";"yyyy-MM-dd";"yyyy/MM/dd HH:mm:ss";"yyyy/MM/dd"
* @return string类型日期时间,根据用户自定义模板返回
*/
public static String format(Date date, String formatString) {
if(date==null) return null;
SimpleDateFormat sdf = new SimpleDateFormat(formatString);
String s = sdf.format(date);
return s;
}

/**
*
* @annotation 自定义模板格式化日期时间,将字符串参数格式化为date。
* @author GRC
* @foundTime 2016-08-12
* @param dateString String格式的日期参数
* @param dateFormatString 用户根据要格式化的字符串自行编写的格式化模板。
* @return date
*/
public static Date parse(String dateString, String dateFormatString) {
if(!ParamUtils.chkString(dateString)) return null;
SimpleDateFormat sdf = new SimpleDateFormat(dateFormatString);
Date date = null;
try {
date = sdf.parse(dateString);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}

/**
*
* @annotation 获取自定义日期毫秒数
* @author GRC
* @foundTime 2016-08-12
* @param date
* @return 毫秒数
*/
public static long getMillionSeconds(Date date) {
long millionSeconds = date.getTime();
return millionSeconds;
}

/**
*
* @annotation 获取系统当前毫秒数
* @author GRC
* @foundTime 2016-08-12
* @return 系统当前毫秒数(long类型)
*/
public static long getMillionSeconds() {
long millionSeconds = System.currentTimeMillis();
return millionSeconds;
}

/**
*
* @annotation 计算两个日期时间之间相差的小时数,时间参数符合相应模板格式,time1时间需在time2之前
* @author GRC
* @foundTime 2016-08-12
* @param time1 字符串格式的时间参数
* @param time2 字符串格式的时间参数
* @return 相差小时数
*/
public static int subTime(String time1, String time2) {
int result = 0;
Timestamp d1 = Timestamp.valueOf(time1);
Timestamp d2 = Timestamp.valueOf(time2);
result = (int) ((d2.getTime() - d1.getTime()) / 1000 / 60 / 60);
return result;
}

/**
*
* @annotation 获取指定年月的天数,亦可用作获取指定年月的最大天数或最后一天
* @author GRC
* @foundTime 2016-08-12
* @param year 年
* @param month 月
* @return 指定年月的天数
*/
public static int getDaysOfMonth(int year, int month) {
Calendar c1 = new GregorianCalendar();
Calendar c2 = new GregorianCalendar();
c1.clear();
c1.set(Calendar.YEAR, month == 12 ? (year + 1) : year);
c1.set(Calendar.MONTH, month == 12 ? 1 : month);
c1.set(Calendar.DAY_OF_MONTH, 1);
long time1 = DateUtil.getMillionSeconds(c1.getTime());
c2.clear();
c2.set(Calendar.YEAR, year);
c2.set(Calendar.MONTH, month-1);
c2.set(Calendar.DAY_OF_MONTH, 1);
c2.getTime();
long time2 = DateUtil.getMillionSeconds(c2.getTime());
return (int) ((time1 - time2) / (24 * 60 * 60 * 1000));
}

/**
*
* @annotation 获得日期的结束时刻
* @author GRC
* @foundTime 2016-08-12
* @param date 指定日期参数
* @return 获得指定日期结束时刻的日期
*/
public static Date endOfDay(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
return c.getTime();
}

/**
*
* @annotation 获得日期的开始时刻
* @author GRC
* @foundTime 2016-08-12
* @param date 指定日期参数
* @return 获得指定日期开始时刻的日期
*/
public static Date beginOfDay(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
return c.getTime();
}

/**
*
* @annotation 计算截止日期
* @author GRC
* @foundTime 2016-08-12
* @param date 生效日期
* @param year 几年后失效,如果不用此参数请设置成 0
* @param month 几月后失效,如果不用此参数请设置成 0
* @param days 几天后失效,如果不用此参数请设置成 0
* @param now 是否到达截止日期当天失效
* @return 失效时间
*/
public static Date calDeadLine(Date date, int year, int month, int days, boolean now) {
Calendar c = Calendar.getInstance();
c.setTime(date);
if (year!=0) c.add(Calendar.YEAR, year) ;
if (month!=0) c.add(Calendar.MONTH, month) ;
if (days!=0) c.add(Calendar.DAY_OF_MONTH, days) ;
if (now) {
return DateUtil.beginOfDay(c.getTime());
} else {
return DateUtil.endOfDay(c.getTime());
}
}

/**
*
* @annotation 自定义date,输入的参数一定要符合日期格式
* @author GRC
* @foundTime 2016-08-12
* @param year 年
* @param month 月
* @param day 日
* @param hours 时
* @param minutes 分
* @param seconds 秒
* @return date
*/
public static Date customDate(int year, int month, int day, int hours, int minutes, i
4000
nt seconds ) {
Calendar c = new GregorianCalendar();
c.clear();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, month-1);
c.set(Calendar.DAY_OF_MONTH, day);
c.set(Calendar.HOUR_OF_DAY, hours);
c.set(Calendar.MINUTE, minutes);
c.set(Calendar.SECOND, seconds);
return c.getTime();
}

/**
*
* @annotation 获取时间格式的字符串
* @author GRC
* @foundTime 2016-08-12
* @return 时间格式字符串
*/
public static String getDateID() {
SimpleDateFormat dateform = new SimpleDateFormat("yyyyMMddhhmmssSSS");
GregorianCalendar calendar = new GregorianCalendar();
String s = dateform.format(calendar.getTime());
return s;
}

/**
*
* @annotation 根据Date对象取得【年份】整数形式
* @author GRC
* @foundTime 2016-08-12
* @param date 日期参数
* @return int型年份
*/
public static int getRightYear(Date date) {
Calendar rightYear = Calendar.getInstance();
if (date != null) {
rightYear.setTime(date);
}
return rightYear.get(Calendar.YEAR);
}

/**
*
* @annotation 根据Date对象取得【月份】整数形式
* @author GRC
* @foundTime 2016-08-12
* @param date 日期参数
* @return int型月份
*/
public static int getRightMonth(Date date) {
Calendar rightMonth = Calendar.getInstance();
if (date != null) {
rightMonth.setTime(date);
}
return rightMonth.get(Calendar.MONTH);
}

/**
*
* @annotation 根据Date对象取得【天】整数形式
* @author GRC
* @foundTime 2016-08-12
* @param date 日期参数
* @return int型天
*/
public static int getRightDay(Date date) {
Calendar rightDay = Calendar.getInstance();
if (date != null) {
rightDay.setTime(date);
}
return rightDay.get(Calendar.DAY_OF_MONTH);
}

/**
*
* @annotation 根据Date对象取得【小时】整数形式
* @author GRC
* @foundTime 2016-08-12
* @param date 日期参数
* @return int型小时
*/
public static int getRightHour(Date date) {
Calendar rightHour = Calendar.getInstance();
if (date != null) {
rightHour.setTime(date);
}
return rightHour.get(Calendar.HOUR_OF_DAY);
}

/**
*
* @annotation 根据Date对象取得【分钟】整数形式
* @author GRC
* @foundTime 2016-08-12
* @param date 日期参数
* @return int型分钟
*/
public static int getRightMinute(Date date) {
Calendar rightMinute = Calendar.getInstance();
if (date != null) {
rightMinute.setTime(date);
}
return rightMinute.get(Calendar.MINUTE);
}

/**
*
* @annotation 根据Date对象取得【秒】整数形式
* @author GRC
* @foundTime 2016-08-12
* @param date 日期参数
* @return int型秒
*/
public static int getRightSecond(Date date) {
Calendar rightSecond = Calendar.getInstance();
if (date != null) {
rightSecond.setTime(date);
}
return rightSecond.get(Calendar.SECOND);
}

/**
*
* @annotation 查询日期是星期几
* @author GRC
* @foundTime 2016-08-12
* @param date 指定的查询日期
* @return week
*/
public static String getRight(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
String week = sdf.format(date);
return week;
}

/**
*
* @annotation 计算两个日期之间相差的天数
* @author GRC
* @foundTime 2016-08-12
* @param g1 日期参数
* @param g2 日期参数
* @return 两个日期相差天数
*/
public static int getDays(GregorianCalendar g1, GregorianCalendar g2) {
int elapsed = 0;
GregorianCalendar gc1, gc2;
if (g2.after(g1)) {
gc2 = (GregorianCalendar) g2.clone();
gc1 = (GregorianCalendar) g1.clone();
} else {
gc2 = (GregorianCalendar) g1.clone();
gc1 = (GregorianCalendar) g2.clone();
}
gc1.clear(Calendar.MILLISECOND);
gc1.clear(Calendar.SECOND);
gc1.clear(Calendar.MINUTE);
gc1.clear(Calendar.HOUR_OF_DAY);
gc2.clear(Calendar.MILLISECOND);
gc2.clear(Calendar.SECOND);
gc2.clear(Calendar.MINUTE);
gc2.clear(Calendar.HOUR_OF_DAY);
while (gc1.before(gc2)) {
gc1.add(Calendar.DATE, 1);
elapsed++;
}
return elapsed;
}

public static void main(String[] args) {
System.out.println(DateUtil.getDaysOfMonth(2016, 8));
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息