/**
* 或者字符串类型的半天时间间隔
*/
public static String getHalfDateOfString(){
//现在的时间
Date startTime= new Date();
//时间格式转换器
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//用StringBuilder进行字符串拼接
StrBuilder builder = new StrBuilder();
//用DateUtils类中的getHour获取时间的小时数
//DateUtils是一个网上下载的工具类,下面我附上代码
if (DateTimeUtil.getHour(startTime)>12) {
builder.append(format.format(startTime)+ " 12:00:00 AND"+format.format(startTime)+" 23:59:59");
}else {
builder.append(format.format(startTime)+" 00:00:00 AND"+format.format(startTime)+" 11:59:59 ");
}
System.out.println(builder);
return builder.toString();
}
/**
* 获取时间的小时
* @param date
* @return
*/
public static Integer getHour(Date date){
if(date==null)
{
return null;
}
return getCalendar(date).get(Calendar.HOUR_OF_DAY);
}
public static Calendar getCalendar(Date date){
if(date==null)
{
return null;
}
DateFormat df = DateFormat.getDateInstance();
df.format(date);
return df.getCalendar();
}
public static Map<String,Date> getHalfDateOfDate(){
String string = DateTimeUtil.getHalfDateOfString();
String[] dateStings = string.split("AND");
Date startDate = DateTimeUtil.parseStrToDate(dateStings[0], STANDARD_FORMAT);
Date endDate = DateTimeUtil.parseStrToDate(dateStings[1], STANDARD_FORMAT);
Map<String,Date> map = new HashMap<>();
map.put("startDate", startDate);
map.put("endDate", endDate);
return map;
}
输出结果如下
Tue Mar 23 12:00:00 CST 2021 Tue Mar 23 23:59:59 CST 2021