package framework.util;
|
|
import java.text.SimpleDateFormat;
|
import java.util.Calendar;
|
import java.util.Date;
|
|
|
public final class TimeUtil {
|
public static final SimpleDateFormat yyyy_MM_dd=new SimpleDateFormat("yyyy-MM-dd");
|
public static final SimpleDateFormat yyyy_MM_dd_HH_mm_ss=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
public static final SimpleDateFormat TIME_FULL =new SimpleDateFormat("yyyyMMdd-HHmmss-SSS");
|
|
/**
|
* yyyy年MM月dd日
|
* @param date
|
* @return
|
*/
|
public static String getDateCnStr(Date date){
|
if(date==null){
|
return "";
|
}
|
SimpleDateFormat fmt = new SimpleDateFormat("yyyy年MM月dd日");
|
return fmt.format(date);
|
}
|
|
/**
|
* new Date().getTime()
|
*
|
* @return
|
*/
|
public static String getTime() {
|
return String.valueOf(new Date().getTime());
|
}
|
|
/**
|
* yyyy-MM-dd_HH_mm_ss
|
* @return
|
*/
|
public static String getLongTime() {
|
SimpleDateFormat fmt = new SimpleDateFormat(
|
"yyyy-MM-dd_HH_mm_ss");
|
return fmt.format(new Date());
|
}
|
/**
|
* yyyyMMdd
|
* @return
|
*/
|
public static String getDate8() {
|
SimpleDateFormat fmt = new SimpleDateFormat(
|
"yyyyMMdd");
|
return fmt.format(new Date());
|
}
|
/**
|
* yyyy-MM-dd
|
* @param date
|
* @return
|
*/
|
public static String getDate10(Date date){
|
if(date==null){
|
return "";
|
}
|
return yyyy_MM_dd.format(date);
|
}
|
|
/**
|
* yyyy-MM-dd HH:mm:ss
|
* @param date
|
* @return
|
*/
|
public static String getDate20(Date date) {
|
SimpleDateFormat fmt = new SimpleDateFormat(
|
"yyyy-MM-dd HH:mm:ss");
|
return fmt.format(date);
|
}
|
/**
|
* yyyy-MM-dd 00:00:00
|
* @param date
|
* @return
|
*/
|
public static Date getDateBegin(Date date) throws Exception {
|
String s = yyyy_MM_dd.format(date)+" 00:00:00";
|
return yyyy_MM_dd_HH_mm_ss.parse(s);
|
}
|
/**
|
* yyyy-MM-dd 00:00:00
|
* @param date
|
* @return
|
* @throws Exception
|
*/
|
public static Date getDateBegin(String date) throws Exception {
|
String s = date+" 00:00:00";
|
return yyyy_MM_dd_HH_mm_ss.parse(s);
|
}
|
/**
|
* yyyy-MM-dd 23:59:59
|
* @param date
|
* @return
|
* @throws Exception
|
*/
|
public static Date getDateEnd(Date date) throws Exception {
|
String s = yyyy_MM_dd.format(date)+" 23:59:59";
|
return yyyy_MM_dd_HH_mm_ss.parse(s);
|
}
|
/**
|
* yyyy-MM-dd 23:59:59
|
* @param date
|
* @return
|
* @throws Exception
|
*/
|
public static Date getDateEnd(String date) throws Exception {
|
String s = date+" 23:59:59";
|
return yyyy_MM_dd_HH_mm_ss.parse(s);
|
}
|
/**
|
* 天数计算
|
* @param date
|
* @param d 加减天数
|
* @return
|
*/
|
public static Date getDate(Date date, int d){
|
long t = date.getTime()+24*3600000*d;
|
return new Date(t);
|
}
|
|
/**
|
* 当月第一天
|
* @return
|
*/
|
public static String getCurrentMonthFirstDate(){
|
Calendar c = Calendar.getInstance();
|
int year = c.get(Calendar.YEAR);
|
int month = c.get(Calendar.MONTH)+1;
|
|
StringBuffer s = new StringBuffer();
|
s.append(year).append("-");
|
if(month<10){
|
s.append("0");
|
}
|
s.append(month).append("-01");
|
return s.toString();
|
}
|
|
/**
|
* 当月最后一天
|
* @return
|
*/
|
public static String getCurrentMonthLastDate(){
|
Calendar c = Calendar.getInstance();
|
c.add(Calendar.MONTH, 1);
|
c.set(Calendar.DATE, 1);
|
c.add(Calendar.DATE, -1);
|
|
return yyyy_MM_dd.format(c.getTime());
|
}
|
|
}
|