关闭

java 时间加工工具类

标签: java时间加工
268人阅读 评论(0) 收藏 举报
分类:
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

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:63122次
    • 积分:1070
    • 等级:
    • 排名:千里之外
    • 原创:43篇
    • 转载:19篇
    • 译文:0篇
    • 评论:3条
    最新评论