需求:获取当天日期到当前访问的时间段,如当日是2024-02-19,那么此刻访问,日期期间就是2024-02-19 00:00:00到2024-02-19 16:58:00
代码如下:
Date date=new Date();
String currentDate=date2Str(date,"yyyy-MM-dd");
String currentTime=date2Str(date,"yyyy-MM-dd HH:mm:ss");
String startTime=currentDate+" "+ "00:00:00";
String endTime=currentTime;
//--------------------------------------------------------------
public String date2Str(Date date, String format) {
if ((format == null) || format.equals("")) {
format = "yyyy-MM-dd HH:mm:ss";
}
SimpleDateFormat sdf = new SimpleDateFormat(format);
if (date != null) {
return sdf.format(date);
}
return "";
}
2.获取当日时间段同理,代码如下:
Date date=new Date();
String currentDate=date2Str(date,"yyyy-MM-dd");
String startTime=currentDate+" "+ "00:00:00";
String endTime=currentDate+" "+ "23:59:59";
//--------------------------------------------------------------
public String date2Str(Date date, String format) {
if ((format == null) || format.equals("")) {
format = "yyyy-MM-dd HH:mm:ss";
}
SimpleDateFormat sdf = new SimpleDateFormat(format);
if (date != null) {
return sdf.format(date);
}
return "";
}