您的位置:首页 > 编程语言 > Java开发

Java日期工具类一

2014-08-23 17:23 399 查看
public class DateTimeUtil {

/**
* Timestamp 转换成String
* @param time 要转换的Timestamp 时间
* @param format  要转换成的格式
* @return  转换失败返回 空字符串
*/
public static String timestampToString(Timestamp time,String format)
{

SimpleDateFormat formatter = new SimpleDateFormat(format);
String dateString = formatter.format(time);
return dateString;

}
/**
* 字符串转换成timestamp
* @param dateTime  要转换的时间字符串
* @return    转换失败返回 null
* @throws ParseException
*/
public static Timestamp stringToTimestamp(String dateTime,String format) throws ParseException
{

SimpleDateFormat df1 = new SimpleDateFormat(format);
Date date11 = df1.parse(dateTime);
Timestamp ts =new Timestamp(date11.getTime());
return ts;

}
/**
* 字符串转换成timestamp
* @param dateTime  要转换的时间字符串
* @return    转换失败返回 null
* @throws ParseException
*/
public static Timestamp stringToTimestamp(String dateTime) throws ParseException
{

SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date11 = df1.parse(dateTime);
//            String time = df1.format(date11);
Timestamp ts = new Timestamp(date11.getTime());
return ts;

}
/**
* 计算两个日期的时间差精确到秒
* @param time1  最晚的时间
* @param time2  最早的时间
* @return  返回相差的时间字符串
*/
public static String getTimeDifference(Date time1,Date time2)
{
try
{
SimpleDateFormat timeformat = new SimpleDateFormat("yyyy-MM-dd,HH:mm:ss");
long t1 = 0L;
long t2 = 0L;
t1 = timeformat.parse(getDateNumberFormat(time1)).getTime();
t2= timeformat.parse(getDateNumberFormat(time2)).getTime();

//因为t1-t2得到的是毫秒级,所以要除3600000得出小时.算天数或秒同理

int hours=(int) ((t1 - t2)/3600000);
int minutes=(int) (((t1 - t2)/1000-hours*3600)/60);
int seconds= (int) (((t1 - t2)/1000-hours*3600));
int day= hours/24;
int month=day/30;
int year=month/12;
if(year<0){

year=0;
}
if(month<0){
month=0;

}
if(day<0){

day=0;
}
if(hours<0){

hours=0;
}
if(minutes<0){

minutes=0;
}
if(seconds<0){

seconds=0;
}
String ret="0秒前";
if(year>0)
{
ret=dateToString(time2,"yyyy-MM-dd");
}
else if(month>0)
{
ret=dateToString(time2,"yyyy-MM-dd");
}
else if(day>0)
{
if(day>7)
{
ret=dateToString(time2,"yyyy-MM-dd");
}
else
{
ret =day+"天前";
}
}
else if(hours>0)
{
ret=hours+"小时前";
}
else if(minutes>0)
{
ret =minutes+"分钟前";
}
else if(seconds>0)
{
ret =seconds+"秒前";
}
return ret;
}
catch (Exception ex)
{
return "0秒前";
}
}
/**
* 计算两个日期的时间差精确到秒
* @param time1  最晚的时间
* @param time2  最早的时间
* @return  返回相差的秒数
*/
public static int getTimeDifferenceSeconds(Timestamp time1, Timestamp time2) throws Exception{
SimpleDateFormat timeformat = new SimpleDateFormat("yyyy-MM-dd,HH:mm:ss");
long t1 = 0L;
long t2 = 0L;

t1 = timeformat.parse(getTimeStampNumberFormat(time1)).getTime();

t2 = timeformat.parse(getTimeStampNumberFormat(time2)).getTime();

Long diff =t1 - t2;   //两时间差,精确到毫秒
int num=(int)(diff/1000);
return num;
}

/**
* 格式化时间
* Locale是设置语言敏感操作
* @param formatTime
* @return
*/
private static String getTimeStampNumberFormat(Timestamp formatTime) {
SimpleDateFormat m_format = new SimpleDateFormat("yyyy-MM-dd,HH:mm:ss", new Locale("zh", "cn"));
return m_format.format(formatTime);
}
/**
* 格式化时间
* Locale是设置语言敏感操作
* @param formatTime
* @return
*/
private static String getDateNumberFormat(Date formatTime) {
SimpleDateFormat m_format = new SimpleDateFormat("yyyy-MM-dd,HH:mm:ss", new Locale("zh", "cn"));
return m_format.format(formatTime);
}
/**
* 获取当前时间 默认格式为 "yyyy-MM-dd HH:mm:ss"
* @return 返回格式化后的当前时间
*/
public static String getCurrentTime() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(new Date());
}
/**
* 获取当前时间
* @param format 时间格式
* @return 如果格式有错误,返回空字符串
*/
public static String getCurrentTime(String format) {

SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(new Date());

}

/**
* 字符串格式转换成date格式
* @param time 要转换的时间
* @param format 要转换成的时间格式
* @return 转换失败返回null
*/
public static Date stringToDate(String time,String format) {
if(format==null || format.length()==0) {
format="yyyy-MM-dd HH:mm:ss";
}

SimpleDateFormat formatter = new SimpleDateFormat(format);
ParsePosition pos = new ParsePosition(0);
Date strtodate = formatter.parse(time, pos);
return strtodate;

}
/**
* date格式转换成字符串格式
* @param time 要转换的时间
* @param format 要转换成的时间格式
* @return 转换失败返回空字符串
*/
public static String dateToString(Date time,String format) {

try
{
SimpleDateFormat formatter = new SimpleDateFormat(format);
String dateString = formatter.format(time);
return dateString;

}
catch (Exception ex)
{
return "";
}
}

/**
* 计算某年某周到开始日期
* @param year
* @param week
* @return
* @throws ParseException
*/
public static String getYearWeekFirstDay(int year,int week)throws ParseException
{
Calendar cal=Calendar.getInstance();
cal.set(Calendar.YEAR,year);
cal.set(Calendar.WEEK_OF_YEAR,week);
cal.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);//周一第一天
//分别取得当前的日期的年月日
String tempYear=Integer.toString(year);
String tempMonth=Integer.toString(cal.get(Calendar.MONTH)+1);
String tempDay=Integer.toString(cal.get(Calendar.DATE));
if(week==1&&tempMonth.equals("12"))
{
//第一周,并且月是12月,开始日期应该为上一年
tempYear=Integer.toString(year-1);
}
String date=tempYear+"-"+tempMonth+"-"+tempDay;
return formatDateTime(date,"yyyy-MM-dd");
}
public static String getYearWeekEndDay(int year,int week)throws ParseException
{
Calendar cal=Calendar.getInstance();
cal.set(Calendar.YEAR,year);
cal.set(Calendar.WEEK_OF_YEAR,week+1);
cal.set(Calendar.DAY_OF_WEEK,Calendar.SUNDAY);//周日
//分别取得当前的日期的年月日
String tempYear=Integer.toString(year);
String tempMonth=Integer.toString(cal.get(Calendar.MONTH)+1);
String tempDay=Integer.toString(cal.get(Calendar.DATE));

String date=tempYear+"-"+tempMonth+"-"+tempDay;
return formatDateTime(date,"yyyy-MM-dd");
}
public static String formatDateTime(String date,String format)th
4000
rows ParseException
{
SimpleDateFormat sdf=new SimpleDateFormat(format);
String sDate=sdf.format(sdf.parse(date));
return  sDate;
}

public static void main(String args[]){
//    	System.out.println(compareCurTime());
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Java