java对日期时间的相关操作方法


/**
*
*/
package com.keda.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* @author LeeYau
*
*/
public class FDateFormat {
public static final SimpleDateFormat NORMAL_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public static final SimpleDateFormat NORMAL_FORMATA = new SimpleDateFormat("yyyy-MM-dd");
public static final SimpleDateFormat YEAR_FORMAT = new SimpleDateFormat("yyyy");
public static final SimpleDateFormat MONTH_FORMAT = new SimpleDateFormat("MM");
public static final SimpleDateFormat TIME_FORMAT = new SimpleDateFormat("HH:mm:ss");
public static final SimpleDateFormat DAY_FORMAT = new SimpleDateFormat("dd");
public static final SimpleDateFormat HOUR_FORMAT = new SimpleDateFormat("HH");
public static final SimpleDateFormat MINUTE_FORMAT = new SimpleDateFormat("mm");
public static final SimpleDateFormat SECOND_FORMAT = new SimpleDateFormat("ss");
public static final SimpleDateFormat YEAR_MONTH = new SimpleDateFormat("yyMM");
public static final SimpleDateFormat CONNECT_FORMAT = new SimpleDateFormat("yyyyMMdd");
public static final SimpleDateFormat YYMMDD_FORMAT = new SimpleDateFormat("yyyyMMdd");
public static final SimpleDateFormat MMDD_FORMAT = new SimpleDateFormat("MM-dd");
public static final SimpleDateFormat MMD_FORMAT = new SimpleDateFormat("MM-d");
public static final SimpleDateFormat MDD_FORMAT = new SimpleDateFormat("M-dd");
public static final SimpleDateFormat MD_FORMAT = new SimpleDateFormat("M-d");


/**
* 返回 yyyyMMdd 格式的日期
* @return
* @author liguosheng 2011-11-23 上午09:54:26
*/
public static String GetConnectDate(){
String strRet = "";
try{
SimpleDateFormat sf = CONNECT_FORMAT;
strRet = sf.format(new java.util.Date(System.currentTimeMillis()));
}catch(Exception e){
e.printStackTrace();
}
return strRet;
}


/**
* 返回 系统的 yyyyMMdd 格式的日期
* add by lee on 2011-12-07
* @return
*/
public static String GetSysyyMMddFormatDate(){
String strRet = "";
try{
SimpleDateFormat sf = YYMMDD_FORMAT;
strRet = sf.format(new java.util.Date(System.currentTimeMillis()));
}catch(Exception e){
e.printStackTrace();
}
return strRet;
}


/***
* 功能:返回 yyyy-MM-dd HH:mm:ss 格式的日期
* @return
*/
public static String GetLongDate(){
String strRet="";
try{

SimpleDateFormat sf=NORMAL_FORMAT;
strRet=sf.format(new java.util.Date(System.currentTimeMillis()));
}catch(Exception e){
e.printStackTrace();
}
return strRet;
}

/***
* 功能:返回 yyyy-MM-dd 格式的日期
* @return
*/
public static String GetShortDate(){
String strRet="";
try{

SimpleDateFormat sf=NORMAL_FORMATA;
strRet=sf.format(new java.util.Date(System.currentTimeMillis()));
}catch(Exception e){
e.printStackTrace();
}
return strRet;
}


/**
* 返回当前时间
* @return
*/
public static String getCurTime(){
String strRet="";
try{
SimpleDateFormat sf=TIME_FORMAT;
strRet=sf.format(new java.util.Date(System.currentTimeMillis()));
}catch(Exception e){
e.printStackTrace();
}
return strRet;
}

/**
* 返回指定日期时间
* @return
*/
public static String getPointToDateTime(String dateTime){
String strRet="";
try{
Date date = NORMAL_FORMAT.parse(dateTime);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
SimpleDateFormat sf=TIME_FORMAT;
strRet=sf.format(new java.util.Date(cal.getTimeInMillis()));
}catch(Exception e){
e.printStackTrace();
}
return strRet;
}


//返回当前日期的格式
public static String GetDateTime(SimpleDateFormat sformat)
{
SimpleDateFormat sf=null;
String strRet=null;
if(sformat==null){
sf=NORMAL_FORMAT;
}else{
sf=sformat;
}
try{
strRet=sf.format(new java.util.Date(System.currentTimeMillis()));
}catch(Exception e){
System.out.println("getCurrent time error="+e.getMessage());
}
return strRet;
}


/***
* 将传入的日期型转换成YYY-MM-DD的字符形
* @param date
* @return
*/
public static String getShortDate(Date date){
try{
SimpleDateFormat sf=NORMAL_FORMATA;
return sf.format(date);
}catch(Exception e){
e.printStackTrace();
}
return null;
}


/****
* 取传入的日期与系统日期相差的天数(传入日期-系统日期)
* @param strDate----参数格式:yyyy-MM-dd
* @return
*/
public static Long getDayDifferWithSystemDate(String strDate){
try{
final long DAY = 3600*1000*24;
DateFormat f = new SimpleDateFormat("yyyy-MM-dd");
Date sysTemDate = f.parse(f.format(new Date()));
Date inputDate = f.parse(strDate);
long lngRetDiffer=(inputDate.getTime()-sysTemDate.getTime())/DAY;
return lngRetDiffer;
}catch(Exception e){
e.printStackTrace();
}
return null;
}

/****
* add by lee on 2011-07-31
* 计算传入的日期相差的天数(endDay-beginDate)
* @param beginDate----参数格式:yyyy-MM-dd
* @param endDay----参数格式:yyyy-MM-dd
* @return
*/
public static Long getDayDiffer(String beginDate,String endDay){
try{
final long DAY = 3600*1000*24;
DateFormat f = new SimpleDateFormat("yyyy-MM-dd");
Date startDate = f.parse(beginDate);
Date endDate = f.parse(endDay);
long lngRetDiffer=(endDate.getTime()-startDate.getTime())/DAY;
return lngRetDiffer;
}catch(Exception e){
e.printStackTrace();
}
return null;
}


/****
* 将长整型的日期类型,转换成字符型的日期类型
* @param lngDate
* @param DateFormate
* @return
*/
public static String getLongToDateString(long lngDate,String DateFormate){
try{
Date date=new Date(lngDate);
DateFormat df=null;
if(DateFormate!=null){
df = new SimpleDateFormat(DateFormate);
}else{
df=NORMAL_FORMAT;
}
return df.format(date);
}catch(Exception e){
System.out.println("长整型:"+lngDate+"转换成日期类型出错了!");
e.printStackTrace();
}
return null;
}

/****
* 根据输入的日期,及相隔的天数,返回相隔天数的日期
* @param strDate-----输入的日期,格式要求yyyy-mm-dd
* @param intDate-----相隔的天数,可为正数或负数,为正数时返回的是当前日期的 后intDate天的日期,
* 为负数时返回的是当前日期的前intDate的日期
* @return------返回为yyyy-mm-dd格式的日期字符串
*/
public static String DateOperate(String strDate,Integer intDate){
try{
String strYear = strDate.substring(0, 4);
String strMoth = strDate.substring(5, 7);
String strDay = strDate.substring(8, 10);
int year = Integer.parseInt(strYear);
int month = Integer.parseInt(strMoth);
int date = Integer.parseInt(strDay);
Calendar calenDar = new GregorianCalendar(year, month - 1, date);
calenDar.add(GregorianCalendar.DATE,intDate);
DateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
String currentTime=formatDate.format(calenDar.getTime());
return currentTime;
}catch(Exception e){
e.printStackTrace();
}
return null;
}

/**
* 日期格式转换,将yyyy/MM/dd日期格式转换成yyyy-MM-dd
* @param sourceDate 格式为yyyy/MM/dd
* @return 返回为yyyy-mm-dd格式的日期字符串
*/
public static String dateFormatConversion(String sourceDate) throws Exception{
String targetDate=sourceDate.replace("/", "-");
return targetDate;
}

/**
* 将日期转换成数字类型,用于日期比较
* @param sourceDate 格式为yyyy-MM-dd
* @return 返回一个整型
*/
public static int numberOfDate(String sourceDate){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 格式化日期
SimpleDateFormat datefd = new SimpleDateFormat("yyyyMMdd"); //日期格式转换---便于日期比较
try {
Date d = sdf.parse(sourceDate);
int newData = Integer.parseInt(datefd.format(d));
return newData;
} catch (ParseException e) {
e.printStackTrace();
}
return 0;
}

/*****
* 判断YYYY-MM-DD格式的字符是否为合法的日期格式
* @param sDate
* @return
*/
public static boolean isValidDate(String sDate) {
String datePattern1 = "\\d{4}-\\d{2}-\\d{2}";
String datePattern2 = "^((\\d{2}(([02468][048])|([13579][26]))"
+ "[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|"
+ "(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?"
+ "((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?("
+ "(((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?"
+ "((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))";
if ((sDate != null)) {
Pattern pattern = Pattern.compile(datePattern1);
Matcher match = pattern.matcher(sDate);
if (match.matches()) {
pattern = Pattern.compile(datePattern2);
match = pattern.matcher(sDate);
return match.matches();
}
else {
return false;
}
}
return false;
}

/**
* 通过开始日期 和 结束日期 算出他们的时间差 并返回分钟

* @param bDate
* @param eDate
* @return
* @author liguosheng 2012-2-15 下午07:53:31
*/
public static double getTimeDifference(String beginDate,String endDate) throws Exception{
SimpleDateFormat sim = NORMAL_FORMAT;
Date bDate = sim.parse(beginDate);
Date eDate = sim.parse(endDate);
return (eDate.getTime() - bDate.getTime())/1000/60 ;
}

/**
* 功能:一个日期时间加上分钟数,得到一个新的日期时间

* @param beginDateTime
* @param addMinutes
* @return
* @throws ParseException
* @author liguosheng 2012-2-16 上午09:01:10
*/
public static String getNewDate(String beginDateTime, long addMinutes)throws ParseException {
SimpleDateFormat sim = NORMAL_FORMAT;
Date d1 = sim.parse(beginDateTime);
long time = d1.getTime();
addMinutes = addMinutes * 60 * 1000;
time += addMinutes;
return sim.format(new Date(time));
}

/**
* 返回 yyyy-MM-dd hh:mm:ss 格式的日期
* @param date
* @return
* @author liguosheng 2012-2-16 上午10:22:14
*/
public static String getFormatLongDate(String date) throws ParseException{
SimpleDateFormat dateFormat = NORMAL_FORMAT;
return dateFormat.format(dateFormat.parse(date));
}


/**
* 返回 yyyy-MM-dd格式的日期
* @param date
* @return
* @author hsh 2012-7-04 16:17:00
*/
public static Date getFormatShortDate(String dateTime) throws ParseException{
return NORMAL_FORMATA.parse(dateTime);
}


/**
* 将日期参数转换成只有月份日期形式
* @param dateTime
* @return
* @throws ParseException
* @author hsh add on 2012-07-09 10:18:00
*/
public static String[] getMD_Format(Date dateTime)throws ParseException{
String[] mdArr = new String[4];
try{
mdArr[0] = MMDD_FORMAT.format(dateTime);
mdArr[1] = MMD_FORMAT.format(dateTime);
mdArr[2] = MDD_FORMAT.format(dateTime);
mdArr[3] = MD_FORMAT.format(dateTime);
}catch(Exception e){
e.printStackTrace();
}
return mdArr;
}

/**
* 获得指定日期的在一个星期中的处于星期几
* @param date 日期时间
* @return
*/
private String getWeekDayCode(Date date ){
Calendar cal = Calendar.getInstance();
cal.setTime(date);

//日历中每个星期是从星期日开始,因此当取到星期日是1,星期一是2,...星期六是7。
String weekDay = String.valueOf(cal.get(Calendar.DAY_OF_WEEK));
return weekDay ;
}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值