您的位置:首页 > 其它

joda-time-2.9.4.jar

2017-08-10 00:00 399 查看
package com.jzfq.fms.common.util;
import com.jzfq.fms.common.common.PageVo;
import com.jzfq.fms.common.util.DateEnum;
import com.jzfq.fms.common.util.ServiceValidate;

import org.apache.commons.lang.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.Days;
import org.joda.time.LocalDate;
import org.joda.time.Months;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

/**

@author 时间计算工具类
*/
public final class DateUtils {

/**

获取当前时间

@return 返回java Date

@author
*/
public static Date getNow() {
return new DateTime().toDate();
}

public static Date truncate(Date date, int field){
return org.apache.commons.lang.time.DateUtils.truncate(date, field);
}

/**

获取当前时间

@return 返回JODA DateTime

@author
*/
public static DateTime getDateTimeNow() {
return new DateTime();
}

public static DateTime dateToDateTime(Date date) {
return new DateTime(date);
}

/**

字符串时间 转换为时间

@param date

@return

@author
*/
public static Date toDate(String date, DateEnum format) {
ServiceValidate.notNull(format);
DateTimeFormatter fmt = DateTimeFormat.forPattern(format.getText());
return fmt.parseDateTime(date.trim()).toDate();
}

/**

毫秒转换为Date

@param ms

@return

@author
*/
public static Date msToDate(long ms) {
ServiceValidate.notNull(ms);
return new DateTime(ms).toDate();
}

/**

格式化时间

@param date 时间

@param template 格式

@return

@author
*/
public static String dateToStr(Date date, DateEnum template) {
ServiceValidate.notNull(date);
ServiceValidate.notNull(template);
SimpleDateFormat format = new SimpleDateFormat(template.getText());
return format.format(date);
}

/**

传入日期增加几天

@param date

@param day

@return

@author
*/
public static Date plusDay(Date date, int day) {
ServiceValidate.notNull(date);
return new DateTime(date).plusDays(day).toDate();
}

/**

传入日期增加几个月

@param date

@param

@return

@author
*/
public static Date plusMonth(Date date, int month) {
ServiceValidate.notNull(month);
return new DateTime(date).plusMonths(month).toDate();
}

/**

传入日期减少几天

@param date

@param day

@return

@author
*/
public static Date minusDay(Date date, int day) {
ServiceValidate.notNull(date);
return new DateTime(date).minusDays(day).toDate();
}

/**

传入日期减少月数

@param date

@param month

@return
*/
public static Date minusMonth(Date date, int month) {
ServiceValidate.notNull(date);
return new DateTime(date).minusMonths(month).toDate();
}

/**

传入日期减少几分钟

@param date

@param

@return
*/
public static Date minusMinutes(Date date, int minutes) {
ServiceValidate.notNull(date);
return new DateTime(date).minusMinutes(minutes).toDate();
}

public static Date longToDate(long millis) {
return new DateTime(millis).toDate();
}

/**

返回传入两个date相差的天数

END 大于 start 忽略时分秒

例:start = 2015-06-05 10:40:30

end = 2015-06-06 10:40:20

返回值 1

@param start

@param end

@return
*/
public static int daysBetween(Date start, Date end) {
int days = Days.daysBetween(new LocalDate(start), new LocalDate(end)).getDays();
return days;
}

/**

返回两个日期之间 月数

@param start

@param end

@return

@author
*/
public static int monthsBetween(Date start, Date end) {
return monthsBetween(new DateTime(start), new DateTime(end));
}

/**

返回两个日期之间 月数

@param start

@param end

@return

@author
*/
public static int monthsBetween(DateTime start, DateTime end) {
int m = Months.monthsBetween(start, end).getMonths();
return m;
}

/**

返回当前日期距离N月之后之后的天数

<p>

例如:计算1月5日,距离2个月之后的3月5日 返回相差天数

@param date

@param addmonths

@return
*/
public static int daysAfterMonths(Date date, int addmonths) {
DateTime dateTime = new DateTime(date);
DateTime dateTimeAfter = dateTime.plusMonths(addmonths);
return daysBetween(date, dateTimeAfter.toDate());
}

/**

返回传入日期+N月之后的日期

@param date

@param addmonths

@return
*/
public static Date dateAfterMonths(Date date, int addmonths) {
DateTime dateTime = new DateTime(date);
DateTime dateTimeAfter = dateTime.plusMonths(addmonths);
return dateTimeAfter.toDate();
}

public static Date handleDateMix(Date date) {
String str = DateUtils.dateToStr(date, DateEnum.DATE_SIMPLE) + " 00:00:00";
return DateUtils.toDate(str, DateEnum.DATE_FORMAT);
}

public static Date handleDateMax(Date date) {
String str = DateUtils.dateToStr(date, DateEnum.DATE_SIMPLE) + " 23:59:59";
return DateUtils.toDate(str, DateEnum.DATE_FORMAT);
}

/**

传入日期和当前日期比较(参数会转换成年月日格式)

@param dt

@return 小于-1,等于0,大于1
*/
public static int compareDateToNow(DateTime dt) {
return dt.toLocalDate().compareTo(new DateTime().toLocalDate());
}

/**

返回当前时间格式yyyyMMdd字符串

@return
*/
public static String getSimpleDate() {
return dateToStr(getNow(), DateEnum.DATE_SIMPLE_MIN);
}

/**

返回当前时间格式HHmmss字符串

@return
*/
public static String getMinTime() {
return dateToStr(getNow(), DateEnum.DATE_TIME_MIN);
}

/**

@param date

@return 当前月第几天
*/
public static int getDayOfMonth(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
return c.get(Calendar.DAY_OF_MONTH);
}

public static Date getDate(String str, DateEnum format) {
SimpleDateFormat sdf = new SimpleDateFormat(format.getText());
Date date = null;
try {
date = sdf.parse(str);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
/**

日期处理30天工具类,天数大于30天和开始时间大于结束时间,有前端处理
*/

public static PageVo getNewOrderTimeAndOrderTimeEnd(Map<String, String[]> map) {
String orderTime = null;
String orderTimeEnd = null;
PageVo pageVo = new PageVo();
for (Map.Entry<String, String[]> entry : map.entrySet()) {
if (entry.getValue() != null) {
if (entry.getKey().equals("orderTime")) {//开始时间不为空
orderTime = entry.getValue()[0];
}
if (entry.getKey().equals("orderTimeEnd")) {//开始时间不为空
orderTimeEnd = entry.getValue()[0];
}
pageVo.getParameters().put(entry.getKey(), entry.getValue()[0]);
}
}
if (StringUtils.isBlank(orderTime) && StringUtils.isBlank(orderTimeEnd)) {
orderTimeEnd = getSimpleDate();//如果不选择时间,默认导出最近30天的
orderTime = dateToStr(plusMonth(new Date(), -1), DateEnum.DATE_SIMPLE);

}
if (StringUtils.isBlank(orderTime) && StringUtils.isNotBlank(orderTimeEnd)) {//开始时间为空,结束时间不为空
orderTime = dateToStr(plusMonth(toDate(orderTimeEnd, DateEnum.DATE_SIMPLE), -1), DateEnum.DATE_SIMPLE);
}
if (StringUtils.isNotBlank(orderTime) && StringUtils.isBlank(orderTimeEnd)) {//结束时间为空,开始时间不为空
orderTimeEnd = dateToStr(plusMonth(toDate(orderTime, DateEnum.DATE_SIMPLE), 1), DateEnum.DATE_SIMPLE);
}
pageVo.getParameters().put("orderTime", orderTime);
pageVo.getParameters().put("orderTimeEnd", orderTimeEnd);
return pageVo;

}

public static Integer getMaxDayOfMonth(Date date){
date = truncate(date, Calendar.MONTH);
date = plusMonth(date, 1);
date = plusDay(date, -1);
return date.getDate();
}

public static boolean checkValidDate(String str,DateEnum dateEnum) {
SimpleDateFormat format = new SimpleDateFormat(dateEnum.getText());
format.setLenient(false);
try {
format.parse(str);
return true;
} catch (ParseException e) {
//如果throw java.text.ParseException或者NullPointerException,就说明格式不对
return false;
}
}

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