自用时间时间工具类
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import org.apache.commons.lang3.time.DateFormatUtils;
/**
* 日期工具类, 继承org.apache.commons.lang.time.DateUtils类
*
* @author SuperWD
* @version 2016-03-30
*/
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
private static String[] parsePatterns = {
"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
"yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
"yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};
/**
* 得到当前日期字符串 格式(yyyy-MM-dd)
*/
public static String getDate() {
return getDate("yyyy-MM-dd");
}
/**
* 得到当前日期字符串 格式(yyyy-MM-dd) pattern可以为:"yyyy-MM-dd" "HH:mm:ss" "E"
*/
public static String getDate(String pattern) {
return DateFormatUtils.format(new Date(), pattern);
}
/**
* 得到日期字符串 默认格式(yyyy-MM-dd) pattern可以为:"yyyy-MM-dd" "HH:mm:ss" "E"
*/
public static String formatDate(Date date, Object... pattern) {
String formatDate = null;
if (pattern != null && pattern.length > 0) {
formatDate = DateFormatUtils.format(date, pattern[0].toString());
} else {
formatDate = DateFormatUtils.format(date, "yyyy-MM-dd");
}
return formatDate;
}
/**
* 得到日期时间字符串,转换格式(yyyy-MM-dd HH:mm:ss)
*/
public static String formatDateTime(Date date) {
return formatDate(date, "yyyy-MM-dd HH:mm:ss");
}
/**
* 得到当前时间字符串 格式(HH:mm:ss)
*/
public static String getTime() {
return formatDate(new Date(), "HH:mm:ss");
}
/**
* 得到当前日期和时间字符串 格式(yyyy-MM-dd HH:mm:ss)
*/
public static String getDateTime() {
return formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
}
/**
* 得到当前日期和时间字符串 格式(yyyyMMddHHmmss)
*/
public static String getDateTimestamp() {
return formatDate(new Date(), "yyyyMMddHHmmss");
}
/**
* 得到当前年份字符串 格式(yyyy)
*/
public static String getYear() {
return formatDate(new Date(), "yyyy");
}
/**
* 得到当前月份字符串 格式(MM)
*/
public static String getMonth() {
return formatDate(new Date(), "MM");
}
/**
* 得到当天字符串 格式(dd)
*/
public static String getDay() {
return formatDate(new Date(), "dd");
}
/**
* 得到当前星期字符串 格式(E)星期几
*/
public static String getWeek() {
return formatDate(new Date(), "E");
}
/**
* 得到当前月份字符串 格式(yyyy-MM)
*/
public static String getLastMonth() {
return formatDate(addMonths(new Date(), -1), "yyyy-MM");
}
/**
* 得到下个月份字符串 格式(yyyy-MM)
*/
public static String getNextMonth() {
return formatDate(addMonths(new Date(), 1), "yyyy-MM");
}
/**
* 日期型字符串转化为日期 格式
* { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm",
* "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm",
* "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm" }
*/
public static Date parseDate(Object str) {
if (str == null) {
return null;
}
try {
return parseDate(str.toString(), parsePatterns);
} catch (ParseException e) {
return null;
}
}
/**
* 获取过去的天数
*
* @param date
* @return
*/
public static long pastDays(Date date) {
long t = new Date().getTime() - date.getTime();
return t / (24 * 60 * 60 * 1000);
}
/**
* 获取过去的年数
*
* @return
*/
public static long pastYears(Date birthday) {
int age = 0;
try {
Calendar now = Calendar.getInstance();
now.setTime(new Date());// 当前时间
Calendar birth = Calendar.getInstance();
birth.setTime(birthday);
if (birth.after(now)) {//如果传入的时间,在当前时间的后面,返回0岁
age = 0;
} else {
age = now.get(Calendar.YEAR) - birth.get(Calendar.YEAR);
// if (now.get(Calendar.DAY_OF_YEAR) > birth.get(Calendar.DAY_OF_YEAR)) {
// age += 1;
// }
}
return age;
} catch (Exception e) {//兼容性更强,异常后返回数据
return 0;
}
}
/**
* 获取过去的小时
*
* @param date
* @return
*/
public static long pastHour(Date date) {
long t = new Date().getTime() - date.getTime();
return t / (60 * 60 * 1000);
}
/**
* 获取过去的分钟
*
* @param date
* @return
*/
public static long pastMinutes(Date date) {
long t = new Date().getTime() - date.getTime();
return t / (60 * 1000);
}
/**
* 转换为时间(天,时:分:秒.毫秒)
*
* @param timeMillis
* @return
*/
public static String formatDateTime(long timeMillis) {
long day = timeMillis / (24 * 60 * 60 * 1000);
long hour = (timeMillis / (60 * 60 * 1000) - day * 24);
long min = ((timeMillis / (60 * 1000)) - day * 24 * 60 - hour * 60);
long s = (timeMillis / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
long sss = (timeMillis - day * 24 * 60 * 60 * 1000 - hour * 60 * 60 * 1000 - min * 60 * 1000 - s * 1000);
return (day > 0 ? day + "," : "") + hour + ":" + min + ":" + s + "." + sss;
}
/**
* 得到当前月份字符串 格式(yyyy-MM)
*/
// public static String getLastMonth() {
// return formatDate(addMonths(new Date(), -1), "yyyy-MM");
// }
/**
* 获取两个日期之间的天数
*
* @param before
* @param after
* @return
*/
public static double getDistanceOfTwoDate(Date before, Date after) {
long beforeTime = before.getTime();
long afterTime = after.getTime();
return (afterTime - beforeTime) / (1000 * 60 * 60 * 24);
}
/**
* 获取当前日期属于当年第几周
*/
public static int getSortOfWeekInYear(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setFirstDayOfWeek(Calendar.MONDAY);
calendar.setTime(date);
return calendar.get(Calendar.WEEK_OF_YEAR);
}
public static Map getBetweenDateByWeekSortAndYear(int year, int sort) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year); // 2016年
cal.set(Calendar.WEEK_OF_YEAR, sort); // 设置为2016年的第10周
cal.set(Calendar.DAY_OF_WEEK, 2); // 1表示周日,2表示周一,7表示周六
Date date1 = cal.getTime();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String firstDate = format.format(date1);
if (sort == 52) {
year += 1;
}
cal.set(Calendar.YEAR, year); // 2016年
cal.set(Calendar.WEEK_OF_YEAR, sort + 1); // 设置为2016年的第10周
cal.set(Calendar.DAY_OF_WEEK, 1); // 1表示周日,2表示周一,7表示周六
Date date2 = cal.getTime();
String endDate = format.format(date2);
Map map = new HashMap();
map.put("firstDate", firstDate);
map.put("finishDate", endDate);
return map;
}
/**
* 日期转星期索引
*
* @param datetime
* @return
*/
public static int dateToWeek(String datetime) {
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
int[] weekDays = {7, 1, 2, 3, 4, 5, 6};
Calendar cal = Calendar.getInstance(); // 获得一个日历
Date datet = null;
try {
datet = f.parse(datetime);
cal.setTime(datet);
} catch (ParseException e) {
e.printStackTrace();
}
int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
if (w < 0)
w = 0;
return weekDays[w];
}
/**
* 日期转星期
*
* @param datetime
* @return
*/
public static String dateToWeekDetail(String datetime) {
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
Calendar cal = Calendar.getInstance(); // 获得一个日历
Date datet = null;
try {
datet = f.parse(datetime);
cal.setTime(datet);
} catch (ParseException e) {
e.printStackTrace();
}
int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
if (w < 0)
w = 0;
return weekDays[w];
}
/**
* 获取最近n周的列表
*/
public static List getLimitWeekList(int n) {
//周期
int weekSort = DateUtils.getSortOfWeekInYear(new Date()) - 1;
List weeks = new ArrayList();
String year = DateUtils.getYear();
for (int i = 0; i < n; i++) {
weekSort += 1;
if (weekSort > 52) {
weekSort = 1;
year = String.valueOf(Integer.parseInt(year) + 1);
}
String weekStr = String.valueOf(weekSort);
if (weekSort < 10) {
weekStr = "0" + weekStr;
}
String finalWeek = year + "年" + weekStr + "周";
weeks.add(finalWeek);
}
return weeks;
}
/**
* 获取两个日期的中间日期
*/
public static List getBetweenDates(String startDate, String endDate) throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date start = format.parse(startDate);
Date end = format.parse(endDate);
List result = new ArrayList();
result.add(start);
Calendar tempStart = Calendar.getInstance();
tempStart.setTime(start);
tempStart.add(Calendar.DAY_OF_YEAR, 1);
Calendar tempEnd = Calendar.getInstance();
tempEnd.setTime(end);
while (tempStart.before(tempEnd)) {
result.add(tempStart.getTime());
tempStart.add(Calendar.DAY_OF_YEAR, 1);
}
result.add(end);
List dateStr = new ArrayList();
//dateStr.add(startDate);
for (Date date : result) {
String aDateStr = format.format(date);
String weekDetail = dateToWeekDetail(aDateStr);
dateStr.add(aDateStr + "(" + weekDetail + ")");
}
//dateStr.add(endDate);
return dateStr;
}
/**
* 获取两个年月中间的月份
*
* @param minDate
* @param maxDate
* @return
* @throws ParseException
*/
public static List getMonthBetween(String minDate, String maxDate) throws ParseException {
ArrayList result = new ArrayList();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");//格式化为年月
Calendar min = Calendar.getInstance();
Calendar max = Calendar.getInstance();
min.setTime(sdf.parse(minDate));
min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);
max.setTime(sdf.parse(maxDate));
max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);
Calendar curr = min;
while (curr.before(max)) {
result.add(sdf.format(curr.getTime()));
curr.add(Calendar.MONTH, 1);
}
return result;
}
//获取某月多少天
public static int getDaysByYearMonth(int month) {
Calendar a = Calendar.getInstance();
a.set(Calendar.MONTH, month - 1);
a.set(Calendar.DATE, 1);
a.roll(Calendar.DATE, -1);
int maxDate = a.get(Calendar.DATE);
return maxDate;
}
/**
* @param args
* @throws ParseException
*/
public static void main(String[] args) throws ParseException {
System.out.println(getDate("yyyy-MM"));
// System.out.println(formatDate(parseDate("2010/3/6")));
// System.out.println(getDate("yyyy年MM月dd日 E"));
// long time = new Date().getTime()-parseDate("2012-11-19").getTime();
// System.out.println(time/(24*60*60*1000));
}
}