package com.yc.utils;
|
|
import java.text.SimpleDateFormat;
|
import java.util.Calendar;
|
import java.util.Date;
|
import java.util.GregorianCalendar;
|
/**
|
* @author 沈奕洲
|
* 2011-11-16
|
*/
|
public class DateChange {
|
|
private static String strDateSeparator = "-"; //日期分隔符
|
|
/**
|
* 格式化日期
|
* @param strDate 字符串的日期格式
|
* @return 如果格式正确,则返回正确的日期;否则,返回""。
|
*/
|
public static String formatDate(String strDate)
|
{
|
String result = "";
|
Date date = null;
|
try
|
{
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy"+strDateSeparator+"MM"+strDateSeparator+"dd");
|
date = sdf.parse(strDate);
|
result = formatDate(date);
|
}
|
catch(Exception e)
|
{
|
//System.out.println("date:" + e.getMessage());
|
}
|
return result;
|
}
|
|
/**
|
* 格式化日期
|
*
|
* @param date Date
|
* @return String
|
*/
|
public static String formatDate(Date date)
|
{
|
return formatDateByFormat(date,"yyyy"+strDateSeparator+"MM"+strDateSeparator+"dd");
|
}
|
|
/**
|
* 格式化今天日期
|
* @return
|
*/
|
public static String formatDate()
|
{
|
Date dToday = new Date();
|
return formatDate(dToday);
|
}
|
|
public static String formatDateTime(Date date)
|
{
|
return formatDateByFormat(date,"yyyy"+strDateSeparator+"MM"+strDateSeparator+"dd HH:mm:ss");
|
}
|
|
public static String formatShortDateTime(Date date)
|
{
|
return formatDateByFormat(date,"yyyy"+strDateSeparator+"MM"+strDateSeparator+"dd");
|
}
|
|
/**
|
* 以指定的格式来格式化日期
|
*
|
* @param date Date
|
* @param format String
|
* @return String
|
*/
|
public static String formatDateByFormat(Date date,String format)
|
{
|
String result = "";
|
if(date != null)
|
{
|
try
|
{
|
SimpleDateFormat sdf = new SimpleDateFormat(format);
|
result = sdf.format(date);
|
}
|
catch(Exception ex)
|
{
|
//System.out.println("date:" + date);
|
ex.printStackTrace();
|
}
|
}
|
return result;
|
}
|
|
|
/**
|
* 紧缩格式化日期
|
*
|
* @param date Date
|
* @return String
|
*/
|
public static String formatDateSN(Date date)
|
{
|
return formatDateByFormat(date,"yyyyMMdd");
|
}
|
|
/**
|
* 取今天的紧缩格式化日期
|
* @return 八位yyyyMMdd格式的字符
|
*/
|
public static String formatDateSN()
|
{
|
Date dToday = new Date();
|
return formatDateSN(dToday);
|
}
|
|
/**
|
* 根据字符串解析日期
|
*
|
* @param strDate String
|
* @param division String
|
* @return Date
|
*/
|
public static Date parseFormatDateTime(String strDate)
|
{
|
Date date = null;
|
try
|
{
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy"+strDateSeparator+"MM"+strDateSeparator+"dd HH:mm:ss");
|
date = sdf.parse(strDate);
|
}
|
catch(Exception ex)
|
{
|
ex.printStackTrace();
|
}
|
return date;
|
}
|
|
/**
|
* 根据字符串解析日期
|
*
|
* @param strDate String
|
* @param division String
|
* @return Date
|
*/
|
public static String parseFormatDateTime_str(String strDate)
|
{
|
Date date = null;
|
String sDate="";
|
try
|
{
|
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
|
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy"+strDateSeparator+"MM"+strDateSeparator+"dd HH:mm:ss");
|
date = sdf.parse(strDate);
|
sDate=sdf1.format(date);
|
}
|
catch(Exception ex)
|
{
|
ex.printStackTrace();
|
}
|
return sDate;
|
}
|
|
/**
|
* 日期变换函数
|
* @param dateformat 日期格式字符串:2005-09-13 14:17:06
|
* @param iDay 日期天数 3
|
* @return 2005-09-16 14:17:06
|
*/
|
public static String GetDate(String dateformat,int iDay)
|
{
|
String strOut="";
|
Date date = parseFormatDateTime(dateformat);
|
if(date!=null){
|
GregorianCalendar worldTour = new GregorianCalendar(date.getYear()+1900,date.getMonth(),date.getDate());
|
worldTour.add(Calendar.DATE,iDay);
|
Date d = worldTour.getTime();
|
strOut = formatDateByFormat(d,"yyyy"+strDateSeparator+"MM"+strDateSeparator+"dd")+" "+"00:00:00";
|
//formatDateByFormat(date,"HH:mm:ss");
|
}
|
return strOut;
|
}
|
|
|
}
|