import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateUtil {
/**
*
* <p>
* 简述:获取当天时间 str
* </p>
* <p>
* 描述:
* </p>
* @param dateformat "yyyy-MM-dd" "yyyy-MM-dd HH:mm:ss"
* @return
* @see [类、类#方法、类#成员]
*/
public static String getNowTimeStr(String dateformat){
Date now = new Date();
//可以方便地修改日期格式
SimpleDateFormat dateFormat = new SimpleDateFormat(dateformat);
String hehe = dateFormat.format(now);
return hehe;
}
/**
*
* <p>
* 简述:获取当天时间 date
* </p>
* <p>
* 描述:
* </p>
* @param dateformat "yyyy-MM-dd" "yyyy-MM-dd HH:mm:ss"
* @return
* @see [类、类#方法、类#成员]
*/
public static Date getNowTimeDate(String dateformat){
Date now = new Date();
//可以方便地修改日期格式
SimpleDateFormat dateFormat = new SimpleDateFormat(dateformat);
String hehe = dateFormat.format(now);
return getStrToDate(hehe, dateformat);
}
/**
*
* <p>
* 简述:获取距今天的指定天数
* </p>
* <p>
* 描述:
* </p>
* @param index 指定天数 (今天之前的天数是负数,今天之后的是正数)
* @return
* @see [类、类#方法、类#成员]
*/
public static String getDayByIndex(int index) {
Calendar c = Calendar.getInstance();
Date date = new Date(System.currentTimeMillis());
c.setTime(date);
int day = c.get(Calendar.DATE);
c.set(Calendar.DATE, day + index);
String dayAfter = new SimpleDateFormat("yyyy-MM-dd")
.format(c.getTime());
return dayAfter;
}
/**
*
* <p>
* 简述:转换日期成字符串
* </p>
* <p>
* 描述:
* </p>
* @param date 日期类型
* @param format "yyyy-MM-dd" "yyyy-MM-dd HH:mm:ss"
* @return
* @see [类、类#方法、类#成员]
*/
public static String getDateToStr(Date date, String format) {
return new SimpleDateFormat(format).format(date);
}