public static String formatDate(String inDate) {
SimpleDateFormat inTime = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
SimpleDateFormat outTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = "";
try{
date = outTime.format(inTime.parse(inDate));
}catch(Exception e){
e.printStackTrace();
}
return date;
}
/**
* 转换为年-月-日 时-分-秒 字符串格式时间
* @param date
* @return
*/
public static String formatYmdHmsDate(Date date) {
SimpleDateFormat sdfyMd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String ymdDate = sdfyMd.format(date);
return ymdDate;
}
/**
* 转换为年-月-日字符串格式时间
* @param date
* @return
*/
public static String formatYmdDate(Date date) {
SimpleDateFormat sdfyMd = new SimpleDateFormat("yyyy-MM-dd");
String ymdDate = sdfyMd.format(date);
return ymdDate;
}
/**
* 转换为年-月字符串格式时间
* @param date
* @return
*/
public static String formatYmDate(Date date) {
SimpleDateFormat sdfyM = new SimpleDateFormat("yyyy-MM");
String ymDate = sdfyM.format(date);
return ymDate;
}
/**
* 获取前一小时的开始时间
* @return
*/
public static String getOneHoursStartDate(){
Calendar calendar = new GregorianCalendar();
calendar.add(Calendar.HOUR_OF_DAY,-1);
calendar.set(Calendar.MINUTE,0);
calendar.set(Calendar.SECOND,0);
calendar.set(Calendar.MILLISECOND,0);
Date dayStart = calendar.getTime();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String startStr = simpleDateFormat.format(dayStart);
return startStr;
}
/**
* 获取前一小时的结束时间
* @return
*/
public static String getOneHoursEndDate(){
Calendar calendar = new GregorianCalendar();
calendar.add(Calendar.HOUR_OF_DAY,-1);
calendar.set(Calendar.MINUTE,59);
calendar.set(Calendar.SECOND,59);
calendar.set(Calendar.MILLISECOND,999);
Date dayEnd = calendar.getTime();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String endStr = simpleDateFormat.format(dayEnd);
return endStr;
}
/**
* 获取昨天的开始时间
* @return
*/
public static String getOneDayStartDate(){
Calendar calendar = new GregorianCalendar();
calendar.add(Calendar.DAY_OF_MONTH,-1);
calendar.set(Calendar.HOUR_OF_DAY,0);
calendar.set(Calendar.MINUTE,0);
calendar.set(Calendar.SECOND,0);
calendar.set(Calendar.MILLISECOND,0);
Date dayStart = calendar.getTime();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String startStr = simpleDateFormat.format(dayStart);
return startStr;
}
/**
* 获取昨天的结束时间
* @return
*/
public static String getOneDayEndDate(){
Calendar calendar = new GregorianCalendar();
calendar.add(Calendar.DAY_OF_MONTH,-1);
calendar.set(Calendar.HOUR_OF_DAY,23);
calendar.set(Calendar.MINUTE,59);
calendar.set(Calendar.SECOND,59);
calendar.set(Calendar.MILLISECOND,999);
Date dayEnd = calendar.getTime();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String endStr = simpleDateFormat.format(dayEnd);
return endStr;
}
/**
* 获取前一个月的开始时间
* @return
*/
public static String getOneMonthStartDate(){
Calendar calendar = new GregorianCalendar();
calendar.add(Calendar.MONTH,-1);
calendar.set(Calendar.DAY_OF_MONTH,1);
Date monthStart = calendar.getTime();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
String monthStartStr = simpleDateFormat.format(monthStart);
return monthStartStr;
}
/**
* 获取前一个月的结束时间
* @return
*/
public static String getOneMonthEndDate(){
Calendar calendar = new GregorianCalendar();
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
int month=calendar.get(Calendar.MONTH);
calendar.set(Calendar.MONTH, month-1);
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
String monthEndStr = simpleDateFormat.format(calendar.getTime());
return monthEndStr;
}
/**
* 随机一段时间
* @param beginDate
* @param endDate
* @return
*/
public static Date randomDate(String beginDate, String endDate) {
try {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date start = format.parse(beginDate);// 构造开始日期
Date end = format.parse(endDate);// 构造结束日期
// getTime()表示返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数。
if (start.getTime() >= end.getTime()) {
return null;
}
long date = random(start.getTime(), end.getTime());
return new Date(date);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 递归调用随机随机时间
* @param begin
* @param end
* @return
*/
public static long random(long begin, long end) {
long rtn = begin + (long) (Math.random() * (end - begin));
// 如果返回的是开始时间和结束时间,则递归调用本函数查找随机值
if (rtn == begin || rtn == end) {
return random(begin, end);
}
return rtn;
}
/**
* 遍历一小时的随机时间
* @return
*/
public static String getOneHoursDate(){
Date randomDate = randomDate(getOneHoursStartDate(), getOneHoursEndDate());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = sdf.format(randomDate);
System.out.println(date);
return date;
}
/**
* 遍历一天的随机时间(24H)
* @return
*/
public static String getOneDayDate(){
Date randomDate = randomDate(getOneDayStartDate(), getOneDayEndDate());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = sdf.format(randomDate);
System.out.println(date);
return date;
}
public static void main(String[] args) {
for (int i = 0; i < Math.round(5) ; i++) {
getOneHoursDate();
}
// for (int i = 0; i < 8 ; i++) {
// Date randomDate = randomDate("2019-10-01 00:00:00", "2019-10-01 23:59:59");
// SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// String date = format.format(randomDate);
// System.out.println(date);
// }
// Calendar calendar = new GregorianCalendar();
// calendar.add(Calendar.HOUR_OF_DAY,-1);
// calendar.set(Calendar.MINUTE,59);
// calendar.set(Calendar.SECOND,59);
// calendar.set(Calendar.MILLISECOND,999);
// Date dayEnd = calendar.getTime();
// SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// String endStr = simpleDateFormat.format(dayEnd);
//
// System.out.println(endStr);
}