import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.UUID;
public class DateUtils {
/**
* 获取未来 第 past 天的日期
* @param past
* @return
*/
public static String getFetureDate(int past) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + past);
Date today = calendar.getTime();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String result = format.format(today);
return result;
}
/**
* 获取当前日期 年月日
* @date: 2020年3月17日 下午6:45:00
* @Title: getTime_yMd
* @Description: TODO(描述这个方法的作用)
* @param: @return
* @return: String
* @throws
*/
public static String getTime_yMd(){
SimpleDateFormat sdf_ymd = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
String nowTimeStr = sdf_ymd.format(date);
return nowTimeStr;
}
/**
* 获取当前日期 年月日时分秒
* @date: 2020年3月17日 下午6:45:14
* @Title: getTime_yMd_Hms
* @param: @return
* @return: String
* @throws
*/
public static String getTime_yMd_Hms(){
SimpleDateFormat sdf_ymd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
String nowTimeStr = sdf_ymd.format(date);
return nowTimeStr;
}
/**
* 获取当前日期前一天
*/
public static String getDateoneTime(){
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.setTime(new Date());
Date end = c.getTime();
String dqrq= format.format(end);//当前日期
c.add(Calendar.DATE, -1);
Date start = c.getTime();
String qyt= format.format(start);//前一天
return qyt;
}
/**
* 指定日期后几天
*
* @param startDay 指定日期
* @param count 天数
* @return String
*/
public static String getAfterDay(String startDay, int count) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date date = sdf.parse(startDay);
Calendar cl = Calendar.getInstance();
cl.setTime(date);
cl.add(Calendar.DATE, count);
return sdf.format(cl.getTime());
} catch (ParseException e) {
e.printStackTrace();
}
}
/**
* 指定日期前几天
*/
public static String getBeforeDay(String startDay, int count) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
Date date = sdf.parse(startDay);
Calendar cl = Calendar.getInstance();
cl.setTime(date);
cl.set(Calendar.DATE, cl.get(Calendar.DATE) - count);
return sdf.format(cl.getTime());
} catch (ParseException e) {
e.printStackTrace();
}
return "";
}
/**
* 获取当前日期的当前小时
*/
public static String getDayTime_HH(){
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("HH");
String time = sdf.format(date);
System.out.println(time);
return time;
}
/**
* 获取下个月第一天
* @return: String
* @throws
*/
public static String getMonth_firstDay_YMD(){
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//获取前月的第一天
Calendar cal_1=Calendar.getInstance();//获取当前日期
cal_1.add(Calendar.MONTH, +1);
cal_1.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
String firstDay = format.format(cal_1.getTime());
return firstDay;
}
/**
* 获取下个月最后一天
* @param: @return
* @return: String
* @throws
*/
public static String getMonth_last_YMD(){
Date date=new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(df.format(date));
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MONTH, 2);
calendar.set(Calendar.DATE, 0);
return df.format(calendar.getTime());
}
/**
* 获取当前月第一天
* @param month
* @return
*/
public static String getFirstDayOfMonth() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Calendar cale = null;
cale = Calendar.getInstance();
cale = Calendar.getInstance();
cale.add(Calendar.MONTH, 0);
cale.set(Calendar.DAY_OF_MONTH, 1);
String firstday = format.format(cale.getTime());
return firstday;
}
/**
* 获取当前月最后第一天
* @param month
* @return
*/
public static String getLastdayDayOfMonth() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Calendar cale = null;
cale = Calendar.getInstance();
cale.add(Calendar.MONTH, 1);
cale.set(Calendar.DAY_OF_MONTH, 0);
String lastday = format.format(cale.getTime());
return lastday;
}
//生成日期的数字形式
public static String getTime_ymd_num() throws Exception {
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
String str=sdf.format(new Date());
System.out.print(str);
return str;
}
}
Java 常用日期总结(当前日期前几天、后几天、当月第一天、当月总后一天、未来第几天日期等)
最新推荐文章于 2022-11-24 11:08:45 发布