/*
* 文 件 名: DateTimeUtils.java
* 版 权: CMRI. Copyright YYYY-YYYY, All rights reserved
* 描 述: <描述>
* 修 改 人: Seven Zhang
* 修改时间: Jun 28, 2014
* 跟踪单号: <跟踪单号>
* 修改单号: <修改单号>
* 修改内容: <修改内容>
*/
package com.nnct.trbs.portal.utils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.joda.time.DateTime;
import org.joda.time.Interval;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import com.nnct.trbs.portal.common.GlobalConstant;
/**
* 時間工具類
*
* @author Seven Zhang
* @version [版本号, Jun 28, 2014]
* @see [相关类/方法]
* @since [产品/模块版本]
*/
public class TimeUtils {
public static final int FIRST_MONTH_OF_YEAR = 1;
public static final int LAST_MONTH_OF_YEAR = 12;
/**
* 将毫秒字符串转成时间格式字符串
*
* @param millis
* @param pattern
* @return
* @see [类、类#方法、类#成员]
*/
public static String format(String millis, String pattern) {
long millisecond = NumberUtils.toLong(millis, 0L);
return DateFormatUtils.format(millisecond, pattern);
}
/**
* whether the time with specified format is after now
*
* @param dateTime
* @param pattern
* @return
* @see [类、类#方法、类#成员]
*/
public static boolean isAfterNow(String dateTime, String pattern) {
DateTimeFormatter dtf = DateTimeFormat.forPattern(pattern);
DateTime dt = dtf.parseDateTime(dateTime);
return dt.isAfterNow();
}
/**
* 格式化当前时间日期
*
* @param pattern
* @return
* @see [类、类#方法、类#成员]
*/
public static String formatNow(String pattern) {
long nowMillis = DateTime.now().getMillis();
return DateFormatUtils.format(nowMillis, pattern);
}
/**
* 获取当前时间的毫秒String值
*
* @return
* @see [类、类#方法、类#成员]
*/
public static String getNowMillisAsStr() {
long nowMillis = DateTime.now().getMillis();
return String.valueOf(nowMillis);
}
/**
* 判断给予时间是否在本周 通过获取周一是否同一天来判断
*
* @param millis
* @return
* @see [类、类#方法、类#成员]
*/
public static boolean isInCurrentWeek(long millis) {
Date thisMonday = DateTime.now().withDayOfWeek(1).toDate();
Date givenMonday = new DateTime(millis).withDayOfWeek(1).toDate();
return DateUtils.isSameDay(thisMonday, givenMonday);
}
/**
* 判断一个时间字符串是否完整格式or仅日期格式
*
* @param timestamp
* @return
* @see [类、类#方法、类#成员]
*/
public static boolean isFullDateTime(String timestamp) {
return StringUtils.contains(timestamp, " ") && StringUtils.contains(timestamp, ":");
}
/**
* 获取某月(默认当前月)的第一天,精确至毫秒
*
* @param year
* @param month
* @return
* @see [类、类#方法、类#成员]
*/
public static DateTime getFirstDayOfMonth() {
return DateTime.now().dayOfMonth().withMinimumValue().millisOfDay().withMinimumValue();
}
/**
* 获取某月(默认当前月)的第一天,精确至毫秒
*
* @param year
* @param month
* @return
* @see [类、类#方法、类#成员]
*/
public static DateTime getFirstDayOfMonth(int year, int month) {
if (year <= 0 || month < 0 || month > 12) {
return getFirstDayOfMonth();
}
else if (month == 0) {
return DateTime.now().year().setCopy(year).monthOfYear().setCopy(FIRST_MONTH_OF_YEAR).dayOfMonth()
.withMinimumValue().millisOfDay().withMinimumValue();
}
else {
return DateTime.now().year().setCopy(year).monthOfYear().setCopy(month).dayOfMonth().withMinimumValue()
.millisOfDay().withMinimumValue();
}
}
/**
* 获取某月(默认当前月)的最后一天,精确至毫秒
*
* @param year
* @param month
* @return
* @see [类、类#方法、类#成员]
*/
public static DateTime getLastDayOfMonth() {
return DateTime.now().dayOfMonth().withMaximumValue().millisOfDay().withMaximumValue();
}
/**
* 获取某月(默认当前月)的最后一天,精确至毫秒
*
* @param year
* @param month
* @return
* @see [类、类#方法、类#成员]
*/
public static DateTime getLastDayOfMonth(int year, int month) {
if (year <= 0 || month < 0 || month > 12) {
return getLastDayOfMonth();
}
else if (month == 0) {
return DateTime.now().year().setCopy(year).monthOfYear().setCopy(LAST_MONTH_OF_YEAR).dayOfMonth()
.withMaximumValue().millisOfDay().withMaximumValue();
}
else {
return DateTime.now().year().setCopy(year).monthOfYear().setCopy(month).dayOfMonth().withMaximumValue()
.millisOfDay().withMaximumValue();
}
}
/**
* 将一个以秒为单位的时间段转化成(小时)分秒的数组
*
* @param duration
* @return
* @see [类、类#方法、类#成员]
*/
public static String[] parseDurationInArray(int duration) {
String[] result = new String[] {};
int hours = (int) Math.floor(duration / (60 * 60));
if (hours > 0) {
result = (String[]) ArrayUtils.add(result, String.valueOf(hours));
}
int minutes = (int) Math.floor((duration % (60 * 60)) / 60);
result = (String[]) ArrayUtils.add(result, String.valueOf(minutes));
int seconds = (int) Math.floor(duration % 60);
result = (String[]) ArrayUtils.add(result, String.valueOf(seconds));
return result;
}
/**
* 获取今天0点毫秒
*
* @return
*/
public static long getMinMillisOfToday() {
return DateTime.now().millisOfDay().withMinimumValue().getMillis();
}
/**
* 获取今天24点毫秒
*
* @return
*/
public static long getMaxMillisOfToday() {
return DateTime.now().millisOfDay().withMinimumValue().plusDays(1).getMillis();
}
/**
* 将日期字符串转换为长整型
*
* @param date
* 日期字符串形式,格式:yyyy-MM-dd
* @param format
* 日期格式
* @return long型日期
*/
public static long convert2long(String date) {
try {
if (StringUtils.isNotBlank(date)) {
SimpleDateFormat sf = new SimpleDateFormat(GlobalConstant.DATE_TIME_PATTERN);
return sf.parse(date).getTime();
}
}
catch (ParseException e) {
e.printStackTrace();
}
return 0l;
}
/**
* 将精确到秒的时间转换为长整型
*
* @param user_time
* @return
*/
public static String getTime(String user_time) {
String re_time = null;
SimpleDateFormat sdf = new SimpleDateFormat(GlobalConstant.FULL_DATE_TIME_PARTERN);
Date d;
try {
d = sdf.parse(user_time);
long l = d.getTime();
re_time = String.valueOf(l);
}
catch (ParseException e) {
e.printStackTrace();
}
return re_time;
}
/**
* Check if two date periods overlap
*
* @param startDate1
* @param endDate1
* @param startDate2
* @param endDate2
* @return
*/
public static boolean isOverlapping(String startDate1, String endDate1, String startDate2, String endDate2) {
DateTime startTime1 = new DateTime(startDate1);
DateTime endTime1 = new DateTime(endDate1);
DateTime startTime2 = new DateTime(startDate2);
DateTime endTime2 = new DateTime(endDate2);
Interval interval1 = new Interval(startTime1, endTime1);
Interval interval2 = new Interval(startTime2, endTime2);
return interval1.overlaps(interval2);
}
public static String getCurrentDay(){
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = format.format(date);
return dateString;
}
}
转载于:https://my.oschina.net/loveryuan/blog/402921