java 时间加工工具类

package cmcc.gz.common.app.util;


import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;


/**
 * 日期转换工具
 * 
 * @author linchengliang
 * 
 */
public class DateUtil {
public static final String C_DATE_DIVISION = "-";
public static final String C_TIME_PATTON_DEFAULT = "yyyy-MM-dd HH:mm:ss";
public static final String C_DATE_PATTON_DEFAULT = "yyyy-MM-dd";
public static final String C_DATA_PATTON_YYYYMMDD = "yyyyMMdd";
public static final String C_TIME_PATTON_HHMMSS = "HH:mm:ss";


public static final int C_ONE_SECOND = 1000;
public static final int C_ONE_MINUTE = 60 * C_ONE_SECOND;
public static final int C_ONE_HOUR = 60 * C_ONE_MINUTE;
public static final long C_ONE_DAY = 24 * C_ONE_HOUR;


/**
* Return the current date

* @return - DATE<br>
*/
public static Date getCurrentDate() {
Calendar cal = Calendar.getInstance();
Date currDate = cal.getTime();


return currDate;
}


/**
* Return the current date string

* @return - 产生的日期字符串<br>
*/
public static String getCurrentDateStr() {
Calendar cal = Calendar.getInstance();
Date currDate = cal.getTime();


return format(currDate);
}


/**
* Return the current date in the specified format

* @param strFormat
* @return
*/
public static String getCurrentDateStr(String strFormat) {
Calendar cal = Calendar.getInstance();
Date currDate = cal.getTime();


return format(currDate, strFormat);
}


/**
* Parse a string and return a date value

* @param dateValue
* @return
* @throws Exception
*/
public static Date parseDate(String dateValue) {
return parseDate(C_DATE_PATTON_DEFAULT, dateValue);
}


/**
* Parse a strign and return a datetime value

* @param dateValue
* @return
*/
public static Date parseDateTime(String dateValue) {
return parseDate(C_TIME_PATTON_DEFAULT, dateValue);
}


/**
* Parse a string and return the date value in the specified format

* @param strFormat
* @param dateValue
* @return
* @throws ParseException
* @throws Exception
*/
public static Date parseDate(String strFormat, String dateValue) {
if (dateValue == null)
return null;


if (strFormat == null)
strFormat = C_TIME_PATTON_DEFAULT;


SimpleDateFormat dateFormat = new SimpleDateFormat(strFormat);
Date newDate = null;


try {
newDate = dateFormat.parse(dateValue);
} catch (ParseException pe) {
pe.printStackTrace();
newDate = null;
}
return newDate;
}


/**
* 将Timestamp类型的日期转换为系统参数定义的格式的字符串。

* @param aTs_Datetime
*            需要转换的日期。
* @return 转换后符合给定格式的日期字符串
*/
public static String format(Date aTs_Datetime) {
return format(aTs_Datetime, C_DATE_PATTON_DEFAULT);
}


/**
* 将Timestamp类型的日期转换为系统参数定义的格式的字符串。

* @param aTs_Datetime
*            需要转换的日期。
* @return 转换后符合给定格式的日期字符串
*/
public static String formatTime(Date aTs_Datetime) {
return format(aTs_Datetime, C_TIME_PATTON_DEFAULT);
}


/**
* 将Date类型的日期转换为系统参数定义的格式的字符串。

* @param aTs_Datetime
* @param as_Pattern
* @return
*/
public static String format(Date aTs_Datetime, String as_Pattern) {
if (aTs_Datetime == null || as_Pattern == null)
return null;
SimpleDateFormat dateFromat = new SimpleDateFormat();
dateFromat.applyPattern(as_Pattern);


return dateFromat.format(aTs_Datetime);
}


/**
* @param aTs_Datetime
* @param as_Format
* @return
*/
public static String formatTime(Date aTs_Datetime, String as_Format) {
if (aTs_Datetime == null || as_Format == null)
return null;
SimpleDateFormat dateFromat = new SimpleDateFormat();
dateFromat.applyPattern(as_Format);


return dateFromat.format(aTs_Datetime);
}


public static String getFormatTime(Date dateTime) {
return formatTime(dateTime, C_TIME_PATTON_HHMMSS);
}


/**
* @param aTs_Datetime
* @param as_Pattern
* @return
*/
public static String format(Timestamp aTs_Datetime, String as_Pattern) {
if (aTs_Datetime == null || as_Pattern == null)
return null;
SimpleDateFormat dateFromat = new SimpleDateFormat();
dateFromat.applyPattern(as_Pattern);


return dateFromat.format(aTs_Datetime);
}


/**
* 取得指定日期N天后的日期

* @param date
* @param days
* @return
*/
public static Date addDays(Date date, int days) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);


cal.add(Calendar.DAY_OF_MONTH, days);


return cal.getTime();
}


/**
* 计算两个日期之间相差的天数

* @param date1
* @param date2
* @return
*/
public static int daysBetween(Date date1, Date date2) {
Calendar cal = Calendar.getInstance();
cal.setTime(date1);
long time1 = cal.getTimeInMillis();
cal.setTime(date2);
long time2 = cal.getTimeInMillis();
long between_days = (time2 - time1) / (1000 * 3600 * 24);


return Integer.parseInt(String.valueOf(between_days));
}


/**
* 计算当前日期相对于"1977-12-01"的天数

* @param date
* @return
*/
public static long getRelativeDays(Date date) {
Date relativeDate = DateUtil.parseDate("yyyy-MM-dd", "1977-12-01");


return DateUtil.daysBetween(relativeDate, date);
}


public static Date getDateBeforTwelveMonth() {
String date = "";
Calendar cla = Calendar.getInstance();
cla.setTime(getCurrentDate());
int year = cla.get(Calendar.YEAR) - 1;
int month = cla.get(Calendar.MONTH) + 1;
if (month > 9) {
date = String.valueOf(year) + C_DATE_DIVISION
+ String.valueOf(month) + C_DATE_DIVISION + "01";
} else {
date = String.valueOf(year) + C_DATE_DIVISION + "0"
+ String.valueOf(month) + C_DATE_DIVISION + "01";
}


Date dateBefore = parseDate(date);
return dateBefore;
}


/**
* 传入时间字符串,加一天后返回Date

* @param date
*            时间 格式 YYYY-MM-DD
* @return
*/
public static Date addDate(String date) {
if (date == null) {
return null;
}
Date tempDate = parseDate(C_DATE_PATTON_DEFAULT, date);
String year = format(tempDate, "yyyy");
String month = format(tempDate, "MM");
String day = format(tempDate, "dd");


GregorianCalendar calendar = new GregorianCalendar(
Integer.parseInt(year), Integer.parseInt(month) - 1,
Integer.parseInt(day));


calendar.add(GregorianCalendar.DATE, 1);
return calendar.getTime();
}


/**
* 判断date1是否在date2之后,在返回true,不在返回false

* @method after
* @param date1
* @param date2
* @return
* @throws
*/
public static boolean after(Date date1, Date date2) {


System.out.println(date1);
System.out.println(date2);
/**
* 获得时间1
*/
Calendar calone = Calendar.getInstance();
calone.setTime(date1);


/**
* 获得时间2
*/
Calendar caltwo = Calendar.getInstance();
caltwo.setTime(date2);


return calone.after(caltwo);


}


public static String getFormatDateTime(Date dateTime) {
return formatTime(dateTime, C_TIME_PATTON_DEFAULT);
}


// public String test(TestDomain joinInfo) {
// return joinInfo.getId() + ":" + joinInfo.getName();
// }


public static String getSGWDateTime(long dateTime) {


dateTime = dateTime * 1000;
long sd = (dateTime - 28800000);


Date dat = new Date(sd);
return formatTime(dat);
}


/**
* 获取当前时间的前一天 yyyy-MM-dd
*/
public static String getBeforDate() {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, -1); // 得到前一天
Date date = calendar.getTime();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
return df.format(date);
}


/**
* 获取当前时间的前一天 yyyyMMdd
*/
public static String getBeforDateNoWire() {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, -1); // 得到前一天
Date date = calendar.getTime();
DateFormat df = new SimpleDateFormat("yyyyMMdd");
return df.format(date);
}


/**
* 当前时间 yyyyMMdd
*/
public static String getNowDate() {
Calendar calendar = Calendar.getInstance();
Date date = calendar.getTime();
DateFormat df = new SimpleDateFormat("yyyyMMdd");
return df.format(date);
}


/**
* 当前时间 yyyy-MM-dd HH:mm:ss
*/
public static String getNow() {
Calendar calendar = Calendar.getInstance();
Date date = calendar.getTime();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return df.format(date);
}


/**
* 将时间戳装换成字符串时间
*/
public static String getDateByNum(String dateNum) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String date = sdf.format(new Date(
(long) (Double.parseDouble(dateNum) * 1000)));
return date;
}



 
/**
* 当前时间 yyyy-MM-dd HH:mm:ss 
*/
public static String getNowTime() {
Calendar calendar = Calendar.getInstance();
Date date = calendar.getTime();
DateFormat df = new SimpleDateFormat("MMddHHmmss");
return df.format(date);
}

public static void main(String[] args) {
System.out.println((int)(Math.random()*100000));
}




}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值