/**
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package test;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
public class DateUtil {
/**
* 获取YYYY格式
*
* @return
*/
public static String getYear() {
return formatDate(new Date(), "yyyy");
}
/**
* 获取YYYY格式
*
* @return
*/
public static String getYear(Date date) {
return formatDate(date, "yyyy");
}
/**
* 获取上个月YYYY-MM格式
*
* @return
*/
public static String getLastMonthDate() {
Calendar calendar = Calendar.getInstance();//日历对象
calendar.setTime(new Date());//设置当前日期
calendar.add(Calendar.MONTH, -1);
String date = DateUtil.formatDate(calendar.getTime(), "yyyy-MM");
return date;
}
/**
* 获取指定月的天数
*
* @return
*/
public static int getMonthOfDays(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int count = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
return count;
}
/**
* 获取YYYY-MM-DD格式
*
* @return
*/
public static String getDay() {
return formatDate(new Date(), "yyyy-MM-dd");
}
/**
* 获取YYYY-MM-DD格式
*
* @return
*/
public static String getDay(Date date) {
return formatDate(date, "yyyy-MM-dd");
}
/**
* 获取YYYYMMDD格式
*
* @return
*/
public static String getDays() {
return formatDate(new Date(), "yyyyMMdd");
}
/**
* 获取YYYYMMDD格式
*
* @return
*/
public static String getDays(Date date) {
return formatDate(date, "yyyyMMdd");
}
/**
* 获取YYYY-MM-DD HH:mm:ss格式
*
* @return
*/
public static String getTime() {
return formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
}
/**
* 获取YYYY-MM-DD HH:mm:ss.SSS格式
*
* @return
*/
public static String getMsTime() {
return formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS");
}
/**
* 获取YYYYMMDDHHmmss格式
*
* @return
*/
public static String getAllTime() {
return formatDate(new Date(), "yyyyMMddHHmmss");
}
/**
* 获取YYYY-MM-DD HH:mm:ss格式
*
* @return
*/
public static String getTime(Date date) {
return formatDate(date, "yyyy-MM-dd HH:mm:ss");
}
public static String formatDate(Date date, String pattern) {
String formatDate = null;
if (StringUtils.isNotBlank(pattern)) {
formatDate = DateFormatUtils.format(date, pattern);
} else {
formatDate = DateFormatUtils.format(date, "yyyy-MM-dd");
}
return formatDate;
}
/**
* 获取YYYY-MM-DD HH:mm:ss:SSS格式
*
* @return
*/
public static String getCurrentSSS(Date date) {
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
//格式化时间 年月日 时分秒毫秒
SimpleDateFormat formatterSSS = new SimpleDateFormat("yyyyMMddHHmmssSSS");
String currentSSS = formatterSSS.format(date);
return currentSSS;
}
/**
* 获取时间的小时值
*
* @return
*/
public static int getHourOfDay(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.HOUR_OF_DAY);
}
/**
* @Title: compareDate
* @Description:(日期比较,如果s>=e 返回true 否则返回false)
* @param s
* @param e
* @return boolean
* @throws
* @author luguosui
*/
public static boolean compareDate(String s, String e) {
if (parseDate(s) == null || parseDate(e) == null) {
return false;
}
return parseDate(s).getTime() >= parseDate(e).getTime();
}
/**
* @Title: compareDate
* @Description:(日期比较,如果s>=e 返回true 否则返回false)
* @param s
* @param e
* @return boolean
* @throws
* @author luguosui
*/
public static boolean compareDate2(String s, String e) {
if (parseTime(s) == null || parseTime(e) == null) {
return false;
}
return parseTime(s).getTime() >= parseTime(e).getTime();
}
/**
* 格式化日期
*
* @return
*/
public static Date parseDate(String date) {
return parse(date,"yyyy-MM-dd");
}
/**
* 格式化日期
*
* @return
*/
public static Date parseTime(String date) {
return parse(date,"yyyy-MM-dd HH:mm:ss");
}
/**
* 格式化日期
*
* @return
*/
public static Date parse(String date, String pattern) {
try {
return DateUtils.parseDate(date,pattern);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
/**
* 格式化日期
*
* @return
*/
public static String format(Date date, String pattern) {
return DateFormatUtils.format(date, pattern);
}
/**
* 把日期转换为Timestamp
*
* @param date
* @return
*/
public static Timestamp format(Date date) {
return new Timestamp(date.getTime());
}
/**
* 校验日期是否合法
*
* @return
*/
public static boolean isValidDate(String s) {
return parse(s, "yyyy-MM-dd HH:mm:ss") != null;
}
/**
* 校验日期是否合法
*
* @return
*/
public static boolean isValidDate(String s, String pattern) {
return parse(s, pattern) != null;
}
public static int getDiffYear(String startTime, String endTime) {
DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
try {
int years = (int) (((fmt.parse(endTime).getTime() - fmt.parse(
startTime).getTime()) / (1000 * 60 * 60 * 24)) / 365);
return years;
} catch (Exception e) {
// 如果throw java.text.ParseException或者NullPointerException,就说明格式不对
return 0;
}
}
/**
* <li>功能描述:时间相减得到天数
*
* @param beginDateStr
* @param endDateStr
* @return long
* @author Administrator
*/
public static int getDaySub(String beginDateStr, String endDateStr) {
long day = 0;
SimpleDateFormat format = new SimpleDateFormat(
"yyyy-MM-dd");
Date beginDate = null;
Date endDate = null;
try {
beginDate = format.parse(beginDateStr);
endDate = format.parse(endDateStr);
} catch (ParseException e) {
e.printStackTrace();
}
day = (endDate.getTime() - beginDate.getTime()) / (24 * 60 * 60 * 1000);
// System.out.println("相隔的天数="+day);
return (int)day;
}
/**
* <li>功能描述:时间相减得到小时差值
*
* @param beginTime
* @param endTime
* @return long
* @author Administrator
*/
public static double getHourSub(String beginTime, String endTime) {
double hour = 0.0;
SimpleDateFormat format = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
Date beginDate = null;
Date endDate = null;
try {
beginDate = format.parse(beginTime);
endDate = format.parse(endTime);
} catch (ParseException e) {
e.printStackTrace();
}
hour = ((double)endDate.getTime() - (double)beginDate.getTime()) / (60 * 60 * 1000);
double hour2 = new BigDecimal(hour).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
return hour2;
}
/**
* <li>功能描述:时间相减得到小时差值
*
* @param beginTime
* @param endTime
* @return long
* @author Administrator
*/
public static double getHourSub(Date beginDate, Date endDate) {
double hour = 0.0;
SimpleDateFormat format = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
hour = ((double)endDate.getTime() - (double)beginDate.getTime()) / (60 * 60 * 1000);
double hour2 = new BigDecimal(hour).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
return hour2;
}
/**
* <li>功能描述:时间相减得到分钟差值
*
* @param beginTime
* @param endTime
* @return long
* @author
*/
public static double getMinuteSub(String beginTime, String endTime) {
double minute = 0.0;
SimpleDateFormat format = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
Date beginDate = null;
Date endDate = null;
try {
beginDate = format.parse(beginTime);
endDate = format.parse(endTime);
} catch (ParseException e) {
e.printStackTrace();
}
minute = ((double)endDate.getTime() - (double)beginDate.getTime()) / (60 * 1000);
double minute2 = new BigDecimal(minute).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
return minute2;
}
/**
* <li>功能描述:时间相减得到分钟差值
*
* @param beginTime
* @param endTime
* @return long
* @author
*/
public static double getMinuteSub(Date beginDate, Date endDate) {
double minute = 0.0;
SimpleDateFormat format = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
minute = ((double)endDate.getTime() - (double)beginDate.getTime()) / (60 * 1000);
double minute2 = new BigDecimal(minute).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
return minute2;
}
/**
* <li>功能描述:时间相减得到秒钟差值
*
* @param beginTime
* @param endTime
* @return long
* @author
*/
public static double getSecondSub(String beginTime, String endTime) {
double second = 0.0;
SimpleDateFormat format = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
Date beginDate = null;
Date endDate = null;
try {
beginDate = format.parse(beginTime);
endDate = format.parse(endTime);
} catch (ParseException e) {
e.printStackTrace();
}
second = ((double)endDate.getTime() - (double)beginDate.getTime()) / (1000);
double second2 = new BigDecimal(second).setScale(0,BigDecimal.ROUND_HALF_UP).doubleValue();
return second2;
}
/**
* <li>功能描述:时间相减得到秒钟差值
*
*
* @return long
* @author
*/
public static double getSecondSub(Date beginDate, Date endDate) {
double second = 0.0;
second = ((double)endDate.getTime() - (double)beginDate.getTime()) / (1000);
double second2 = new BigDecimal(second).setScale(0,BigDecimal.ROUND_HALF_UP).doubleValue();
return second2;
}
/**
* 得到今日的n天之后的日期 yyyy-MM-dd
*
* @param n
* @return
*/
public static String getNDay(int n) {
Date date=new Date();//取当前时间
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
calendar.add(calendar.DATE,n);
date=calendar.getTime();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
return formatter.format(date);
}
/**
* 得到n天之后的日期 yyyy-MM-dd
*
* @param n
* @param date
* @return
*/
public static String getNDay(int n,Date date) {
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
calendar.add(calendar.DATE,n);
date=calendar.getTime();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
return formatter.format(date);
}
/**
* 得到n天之后的日期 yyyy-MM-dd
*
* @param n
* @param dateStr
* @return
*/
public static String getNDay(int n,String dateStr) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = new GregorianCalendar();
try {
calendar.setTime(formatter.parse(dateStr));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
calendar.add(calendar.DATE,n);
Date date = calendar.getTime();
return formatter.format(date);
}
/**
* 得到n天之后的日期 yyyy-MM-dd HH:mm:ss
*
* @param days
* @return
*/
public static String getAfterDayDate(String days) {
int daysInt = Integer.parseInt(days);
Calendar canlendar = Calendar.getInstance(); // java.util包
canlendar.add(Calendar.DATE, daysInt); // 日期减 如果不够减会将月变动
Date date = canlendar.getTime();
SimpleDateFormat sdfd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateStr = sdfd.format(date);
return dateStr;
}
/**
* 得到上个月最后一天的日期
*
* @param days
* @return
*/
public static String getLastMonth() {
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
Calendar cale = Calendar.getInstance();
cale.set(Calendar.DAY_OF_MONTH,0);
String date = format.format(cale.getTime());
return date;
}
/**
* 得到n小时之后的日期 yyyy-MM-dd HH:mm:ss
*
* @param date
* @param n
* @return
*/
public static String getNHour(Date date, int n) {
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
calendar.add(calendar.HOUR,n);
date=calendar.getTime();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return formatter.format(date);
}
/**
* @一只耳儿
* 得到n分钟之后的日期 yyyy-MM-dd HH:mm:ss
*
* @param date
* @param n
* @return String yyyy-MM-dd HH:mm:ss
*/
public static String getNMinute(Date date, int n) {
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
calendar.add(calendar.MINUTE,n);
date=calendar.getTime();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return formatter.format(date);
}
/**
* 得到日期转化周几
*
* @param days
* @return
*/
public static String getWeekDays(String days) {
Date date = parseDate(days);
String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
Calendar cal = Calendar.getInstance();
cal.setTime(date);
date = cal.getTime();
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0)
w = 0;
return weekDays[w];
}
/**
* 得到n天之后是周几
*
* @param days
* @return
*/
public static String getWeekDays(String days,int n) {
Date date = parseDate(days);
String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, n);
date = cal.getTime();
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0)
w = 0;
return weekDays[w];
}
/**
* 得到n天之后是周几
*
* @param days
* @return
*/
public static String getAfterDayWeek(String days) {
int daysInt = Integer.parseInt(days);
Calendar canlendar = Calendar.getInstance(); // java.util包
canlendar.add(Calendar.DATE, daysInt); // 日期减 如果不够减会将月变动
Date date = canlendar.getTime();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String dateStr = sdf.format(date);
return dateStr;
}
// /**
// * 格式化Oracle Date
// * @param value
// * @return
// */
// public static String buildDateValue(Object value){
// if(Func.isOracle()){
// return "to_date('"+ value +"','yyyy-mm-dd HH24:MI:SS')";
// }else{
// return Func.toStr(value);
// }
// }
/**
*获取某段时间内的所有日期 (Date类型)
* @param dBegin
* @param dEnd
* @return
*/
public static List<Date> findDates(Date dBegin, Date dEnd)
{
List lDate = new ArrayList();
lDate.add(dBegin);
Calendar calBegin = Calendar.getInstance();
// 使用给定的 Date 设置此 Calendar 的时间
calBegin.setTime(dBegin);
Calendar calEnd = Calendar.getInstance();
// 使用给定的 Date 设置此 Calendar 的时间
calEnd.setTime(dEnd);
// 测试此日期是否在指定日期之后
while (dEnd.after(calBegin.getTime()))
{
// 根据日历的规则,为给定的日历字段添加或减去指定的时间量
calBegin.add(Calendar.DAY_OF_MONTH, 1);
lDate.add(calBegin.getTime());
}
return lDate;
}
/**
* 获取某段时间内的所有日期 (Sting 类型)
* @param dBegin
* @param dEnd
* @return
*/
public static List<String> findStringDates(Date dBegin, Date dEnd)
{
List lDate = new ArrayList();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String begin = sdf.format(dBegin);
lDate.add(begin);
Calendar calBegin = Calendar.getInstance();
// 使用给定的 Date 设置此 Calendar 的时间
calBegin.setTime(dBegin);
Calendar calEnd = Calendar.getInstance();
// 使用给定的 Date 设置此 Calendar 的时间
calEnd.setTime(dEnd);
// 测试此日期是否在指定日期之后
while (dEnd.after(calBegin.getTime()))
{
// 根据日历的规则,为给定的日历字段添加或减去指定的时间量
calBegin.add(Calendar.DAY_OF_MONTH, 1);
String after =sdf.format(calBegin.getTime());
lDate.add(after);
}
return lDate;
}
/**
* 获取指定月份的所有日期
* @param month
* @return
*/
public static List<String> findStringDatesByMonth(int year,int month){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
Date begin = null;
Date end = null;
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, month);
c.set(Calendar.DATE, 1);
begin = c.getTime();
c.add(Calendar.MONTH, 1);
c.add(Calendar.DATE, -1);
end = c.getTime();
return findStringDates(begin,end);
}
/**
* 获取指定年月的第一天和最后一天日期
* @param monthDate 2018-09
* @return
*/
public static Map<String,String> findDateByYearMonth(String monthDate){
String[] split = monthDate.split("-");
int year = Integer.parseInt(split[0]);
int month = Integer.parseInt(split[1]);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR,year);
c.set(Calendar.MONTH, month-1);
c.set(Calendar.DAY_OF_MONTH,1);
Date begin = c.getTime();
c.set(Calendar.YEAR,year);
c.set(Calendar.MONTH, month);
c.set(Calendar.DAY_OF_MONTH,0);
Date end = c.getTime();
Map<String, String> map = new HashMap<String,String>();
map.put("beginDate", sdf.format(begin));
map.put("endDate", sdf.format(end));
return map;
}
/**
* 功能描述:获取系统当前所处的月份
* @param: []
* @return: int
* @auther:
* @date: 2018/10/15 11:13
*/
public static int getMonth(){
Calendar c = Calendar.getInstance();
return c.get(Calendar.MONTH)+1;
}
/**
* @Title: compareDateTime
* @Description:(日期时间比较,如果s>=e 返回true 否则返回false)
* @param s
* @param e
* @return boolean
* @throws
* @author
*/
public static boolean compareDateTime(String s, String e) {
if (parseTime(s) == null || parseTime(e) == null) {
return false;
}
return parseTime(s).getTime() >= parseTime(e).getTime();
}
public static void main(String[] args) {
//System.out.println(getMinuteSub("2018-8-13 10:35:32","2018-8-13 10:33:32"));
BigDecimal f = new BigDecimal("2.224667").setScale(0, BigDecimal.ROUND_CEILING);
System.out.println(f);
}
}
DateUtil.java
最新推荐文章于 2023-12-10 11:00:00 发布