Date获取时间段

时间:2022-05-07 20:48:10
/**
*
*/
package com.chinabase.common.util; /**
* @author yuanji
* @created on:Sep 19, 2008
*/
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date; public class TimeFormat { /**
* 获得当前时间精确到毫秒
*
* @return
*/
public static String getTimestamp() {
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
return timestamp.toString();
} /**
* 生成文件夹名
*
* @return
*/
public static String toFileName() {
return getTimestamp().replaceAll("-| |:|\\.", "");
} /**
* 获得年-月-日
*
* @return
*/
public static String getDate() {
return getTimestamp().substring(0, 10);
} /**
*
* @return 获取当月的第一天
*/
public static String getFirstMonthDay(){
SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
Date da = new Date(date.getYear(),date.getMonth(),01);
Timestamp startTime=Timestamp.valueOf(df1.format(da));
return startTime.toLocaleString();
}
//获取当月的最后-天
public static String getLastDayOfMonth() {
Calendar cal = Calendar.getInstance();
SimpleDateFormat datef=new SimpleDateFormat("yyyy-MM-dd");
cal.set( Calendar.DATE, 1 );
cal.roll(Calendar.DATE, - 1 );
Date endTime=cal.getTime();
String endTime1=datef.format(endTime)+" 23:59:59";
return endTime1;
}
public static String getTime() {
return getTimestamp().substring(11);
} public static void main(String[] args) {
System.out.println(TimeFormat.getTimestamp());
System.out.println(TimeFormat.toFileName());
System.out.println(TimeFormat.getDate());
System.out.println(getTime());
System.out.println(getTime().substring(0, 8));
System.out.println(getLastDayOfMonth());
System.out.println(getFirstMonthDay());
}
}