Java 时间处理工具类

工作中处理时间的工具类,希望对大家有用

package com.utils;

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class TimeUtil {

/**
* 获取当前日期并转换成字符串
*
* @param formart
* 转换格式
* @return
*/
public static String getDate2String(String formart) {
return getDate2String(new Date(), formart);
}

/**
* 获取当前日期并转换成YYYY-MM-dd HH:mm:ss 格式的字符串
*
* @return
*/
public static String getDate2String() {
return getDate2String(new Date());
}

/**
* 将日期转换成字符串
*
* @param date
* 转换日期
* @param formart
* 转换格式
* @return
*/
public static String getDate2String(Date date, String formart) {
SimpleDateFormat sdf = new SimpleDateFormat(formart);
return sdf.format(date);
}

/**
* 将日期转换成YYYY-MM-dd HH:mm:ss 格式的字符串
*
* @param date
* 转换日期
* @return
*/
public static String getDate2String(Date date) {
return getDate2String(date, "yyyy-MM-dd HH:mm:ss");
}

/**
* 获取但前日期月份
*
* @return
*/
public static int getMonth() {
return getMonth(new Date());
}

/**
* 获取日期月份
*
* @param date
* @return
*/
public static int getMonth(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
return c.get(Calendar.MONTH);
}

/**
* 获取日期年份
*
* @return
*/
public static int getYear() {
return getYear(new Date());
}

/**
* 获取日期年份
*
* @param date
* @return
*/
public static int getYear(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
return c.get(Calendar.YEAR);
}

/**
* 获取当前日期在月中的天数
*
* @return
*/
public static int getDayOfMonth() {
return getDayOfMonth(new Date());
}

/**
* 获取日期在月份中的天数
*
* @param date
* @return
*/
public static int getDayOfMonth(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
return c.get(Calendar.DAY_OF_MONTH);
}

/**
* 获取当前日期的的星期数
*
* @return
*/
public static int getDayOfWeek() {
return getDayOfWeek(new Date());
}

/**
* 获取日期的星期数
*
* @param date
* @return
*/
public static int getDayOfWeek(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
return c.get(Calendar.DAY_OF_WEEK);
}

/**
* 获取当前日期是一年中第几天
*
* @return
*/
public static int getDayOfYear() {
return getDayOfYear(new Date());
}

/**
* 获取日期在一年中的天数
*
* @param date
* @return
*/
public static int getDayOfYear(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
return c.get(Calendar.DAY_OF_YEAR);
}

/**
* 获取下一个月
*
* @return
*/
public static int getNextMonth() {
return getNextMonth(new Date());
}

/**
* 获取下一个月
*
* @param date
* @return
*/
public static int getNextMonth(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.MONTH, 1);
return c.get(Calendar.MONTH);
}

/**
* 获取上一个月
*
* @param date
* @return
*/
public static int getUpMonth(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.MONTH, -1);
return c.get(Calendar.MONTH);
}

/**
* 获取上一个月
*
* @return
*/
public static int getUpMonth() {
return getUpMonth(new Date());
}

/**
* 获取一周的开始日期
*
* @param date
* @return
*/
public static int getFirstDayOfWeek(Date date) {
int n = getDayOfWeek(date);
Calendar c = Calendar.getInstance();
c.setTime(date);
c.set(Calendar.DAY_OF_MONTH, c.get(Calendar.DAY_OF_MONTH) - n + 1);
return c.get(Calendar.DAY_OF_MONTH);
}

/**
* 获取一周的最后一天
*
* @param date
* @return
*/
public static int getLastDayOfWeek(Date date) {
int n = getDayOfWeek(date);
Calendar c = Calendar.getInstance();
c.setTime(date);
System.out.println(getDate2String(c.getTime()));
c.set(Calendar.DAY_OF_MONTH, c.get(Calendar.DAY_OF_MONTH) + (7 - n));
return c.get(Calendar.DAY_OF_MONTH);
}

/**
* 在日期上加days天
*
* @param date
* @param days
* @return
*/
public static Date addDay(Date date, int days) {
return add(date,Calendar.DAY_OF_MONTH,days);
}

/**
* 获取明天日期
*
* @param date
* @return
*/
public static Date getTomorrow(Date date) {
return addDay(date, 1);
}

/**
* 获取明天日期
*
* @return
*/
public static Date getTomorrow() {
return getTomorrow(new Date());
}

/**
* 获取昨天日期
*
* @param date
* @return
*/
public static Date getYestoday(Date date) {
return addDay(date, -1);
}

/**
* 获取昨天日期
*
* @return
*/
public static Date getYestoday() {
return getYestoday(new Date());
}

/**
* 获取两日期之间相差的天数
*
* @param starDate
* @param endDate
* @return
*/
public static long getDiffOfDays(Date starDate, Date endDate) {
return getDiffOfHours(starDate,endDate)/24;
}

/**
* 获取两日期之间相差的小时
*
* @param starDate
* @param endDate
* @return
*/
public static long getDiffOfHours(Date starDate, Date endDate) {
return getDiffOfMinute(starDate,endDate)/60;
}

public static long getDiffOfMinute(Date starDate, Date endDate) {
return getDiffOfSecond(starDate,endDate)/60;
}

public static long getDiffOfSecond(Date starDate, Date endDate) {
return getDiffOfMillis(starDate,endDate)/1000;
}

public static long getDiffOfMillis(Date starDate, Date endDate) {
Calendar star = Calendar.getInstance();
star.setTime(starDate);
Calendar end = Calendar.getInstance();
end.setTime(endDate);
long diff = star.getTimeInMillis() - end.getTimeInMillis();
long diffMillis =(Math.abs(diff));
return diffMillis;
}

/**
* 获取量日期之间相差的月数
*
* @param starDate
* @param endDate
* @return
*/
public static int getDiffOfMonth(Date starDate, Date endDate) {
Calendar star = Calendar.getInstance();
star.setTime(starDate);
Calendar end = Calendar.getInstance();
end.setTime(endDate);
int diffYear = star.get(Calendar.YEAR) - end.get(Calendar.YEAR);
int diffMonth = star.get(Calendar.MONTH) - end.get(Calendar.MONTH);
return Math.abs(diffYear * 12 + diffMonth);
}

/**
* 获取两日期间相差的年数
*
* @param starDate
* @param endDate
* @return
*/
public static int getDiffOfYears(Date starDate, Date endDate) {
Calendar star = Calendar.getInstance();
star.setTime(starDate);
Calendar end = Calendar.getInstance();
end.setTime(endDate);
int diffYear = star.get(Calendar.YEAR) - end.get(Calendar.YEAR);
return Math.abs(diffYear);
}

/**
* 获取月中的天数
*
* @param date
* @return
*/
public static int getMonthDays(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
return c.getActualMaximum(Calendar.DAY_OF_MONTH);
}

/**
* 获取月中的天数
*
* @return
*/
public static int getMonthDays() {
return getMonthDays(new Date());
}

/**
* 获取下月天数
*
* @param date
* @return
*/
public static int getNextMonthDays(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.MONTH, 1);
return c.getActualMaximum(Calendar.DAY_OF_MONTH);
}

/**
* 获取下月天数
*
* @return
*/
public static int getNextMonthDays() {
return getNextMonthDays(new Date());
}

/**
* 获取上月天数
*
* @param date
* @return
*/
public static int getUpMonthDays(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.MONTH, -1);
return c.getActualMaximum(Calendar.DAY_OF_MONTH);
}

/**
* 获取上月天数
*
* @return
*/
public static int getUpMonthDays() {
return getUpMonthDays(new Date());
}

/**
* 把时间字符串转换成Date
* @param date
* @param datePartten
* @return
* @throws ParseException
*/
public static Date parseDate(String date, String datePartten)
throws ParseException {
SimpleDateFormat df = new SimpleDateFormat(datePartten);
Date d = df.parse(date);
return d;
}

/**
* 把时间字符串转换成Date
* @param date
* @return
* @throws Exception
*/
public static Date parseDate(String date) throws Exception {
String[] datePartten = { "yyyy-MM-dd HH:mm:ss",
"yyyy-MM-dd", "yyyy/MM/dd HH:mm;ss", "yyyy/MM/dd","yyyyMMddHHmmss", "yyyyMMdd" };
return parseDate(date,datePartten);
}

/**
* 把时间字符串转换成Date
* @param date
* @param dateParttens
* @return
* @throws Exception
*/
public static Date parseDate(String date,String [] dateParttens) throws Exception {
SimpleDateFormat df = null;
Date d = null;
boolean isParse=false;
for (String partten : dateParttens) {
df = new SimpleDateFormat(partten);
try {
d = df.parse(date);
isParse=true;
break;
} catch (ParseException e) {
isParse=false;
}
}
if(!isParse){
throw new Exception();
}
return d;
}

/**
* 是时间的基础上+
* @param date
* @param field 请使用Calendar的常量
* @param amount
* @return
*/
public static Date add(Date date,int field,int amount){
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(field, amount);
return c.getTime();
}


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值