Elasticsearch(ES)有两种连接方式:transport、rest。transport通过TCP方式访问ES(只支持java),rest方式通过http API 访问ES(没有语言限制)。
transport client 使用TCP方式访问,默认端口是9300;
rest client 使用HTTP方式访问,默认端口是9200;
实际端口可以查看ES的配置文件application.yml 中的配置项
transport.tcp.port:39300
http.port:39200
本文假定你已经对Elasticsearch 的基本概念有比较全面的认识,然后演示如何使用Elasticsearch 提供的Java API。
通过官方文档可以得知,现在存在至少三种Java客户端。
1. Transport Client
TransportClient旨在被Java High-level REST client接口取代。 在 Elasticsearch 7.*版本中将不赞成使用TransportClient,在Elasticsearch 8.0 版本中将被移除,建议使用Java High-level REST Client客户端。
2. Java High Level REST Client
它基于Low-level REST Client接口,并暴露了特定的API方法,负责处理请求的序列化和响应的反序列化。
使用Java High Level REST Client操作最新版Elasticsearch 7.3.0
3. Java Low Level REST Client
它允许HTTP和Elasticsearch集群通信,并将请求的序列化和响应的反序列化交给用户自己处理。官方提供low-level rest client(支持5.0及以后版本) 和high-level rest client(支持版本为 5.6及以后版本),
low-level REST 客户端与 elasticsearch 的发布周期相同。可以使用版本替换,但必须是 5.0.0-alpha4 之后的版本。客户端版本与 Elasticsearch 服务版本之间没有关联。low-level REST 客户端兼容所有 Elasticsearch 版本。客户端和集群的版本,并不强烈要求一致,但是根据经验还是当版本一致时,出现问题能够快速定位。因为不同版本的API不一样,高版本的客户端不能调用低版本的服务端。
Transport Client maven 依赖
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
5.0之前没有提供rest client jar包,RestClient已经包含在elasticsearch 的jar包里,如下图:
Rest Client(已引入transport不能够再引入 rest,包会发生冲突)
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>6.5.4</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>6.5.4</version>
</dependency>
下面是TransportClient 的使用示例
服务器ES用的是2.1.1版本
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>2.1.1</version>
</dependency>
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.net.InetAddress;
import java.net.UnknownHostException;
@Configuration
public class ElasticsearchConfig {
private static Logger logger = LoggerFactory.getLogger(ElasticsearchConfig.class);
@Value("${spring.elasticsearch.ip}")
private String hostlist;
@Value("${elasticsearch.cluster.name}")
private String clusterName;
@Bean
public TransportClient transportClient() {
//设置集群名称
Settings settings = Settings.settingsBuilder().put("cluster.name", clusterName).build();
//创建RestClient客户端
TransportClient client = TransportClient.builder().settings(settings).build();
String[] split = hostlist.split(",");
// 创建HttpHost数组,其中存放es主机和端口的配置信息
for (int i = 0; i < split.length; i++) {
String item = split[i];
try {
int port = Integer.parseInt(item.split(":")[1]);
client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(item.split(":")[0]), port));
} catch (UnknownHostException e) {
logger.error("初始化client异常!", e);
}
}
return client;
}
}
import com.hikvision.js.hikmanager.param.VehicleStaParam;
import com.hikvision.js.hikmanager.service.VehicleStaService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author
* @description
* @date 2022/3/9
*/
@Api(value = "过车数据", description = "过车数据")
@RestController
@RequestMapping("/vehiclepass")
public class VehiclePassController {
private String index = "vehicle_data_index_20210830";
private String fieldSta = "plateno";
private String fieldRange = "passtime";
@Autowired
private VehicleStaService vehicleStaService;
@PostMapping("/vehiclePassExport")
public void vehiclePassExport(@RequestBody VehicleStaParam vehicleStaParam, HttpServletResponse response, HttpServletRequest request) {
String crossingIds = vehicleStaParam.getCrossingIds();
String startTime = vehicleStaParam.getStartTime();
String endTime = vehicleStaParam.getEndTime();
//导出数据
vehicleStaService.vehiclePassExport(response, index, fieldSta, fieldRange, startTime, endTime, crossingIds);
}
}
import com.hikvision.js.hikmanager.common.utils.DateUtil;
import com.hikvision.js.hikmanager.common.utils.ExcelUtil;
import com.hikvision.js.hikmanager.common.utils.es.base.PageInfo;
import com.hikvision.js.hikmanager.dto.VehiclePass;
import com.hikvision.js.hikmanager.service.VehicleStaService;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbookType;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.index.query.TermsQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
/**
* @author
* @ClassName:
* @Description: 服务层
* @date 2018/5/18 14:57
*/
@Service
public class VehicleStaServiceImpl implements VehicleStaService {
private static Logger logger = LoggerFactory.getLogger(VehicleStaServiceImpl.class);
private static final int NUM_PER_SHEET = 60000;
private static final Integer pageNo = 1;
private static final Integer PAGE_SIZE = 10000;
private String indexPrefix = "traffic-";
private String indexsufix = "00";
@Value("${service.excelDir}")
private String excelsPath;
@Autowired
private TransportClient transportClient;
@Override
public PageInfo staVehiclePass(String index, String fieldSta, String fieldRange, Integer pageNo, Integer pageSize, String startTime, String endTime, String crossingIds) {
index = getIndex(startTime, endTime);
startTime = DateUtil.fromTimeToTimestampMs(startTime);
endTime = DateUtil.fromTimeToTimestampMs(endTime);
String[] crossingIdArray = crossingIds.split(",");
List<String> crossingIdList = Arrays.asList(crossingIdArray);
PageInfo pageInfo = null;
try {
SearchRequest searchRequest = new SearchRequest(index);
//在某个范围条件下
RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery(fieldRange).gte(startTime).lte(endTime);
TermsQueryBuilder termsQueryBuilder = QueryBuilders.termsQuery("crossingid", crossingIdList);
//测试用
// TermsQueryBuilder termsQueryBuilder = QueryBuilders.termsQuery("crossing_id", crossingIdList);
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery().must(rangeQueryBuilder).must(termsQueryBuilder);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(boolQueryBuilder);
searchSourceBuilder.from((pageNo - 1) * pageSize);
searchSourceBuilder.size(pageSize);
String searchString = searchSourceBuilder.toString();
logger.info("执行语句:" + searchString);
searchRequest.source(searchSourceBuilder);
SearchResponse response = null;
try {
response = transportClient.search(searchRequest).actionGet();
} catch (Exception e) {
logger.error("第"+pageNo+"页查询异常!", e);
}
pageInfo = new PageInfo();
long total = 0L;
List<Map> mapList = new ArrayList<>();
List<VehiclePass> vehiclePassesList = new ArrayList<>();
total = response.getHits().totalHits();
for (SearchHit searchHit : response.getHits()) {
Map<String, Object> tempMap = searchHit.getSource();
VehiclePass tempVehiclePass = new VehiclePass();
tempVehiclePass.setPlateNo(tempMap.get("plateno").toString());
tempVehiclePass.setCrossId(tempMap.get("crossingid").toString());
tempVehiclePass.setPlateColor(tempMap.get("platecolor").toString());
String passtTimeStamp = tempMap.get("passtime").toString();
String passTime = DateUtil.fromTimestampMsToTime(passtTimeStamp);
tempVehiclePass.setPassTime(passTime);
vehiclePassesList.add(tempVehiclePass);
}
pageInfo.setPageNo(pageNo);
pageInfo.setPageSize(pageSize);
pageInfo.setTotal(total);
pageInfo.setList(vehiclePassesList);
} catch (Exception e) {
logger.error("分页查询异常!",e);
}
return pageInfo;
}
/**
* 功能描述 获取所有符合条件的过车数据
*
* @param index
* @param fieldSta
* @param fieldRange
* @param startTime
* @param endTime
* @param crossingIds
* @return java.util.List<com.hikvision.js.hikmanager.dto.VehiclePass>
* @author wll8
* @date 2022/3/14
*/
public List<VehiclePass> getAllVehiclePass(String index, String fieldSta, String fieldRange, String startTime, String endTime, String crossingIds) {
Integer pageSize = PAGE_SIZE;
List<VehiclePass> vehiclePasses = new ArrayList<>();
PageInfo pageInfo = new PageInfo();
pageInfo = staVehiclePass(index, fieldSta, fieldRange, 1, pageSize, startTime, endTime, crossingIds);
List<VehiclePass> vehiclePasseList = (List<VehiclePass>) pageInfo.getList();
vehiclePasses.addAll(vehiclePasseList);
long total = pageInfo.getTotal();
if (total > pageSize) {
int totalfPage = (int) (total % pageSize == 0 ? total / pageSize : total / pageSize + 1);
for (int page = 2; page <= totalfPage; page++) {
PageInfo tempPageInfo = staVehiclePass(index, fieldSta, fieldRange, page, pageSize, startTime, endTime, crossingIds);
List<VehiclePass> tempVehiclePasseList = (List<VehiclePass>) tempPageInfo.getList();
vehiclePasses.addAll(tempVehiclePasseList);
}
}
return vehiclePasses;
}
public List<List<String>> vehiclePassToListString(List<VehiclePass> vehiclePasses) {
List<List<String>> dataList = new ArrayList<List<String>>();
try {
for (VehiclePass vehiclePass : vehiclePasses) {
List rowData = new ArrayList();
rowData.add(vehiclePass.getPlateNo());
rowData.add(vehiclePass.getPlateColor());
rowData.add(vehiclePass.getPassTime());
rowData.add(vehiclePass.getCrossId());
dataList.add(rowData);
}
} catch (Exception e) {
logger.error("过车数据转化为List发生异常!", e);
}
return dataList;
}
public void vehiclePassExport(HttpServletResponse response, String index, String fieldSta, String fieldRange, String startTime, String endTime, String crossingIds) {
List<VehiclePass> vehiclePasses = getAllVehiclePass(index, fieldSta, fieldRange, startTime, endTime, crossingIds);
List<List<String>> dataList = vehiclePassToListString(vehiclePasses);
try {
response.setHeader("Content-Disposition", "attachment; filename=" + new String(("过车数据").getBytes(), StandardCharsets.UTF_8) + ".xlsx");
response.setHeader("Connection", "close");
response.setCharacterEncoding("utf-8");
//不能设置为application/vnd.ms-excel,该格式默认下载的是xls格式的
//response.setHeader("Content-Type", "application/vnd.ms-excel");
response.setHeader("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
OutputStream ouputStream = response.getOutputStream();
String[] headers = {"车牌号", "车牌颜色", "过车时间", "卡口"};
XSSFWorkbook workbook = new XSSFWorkbook();
workbook.setWorkbookType(XSSFWorkbookType.XLSX);
// ExcelUtil.exportExcel(workbook, 0, "第一页", headers, data1, ouputStream);
// ExcelUtil.exportExcel(workbook, 1, "第二页", headers, data2, ouputStream);
long totalData = dataList.size();
long tlatlSheet = totalData % NUM_PER_SHEET == 0 ? totalData / NUM_PER_SHEET : totalData / NUM_PER_SHEET + 1;
for (int sheetNum = 1; sheetNum <= tlatlSheet; sheetNum++) {
List<List<String>> data = new ArrayList<>();
if (sheetNum != tlatlSheet) {
data = dataList.subList(NUM_PER_SHEET * (sheetNum - 1), NUM_PER_SHEET * sheetNum);
} else {
data = dataList.subList(NUM_PER_SHEET * (sheetNum - 1), dataList.size());
}
ExcelUtil.exportExcel(workbook, sheetNum - 1, "第" + sheetNum + "页", headers, data, ouputStream);
}
workbook.write(ouputStream);
ouputStream.close();
} catch (IOException e) {
logger.error("关闭流异常!", e);
}
}
//根据规则组建index
private String getIndex(String startTime, String endTime) {
String index = "";
String startDate = null;
String endDate = null;
StringBuilder indexSb = new StringBuilder();
startDate = DateUtil.getBeforeThursday(startTime);
endDate = DateUtil.getAfterThursday(endTime);
String[] week = {"5"};
List<String> dateList = DateUtil.getNeedDate(startTime, endTime, week);
if (!CollectionUtils.isEmpty(dateList)) {
if (1 == dateList.size()) {
indexSb.append(indexPrefix + startDate + indexsufix + "-" + dateList.get(0) + indexsufix + ",").append(indexPrefix + dateList.get(0) + indexsufix + "-" + endDate + indexsufix);
} else {
indexSb.append(indexPrefix + startDate + indexsufix + "-" + dateList.get(0) + indexsufix + ",");
for (int i = 0; i < dateList.size() - 1; i++) {
indexSb.append(indexPrefix + dateList.get(i) + indexsufix + "-" + dateList.get(i + 1) + indexsufix + ",");
}
indexSb.append(indexPrefix + dateList.get(dateList.size() - 1) + indexsufix + "-" + endDate + indexsufix);
}
} else {
indexSb.append(indexPrefix + startDate + indexsufix + "-" + endDate + indexsufix);
}
index = indexSb.substring(0, indexSb.length());
logger.info("组建的index值:" + index);
return index;
}
}
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.xssf.usermodel.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;
/**
* 使用EasyExcel导出
*/
@Component
public class ExcelUtil {
private static Logger logger = LoggerFactory.getLogger(ExcelUtil.class);
/**
* 导出的核心方法
*
* @param response
* @param request
* @param data
* @param fileName
* @param cla
*/
public static void excelWrite(HttpServletResponse response, HttpServletRequest request, List data, String fileName, Class cla) {
try {
//判断浏览器类型
boolean isMSIE = isMSBrowser(request);
if (isMSIE) {
//IE浏览器的乱码问题解决
fileName = URLEncoder.encode(fileName, "UTF-8");
} else {
//万能乱码问题解决
fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
}
//设置请求头信息
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
// excel头策略
WriteCellStyle headWriteCellStyle = new WriteCellStyle();
WriteFont headWriteFont = new WriteFont();
headWriteFont.setFontHeightInPoints((short) 11);
headWriteFont.setBold(false);
headWriteCellStyle.setWriteFont(headWriteFont);
// excel内容策略
WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
WriteFont contentWriteFont = new WriteFont();
contentWriteFont.setFontHeightInPoints((short) 11);
contentWriteCellStyle.setWriteFont(contentWriteFont);
// 设置handler
HorizontalCellStyleStrategy styleStrategy =
new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
//写出Excel
EasyExcel.write(response.getOutputStream(), cla)
.sheet("数据")
.registerWriteHandler(styleStrategy)
.doWrite(data);
} catch (IOException e) {
logger.error("导出excel失败=========" + e.getMessage());
}
}
//IE浏览器类型
private static String[] IEBrowserSignals = {"MSIE", "Trident", "Edge"};
/**
* 判断是否是IE浏览器的类型
*
* @param request
* @return
*/
public static boolean isMSBrowser(HttpServletRequest request) {
String userAgent = request.getHeader("User-Agent");
for (String signal : IEBrowserSignals) {
if (userAgent.contains(signal))
return true;
}
return false;
}
/**
* @param workbook
* @param sheetNum (sheet的位置,0表示第一个表格中的第一个sheet)
* @param sheetTitle (sheet的名称)
* @param headers (表格的标题)
* @param result (表格的数据)
* @param out (输出流)
* @throws Exception
* @Title: exportExcel
* @Description: 导出Excel的方法
* @author: evan @ 2014-01-09
*/
public static void exportExcel(XSSFWorkbook workbook, int sheetNum,
String sheetTitle, String[] headers, List<List<String>> result,
OutputStream out){
// 生成一个表格
XSSFSheet sheet = workbook.createSheet();
workbook.setSheetName(sheetNum, sheetTitle);
// 设置表格默认列宽度为20个字节
sheet.setDefaultColumnWidth((short) 20);
// 生成一个样式
XSSFCellStyle style = workbook.createCellStyle();
// 设置这些样式
style.setFillForegroundColor(HSSFColor.HSSFColorPredefined.PALE_BLUE.getIndex());
style.setFillPattern( FillPatternType.SOLID_FOREGROUND);
style.setBorderBottom( BorderStyle.THIN);
style.setBorderLeft(BorderStyle.THIN);
style.setBorderRight(BorderStyle.THIN);
style.setBorderTop(BorderStyle.THIN);
style.setAlignment(HorizontalAlignment.CENTER);
// 生成一个字体
XSSFFont font = workbook.createFont();
font.setColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex());
font.setFontHeightInPoints((short) 12);
font.setBold(true);
// 把字体应用到当前的样式
style.setFont(font);
// 指定当单元格内容显示不下时自动换行
style.setWrapText(true);
// 产生表格标题行
XSSFRow row = sheet.createRow(0);
for (int i = 0; i < headers.length; i++) {
XSSFCell cell = row.createCell((short) i);
cell.setCellStyle(style);
HSSFRichTextString text = new HSSFRichTextString(headers[i]);
cell.setCellValue(text.toString());
}
// 遍历集合数据,产生数据行
if (result != null) {
int index = 1;
for (List<String> m : result) {
row = sheet.createRow(index);
int cellIndex = 0;
for (String str : m) {
XSSFCell cell = row.createCell((short) cellIndex);
cell.setCellValue(str);
cellIndex++;
}
index++;
}
}
}
public static void writeExcelToDisk(List<?> dataList, String fileName, Class cla) {
OutputStream outputStream = null;
try {
outputStream = new FileOutputStream(fileName);
} catch (FileNotFoundException e1) {
logger.error("未找到文件!", e1.getMessage());
}
try {
// excel头策略
WriteCellStyle headWriteCellStyle = new WriteCellStyle();
WriteFont headWriteFont = new WriteFont();
headWriteFont.setFontHeightInPoints((short) 11);
headWriteFont.setBold(false);
headWriteCellStyle.setWriteFont(headWriteFont);
// excel内容策略
WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
WriteFont contentWriteFont = new WriteFont();
contentWriteFont.setFontHeightInPoints((short) 11);
contentWriteCellStyle.setWriteFont(contentWriteFont);
// 设置handler
HorizontalCellStyleStrategy styleStrategy =
new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
//写出Excel
EasyExcel.write(outputStream, cla)
.sheet("预警统计数据")
.registerWriteHandler(styleStrategy)
.doWrite(dataList);
} catch (Exception e) {
logger.error("导出excel失败=========" + e.getMessage());
}
}
}
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
public class DateUtil extends DateUtils {
private final static Logger logger = LoggerFactory.getLogger(DateUtil.class);
/**
* 日期格式,年份,例如:2004,2008
*/
public static final String DATE_FORMAT_YYYY = "yyyy";
/**
* 日期格式,年份和月份,例如:200707,200808
*/
public static final String DATE_FORMAT_YYYYMM = "yyyyMM";
/**
* 日期格式,年份和月份,例如:200707,2008-08
*/
public static final String DATE_FORMAT_YYYY_MM = "yyyy-MM";
/**
* 日期格式,年月日,例如:050630,080808
*/
public static final String DATE_FORMAT_YYMMDD = "yyMMdd";
/**
* 日期格式,年月日,用横杠分开,例如:06-12-25,08-08-08
*/
public static final String DATE_FORMAT_YY_MM_DD = "yy-MM-dd";
/**
* 日期格式,年月日,例如:20050630,20080808
*/
public static final String DATE_FORMAT_YYYYMMDD = "yyyyMMdd";
/**
* 日期格式,年月日,用横杠分开,例如:2006-12-25,2008-08-08
*/
public static final String DATE_FORMAT_YYYY_MM_DD = "yyyy-MM-dd";
/**
* 日期格式,年月日,例如:2016.10.05
*/
public static final String DATE_FORMAT_POINTYYYYMMDD = "yyyy.MM.dd";
/**
* 日期格式,年月日,例如:2016年10月05日
*/
public static final String DATE_TIME_FORMAT_YYYY年MM月DD日 = "yyyy年MM月dd日";
/**
* 日期格式,年月日时分,例如:200506301210,200808081210
*/
public static final String DATE_FORMAT_YYYYMMDDHHmm = "yyyyMMddHHmm";
/**
* 日期格式,年月日时分,例如:20001230 12:00,20080808 20:08
*/
public static final String DATE_TIME_FORMAT_YYYYMMDD_HH_MI = "yyyyMMdd HH:mm";
/**
* 日期格式,年月日时分,例如:2000-12-30 12:00,2008-08-08 20:08
*/
public static final String DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI = "yyyy-MM-dd HH:mm";
/**
* 日期格式,年月日时分秒,例如:20001230120000,20080808200808
*/
public static final String DATE_TIME_FORMAT_YYYYMMDDHHMISS = "yyyyMMddHHmmss";
/**
* 日期格式,年月日时分秒,年月日用横杠分开,时分秒用冒号分开
* 例如:2005-05-10 23:20:00,2008-08-08 20:08:08
*/
public static final String DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS = "yyyy-MM-dd HH:mm:ss";
public static final String DATE_TIME_FORMAT_HH_MI_SS = "HH:mm:ss";
/**
* 日期格式,年月日时分秒毫秒,例如:20001230120000123,20080808200808456
*/
public static final String DATE_TIME_FORMAT_YYYYMMDDHHMISSSSS = "yyyyMMddHHmmssSSS";
/**
* 日期格式,月日时分,例如:10-05 12:00
*/
public static final String DATE_FORMAT_MMDDHHMI = "MM-dd HH:mm";
/**
* ISO8601标准时间格式
*/
public static final String ISO8601_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";
public static final String ISO8601 = "yyyy-MM-dd'T'HH:mm:ssXXX";
/**
* 获取某日期的年份
*
* @param date
* @return
*/
public static Integer getYear(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(Calendar.YEAR);
}
/**
* 获取某日期的月份
*
* @param date
* @return
*/
public static Integer getMonth(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(Calendar.MONTH) + 1;
}
/**
* 获取某日期的日数
*
* @param date
* @return
*/
public static Integer getDay(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int day = cal.get(Calendar.DATE);//获取日
return day;
}
/**
* 格式化Date时间
*
* @param time Date类型时间
* @param timeFromat String类型格式
* @return 格式化后的字符串
*/
public static String parseDateToStr(Date time, String timeFromat) {
DateFormat dateFormat = new SimpleDateFormat(timeFromat);
return dateFormat.format(time);
}
/**
* 格式化Timestamp时间
*
* @param timestamp Timestamp类型时间
* @param timeFromat
* @return 格式化后的字符串
*/
public static String parseTimestampToStr(Timestamp timestamp, String timeFromat) {
SimpleDateFormat df = new SimpleDateFormat(timeFromat);
return df.format(timestamp);
}
/**
* 格式化Date时间
*
* @param time Date类型时间
* @param timeFromat String类型格式
* @param defaultValue 默认值为当前时间Date
* @return 格式化后的字符串
*/
public static String parseDateToStr(Date time, String timeFromat, final Date defaultValue) {
try {
DateFormat dateFormat = new SimpleDateFormat(timeFromat);
return dateFormat.format(time);
} catch (Exception e) {
if (defaultValue != null)
return parseDateToStr(defaultValue, timeFromat);
else
return parseDateToStr(new Date(), timeFromat);
}
}
/**
* 格式化Date时间
*
* @param time Date类型时间
* @param timeFromat String类型格式
* @param defaultValue 默认时间值String类型
* @return 格式化后的字符串
*/
public static String parseDateToStr(Date time, String timeFromat, final String defaultValue) {
try {
DateFormat dateFormat = new SimpleDateFormat(timeFromat);
return dateFormat.format(time);
} catch (Exception e) {
return defaultValue;
}
}
/**
* 格式化String时间
*
* @param time String类型时间
* @param timeFromat String类型格式
* @return 格式化后的Date日期
*/
public static Date parseStrToDate(String time, String timeFromat) {
if (time == null || time.equals("")) {
return null;
}
Date date = null;
try {
DateFormat dateFormat = new SimpleDateFormat(timeFromat);
date = dateFormat.parse(time);
} catch (Exception e) {
}
return date;
}
/**
* 格式化String时间
*
* @param strTime String类型时间
* @param timeFromat String类型格式
* @param defaultValue 异常时返回的默认值
* @return
*/
public static Date parseStrToDate(String strTime, String timeFromat,
Date defaultValue) {
try {
DateFormat dateFormat = new SimpleDateFormat(timeFromat);
return dateFormat.parse(strTime);
} catch (Exception e) {
return defaultValue;
}
}
/**
* 当strTime为2008-9时返回为2008-9-1 00:00格式日期时间,无法转换返回null.
*
* @param strTime
* @return
*/
public static Date strToDate(String strTime) {
if (strTime == null || strTime.trim().length() <= 0)
return null;
Date date = null;
List<String> list = new ArrayList<String>(0);
list.add(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
list.add(DATE_TIME_FORMAT_YYYYMMDDHHMISSSSS);
list.add(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI);
list.add(DATE_TIME_FORMAT_YYYYMMDD_HH_MI);
list.add(DATE_TIME_FORMAT_YYYYMMDDHHMISS);
list.add(DATE_FORMAT_YYYY_MM_DD);
list.add(DATE_FORMAT_YYYYMMDD);
list.add(DATE_FORMAT_YYYY_MM);
list.add(DATE_FORMAT_YYYYMM);
list.add(DATE_FORMAT_YYYY);
for (Iterator iter = list.iterator(); iter.hasNext(); ) {
String format = (String) iter.next();
if (strTime.indexOf("-") > 0 && format.indexOf("-") < 0)
continue;
if (strTime.indexOf("-") < 0 && format.indexOf("-") > 0)
continue;
if (strTime.length() > format.length())
continue;
date = parseStrToDate(strTime, format);
if (date != null)
break;
}
return date;
}
/**
* 解析两个日期之间的所有月份
*
* @param beginDateStr 开始日期,至少精确到yyyy-MM
* @param endDateStr 结束日期,至少精确到yyyy-MM
* @return yyyy-MM日期集合
*/
public static List<String> getMonthListOfDate(String beginDateStr, String endDateStr) {
// 指定要解析的时间格式
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM");
// 返回的月份列表
String sRet = "";
// 定义一些变量
Date beginDate = null;
Date endDate = null;
GregorianCalendar beginGC = null;
GregorianCalendar endGC = null;
List<String> list = new ArrayList<String>();
try {
// 将字符串parse成日期
beginDate = f.parse(beginDateStr);
endDate = f.parse(endDateStr);
// 设置日历
beginGC = new GregorianCalendar();
beginGC.setTime(beginDate);
endGC = new GregorianCalendar();
endGC.setTime(endDate);
// 直到两个时间相同
while (beginGC.getTime().compareTo(endGC.getTime()) <= 0) {
sRet = beginGC.get(Calendar.YEAR) + "-"
+ (beginGC.get(Calendar.MONTH) + 1);
list.add(sRet);
// 以月为单位,增加时间
beginGC.add(Calendar.MONTH, 1);
}
return list;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 解析两个日期段之间的所有日期
*
* @param beginDateStr 开始日期 ,至少精确到yyyy-MM-dd
* @param endDateStr 结束日期 ,至少精确到yyyy-MM-dd
* @return yyyy-MM-dd日期集合
*/
public static List<String> getDayListOfDate(String beginDateStr, String endDateStr) {
// 指定要解析的时间格式
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
// 定义一些变量
Date beginDate = null;
Date endDate = null;
Calendar beginGC = null;
Calendar endGC = null;
List<String> list = new ArrayList<String>();
try {
// 将字符串parse成日期
beginDate = f.parse(beginDateStr);
endDate = f.parse(endDateStr);
// 设置日历
beginGC = Calendar.getInstance();
beginGC.setTime(beginDate);
endGC = Calendar.getInstance();
endGC.setTime(endDate);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
// 直到两个时间相同
while (beginGC.getTime().compareTo(endGC.getTime()) <= 0) {
list.add(sdf.format(beginGC.getTime()));
// 以日为单位,增加时间
beginGC.add(Calendar.DAY_OF_MONTH, 1);
}
return list;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 获取当下年份指定前后数量的年份集合
*
* @param before 当下年份前年数
* @param behind 当下年份后年数
* @return 集合
*/
public static List<Integer> getYearListOfYears(int before, int behind) {
if (before < 0 || behind < 0) {
return null;
}
List<Integer> list = new ArrayList<Integer>();
Calendar c = null;
c = Calendar.getInstance();
c.setTime(new Date());
int currYear = Calendar.getInstance().get(Calendar.YEAR);
int startYear = currYear - before;
int endYear = currYear + behind;
for (int i = startYear; i < endYear; i++) {
list.add(Integer.valueOf(i));
}
return list;
}
/**
* 获取当前日期是一年中第几周
*
* @param date
* @return
*/
public static Integer getWeekthOfYear(Date date) {
Calendar c = new GregorianCalendar();
c.setFirstDayOfWeek(Calendar.MONDAY);
c.setMinimalDaysInFirstWeek(7);
c.setTime(date);
return c.get(Calendar.WEEK_OF_YEAR);
}
/**
* 获取某一年各星期的始终时间
* 实例:getWeekList(2016),第52周(从2016-12-26至2017-01-01)
*
* @param year
* @return
*/
public static HashMap<Integer, String> getWeekTimeOfYear(int year) {
HashMap<Integer, String> map = new LinkedHashMap<Integer, String>();
Calendar c = new GregorianCalendar();
c.set(year, Calendar.DECEMBER, 31, 23, 59, 59);
int count = getWeekthOfYear(c.getTime());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String dayOfWeekStart = "";
String dayOfWeekEnd = "";
for (int i = 1; i <= count; i++) {
dayOfWeekStart = sdf.format(getFirstDayOfWeek(year, i));
dayOfWeekEnd = sdf.format(getLastDayOfWeek(year, i));
map.put(Integer.valueOf(i), "第" + i + "周(从" + dayOfWeekStart + "至" + dayOfWeekEnd + ")");
}
return map;
}
/**
* 获取某一年的总周数
*
* @param year
* @return
*/
public static Integer getWeekCountOfYear(int year) {
Calendar c = new GregorianCalendar();
c.set(year, Calendar.DECEMBER, 31, 23, 59, 59);
int count = getWeekthOfYear(c.getTime());
return count;
}
/**
* 获取指定日期所在周的第一天
*
* @param date
* @return
*/
public static Date getFirstDayOfWeek(Date date) {
Calendar c = new GregorianCalendar();
c.setFirstDayOfWeek(Calendar.MONDAY);
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); // Monday
return c.getTime();
}
/**
* 获取指定日期所在周的最后一天
*
* @param date
* @return
*/
public static Date getLastDayOfWeek(Date date) {
Calendar c = new GregorianCalendar();
c.setFirstDayOfWeek(Calendar.MONDAY);
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6); // Sunday
return c.getTime();
}
/**
* 获取某年某周的第一天
*
* @param year 目标年份
* @param week 目标周数
* @return
*/
public static Date getFirstDayOfWeek(int year, int week) {
Calendar c = new GregorianCalendar();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, Calendar.JANUARY);
c.set(Calendar.DATE, 1);
Calendar cal = (GregorianCalendar) c.clone();
cal.add(Calendar.DATE, week * 7);
return getFirstDayOfWeek(cal.getTime());
}
/**
* 获取某年某周的最后一天
*
* @param year 目标年份
* @param week 目标周数
* @return
*/
public static Date getLastDayOfWeek(int year, int week) {
Calendar c = new GregorianCalendar();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, Calendar.JANUARY);
c.set(Calendar.DATE, 1);
Calendar cal = (GregorianCalendar) c.clone();
cal.add(Calendar.DATE, week * 7);
return getLastDayOfWeek(cal.getTime());
}
/**
* 获取某年某月的第一天
*
* @param year 目标年份
* @param month 目标月份
* @return
*/
public static Date getFirstDayOfMonth(int year, int month) {
month = month - 1;
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, month);
int day = c.getActualMinimum(c.DAY_OF_MONTH);
c.set(Calendar.DAY_OF_MONTH, day);
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
return c.getTime();
}
/**
* 获取某年某月的最后一天
*
* @param year 目标年份
* @param month 目标月份
* @return
*/
public static Date getLastDayOfMonth(int year, int month) {
month = month - 1;
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, month);
int day = c.getActualMaximum(c.DAY_OF_MONTH);
c.set(Calendar.DAY_OF_MONTH, day);
c.set(Calendar.HOUR_OF_DAY, 23);
c.set(Calendar.MINUTE, 59);
c.set(Calendar.SECOND, 59);
c.set(Calendar.MILLISECOND, 999);
return c.getTime();
}
/**
* 获取某个日期为星期几
*
* @param date
* @return String "星期*"
*/
public static String getDayWeekOfDate1(Date date) {
String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0)
w = 0;
return weekDays[w];
}
/**
* 获取某个日期为周几
*
* @param dateStr
* @return String "星期*"
*/
public static String getDayWeekOfDate1(String dateStr) {
String[] weekDays = {"星期日", "周一", "周二", "周三", "周四", "周五", "周六"};
DateFormat df = new SimpleDateFormat(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
Date date = null;
try {
date = df.parse(dateStr);
} catch (ParseException e) {
logger.error("时间解析发生异常!", e);
}
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0)
w = 0;
return weekDays[w];
}
/**
* 获得指定日期的星期几数
*
* @param date
* @return int
*/
public static Integer getDayWeekOfDate2(Date date) {
Calendar aCalendar = Calendar.getInstance();
aCalendar.setTime(date);
int weekDay = aCalendar.get(Calendar.DAY_OF_WEEK);
return weekDay;
}
/**
* 验证字符串是否为日期
* 验证格式:YYYYMMDD、YYYY_MM_DD、YYYYMMDDHHMISS、YYYYMMDD_HH_MI、YYYY_MM_DD_HH_MI、YYYYMMDDHHMISSSSS、YYYY_MM_DD_HH_MI_SS
*
* @param strTime
* @return null时返回false;true为日期,false不为日期
*/
public static boolean validateIsDate(String strTime) {
if (strTime == null || strTime.trim().length() <= 0)
return false;
Date date = null;
List<String> list = new ArrayList<String>(0);
list.add(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
list.add(DATE_TIME_FORMAT_YYYYMMDDHHMISSSSS);
list.add(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI);
list.add(DATE_TIME_FORMAT_YYYYMMDD_HH_MI);
list.add(DATE_TIME_FORMAT_YYYYMMDDHHMISS);
list.add(DATE_FORMAT_YYYY_MM_DD);
for (Iterator iter = list.iterator(); iter.hasNext(); ) {
String format = (String) iter.next();
if (strTime.indexOf("-") > 0 && format.indexOf("-") < 0)
continue;
if (strTime.indexOf("-") < 0 && format.indexOf("-") > 0)
continue;
if (strTime.length() > format.length())
continue;
date = parseStrToDate(strTime.trim(), format);
if (date != null)
break;
}
if (date != null) {
System.out.println("生成的日期:" + DateUtil.parseDateToStr(date, DateUtil.DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS, "--null--"));
return true;
}
return false;
}
/**
* 将指定日期的时分秒格式为零
*
* @param date
* @return
*/
public static Date formatHhMmSsOfDate(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
}
/**
* 获得指定时间加减参数后的日期(不计算则输入0)
*
* @param date 指定日期
* @param year 年数,可正可负
* @param month 月数,可正可负
* @param day 天数,可正可负
* @param hour 小时数,可正可负
* @param minute 分钟数,可正可负
* @param second 秒数,可正可负
* @param millisecond 毫秒数,可正可负
* @return 计算后的日期
*/
public static Date addDate(Date date, int year, int month, int day, int hour, int minute, int second, int millisecond) {
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.YEAR, year);//加减年数
c.add(Calendar.MONTH, month);//加减月数
c.add(Calendar.DATE, day);//加减天数
c.add(Calendar.HOUR, hour);//加减小时数
c.add(Calendar.MINUTE, minute);//加减分钟数
c.add(Calendar.SECOND, second);//加减秒
c.add(Calendar.MILLISECOND, millisecond);//加减毫秒数
return c.getTime();
}
/**
* 获取指定时间段内,特定星期几的日期字符串列表
*
* @param startDateStr 开始日期 格式为:2012-12-12 00:00:00
* @param endDateStr 结束日期 格式为:2012-12-12 23:00:00
* @param week 要选择的星期 为本周的第几天,以星期日为 本周的第“1”天
* @return 日期字符串列表
*/
public static List getNeedDate(String startDateStr, String endDateStr, String[] week) {
List list = new ArrayList();
SimpleDateFormat dfDate = new SimpleDateFormat("yyyyMMdd", Locale.CHINESE);
SimpleDateFormat dfDateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINESE);
int weekDay = 0;
try {
weekDay = getDayWeekOfDate2(dfDateTime.parse(endDateStr));
} catch (ParseException e) {
logger.error("时间解析异常!", e);
}
Calendar beginDate = Calendar.getInstance();
Calendar endDate = Calendar.getInstance();
try {
beginDate.setTime(dfDateTime.parse(startDateStr));
endDate.setTime(dfDateTime.parse(endDateStr));
} catch (ParseException e) {
logger.error("时间解析异常!", e);
}
//获取指定日历的副本
Calendar date = (Calendar) beginDate.clone();
while (date.before(endDate)) {
//获取日期所在周的第几天
int dayOfWeek = date.get(Calendar.DAY_OF_WEEK);
for (int i = 0; i < week.length; i++) {
if (dayOfWeek == Integer.parseInt(week[i])) {
String str = dfDate.format(date.getTime());
list.add(str);
}
}
//日期的后一天
date.add(Calendar.DAY_OF_MONTH, 1);
}
if (5 == weekDay) {
try {
list.add(dfDate.format(dfDateTime.parse(endDateStr)));
} catch (ParseException e) {
logger.error("时间解析异常!", e);
}
}
return list;
}
//获取下上一个周四
public static String getBeforeThursday(String dateStr) {
int weekDay = 0;
String resultDate = "";
SimpleDateFormat dfDateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINESE);
try {
weekDay = getDayWeekOfDate2(dfDateTime.parse(dateStr));
int diff = 0;
if (weekDay < 5) {
diff = 7 + weekDay - 5;
} else {
diff = weekDay - 5;
}
resultDate = getDateBeforeNDate(dfDateTime.parse(dateStr), diff);
} catch (ParseException e) {
e.printStackTrace();
}
return resultDate;
}
//获取下一个周四
public static String getAfterThursday(String dateStr) {
int weekDay = 0;
String resultDate = "";
SimpleDateFormat dfDateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINESE);
try {
weekDay = getDayWeekOfDate2(dfDateTime.parse(dateStr));
int diff = 0;
if (weekDay < 5) {
diff = 5 - weekDay;
} else {
diff = (12 - weekDay) % 7;
}
resultDate = getDateBeforeNDate(dfDateTime.parse(dateStr), 0 - diff);
} catch (ParseException e) {
e.printStackTrace();
}
return resultDate;
}
/**
* 获得两个日期的时间戳之差
*
* @param startDate
* @param endDate
* @return
*/
public static Long getDistanceTimestamp(Date startDate, Date endDate) {
long daysBetween = (endDate.getTime() - startDate.getTime() + 1000000) / (3600 * 24 * 1000);
return daysBetween;
}
/**
* 获得两个日期的时间戳之差
*
* @param startDateStr
* @param endDateStr
* @return
*/
public static Long getDistanceTimestamp(String startDateStr, String endDateStr) {
DateFormat df = new SimpleDateFormat(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
Date startDate = null;
Date endDate = null;
try {
startDate = df.parse(startDateStr);
endDate = df.parse(endDateStr);
} catch (ParseException e) {
logger.error("时间解析发生异常!");
}
long secondsBetween = (endDate.getTime() - startDate.getTime() + 1000000) / (1000);
return secondsBetween;
}
/**
* 判断二个时间是否为同年同月
*
* @param date1
* @param date2
* @return
*/
public static Boolean compareIsSameMonth(Date date1, Date date2) {
boolean flag = false;
int year1 = getYear(date1);
int year2 = getYear(date2);
if (year1 == year2) {
int month1 = getMonth(date1);
int month2 = getMonth(date2);
if (month1 == month2) flag = true;
}
return flag;
}
/**
* 获得两个时间相差距离多少天多少小时多少分多少秒
*
* @param str1 时间参数 1 格式:1990-01-01 12:00:00
* @param str2 时间参数 2 格式:2009-01-01 12:00:00
* @return long[] 返回值为:{天, 时, 分, 秒}
*/
public static long[] getDistanceTime(Date str1, Date str2) {
long day = 0;
long hour = 0;
long min = 0;
long sec = 0;
try {
long time1 = str1.getTime();
long time2 = str2.getTime();
long diff;
if (time1 < time2) {
diff = time2 - time1;
} else {
diff = time1 - time2;
}
day = diff / (24 * 60 * 60 * 1000);
hour = (diff / (60 * 60 * 1000) - day * 24);
min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
sec = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
} catch (Exception e) {
e.printStackTrace();
}
long[] times = {day, hour, min, sec};
return times;
}
/**
* 两个时间相差距离多少天多少小时多少分多少秒
*
* @param str1 时间参数 1 格式:1990-01-01 12:00:00
* @param str2 时间参数 2 格式:2009-01-01 12:00:00
* @return String 返回值为:{天, 时, 分, 秒}
*/
public static long[] getDistanceTime(String str1, String str2) {
DateFormat df = new SimpleDateFormat(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
Date one;
Date two;
long day = 0;
long hour = 0;
long min = 0;
long sec = 0;
try {
one = df.parse(str1);
two = df.parse(str2);
long time1 = one.getTime();
long time2 = two.getTime();
long diff;
if (time1 < time2) {
diff = time2 - time1;
} else {
diff = time1 - time2;
}
day = diff / (24 * 60 * 60 * 1000);
hour = (diff / (60 * 60 * 1000) - day * 24);
min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
sec = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
} catch (ParseException e) {
e.printStackTrace();
}
long[] times = {day, hour, min, sec};
return times;
}
/**
* 两个时间之间相差距离多少天
*
* @param str1 时间参数 1:
* @param str2 时间参数 2:
* @return 相差天数
*/
public static Long getDistanceDays(String str1, String str2) throws Exception {
DateFormat df = new SimpleDateFormat(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
Date one;
Date two;
long days = 0;
try {
one = df.parse(str1);
two = df.parse(str2);
long time1 = one.getTime();
long time2 = two.getTime();
long diff;
if (time1 < time2) {
diff = time2 - time1;
} else {
diff = time1 - time2;
}
days = diff / (1000 * 60 * 60 * 24);
} catch (ParseException e) {
e.printStackTrace();
}
return days;
}
/**
* 获取指定时间的那天 00:00:00.000 的时间
*
* @param date
* @return
*/
public static Date getDayBeginTime(final Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
return c.getTime();
}
/**
* 获取指定时间的那天 23:59:59.999 的时间
*
* @param date
* @return
*/
public static Date getDayEndTime(final Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
c.set(Calendar.HOUR_OF_DAY, 23);
c.set(Calendar.MINUTE, 59);
c.set(Calendar.SECOND, 59);
c.set(Calendar.MILLISECOND, 999);
return c.getTime();
}
/**
* 获取ISO8601时间格式的时间字符串
*
* @param dateTime
* @return
*/
public static String formatISO8601(Date dateTime) {
DateFormat sdf = new SimpleDateFormat(ISO8601_DATE_FORMAT);
return sdf.format(dateTime);
}
/**
* 获取ISO8601时间格式的时间
*
* @param dateTime
* @return
*/
public static Date fromISO8601(String dateTime) {
try {
return new SimpleDateFormat(ISO8601_DATE_FORMAT).parse(dateTime);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
/**
* 获取yyyy-MM-dd HH:mm:ss格式的时间字符串
*
* @param dateTime
* @return
*/
public static String formatDateTime(Date dateTime) {
DateFormat sdf = new SimpleDateFormat(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
return sdf.format(dateTime);
}
/**
* 获取yyyy-MM-dd HH:mm:ss时间格式的时间
*
* @param dateTime
* @return
*/
public static Date fromDateTime(String dateTime) {
try {
return new SimpleDateFormat(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS).parse(dateTime);
} catch (ParseException e) {
logger.error("时间格式转化异常!", e.toString());
e.printStackTrace();
}
return null;
}
/**
* 将yyyyMMddHHmmss形式的时间转化为yyyy-MM-dd'T'HH:mm:ss.SSSXXX格式的时间
*
* @param dateTime
* @return
*/
public static String formatISO8601FromYYMMDDHHmmss(String dateTime) {
try {
DateFormat newSdf = new SimpleDateFormat(ISO8601_DATE_FORMAT);
DateFormat oldSdf = new SimpleDateFormat(DATE_TIME_FORMAT_YYYYMMDDHHMISS);
Date oldDate = oldSdf.parse(dateTime);
return newSdf.format(oldDate);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
/**
* 将yyyy-MM-dd HH:mm:ss形式的时间转化为yyyy-MM-dd'T'HH:mm:ss.SSSXXX格式的时间
*
* @param dateTime
* @return
*/
public static String formatISO8601FromStandard(String dateTime) {
try {
DateFormat newSdf = new SimpleDateFormat(ISO8601_DATE_FORMAT);
DateFormat oldSdf = new SimpleDateFormat(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
Date oldDate = oldSdf.parse(dateTime);
return newSdf.format(oldDate);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
/**
* 将yyyy-MM-dd'T'HH:mm:ss.SSSXXX格式的时间转化为yyyy-MM-dd HH:mm:ss形式的时间
*
* @param dateTime
* @return
*/
public static String formatStandardFromISO8601(String dateTime) {
try {
DateFormat newSdf = new SimpleDateFormat(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
DateFormat oldSdf = new SimpleDateFormat(ISO8601_DATE_FORMAT);
Date oldDate = oldSdf.parse(dateTime);
return newSdf.format(oldDate);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
public static String formatStandardTimeFromISO8601(String timeStr) {
try {
SimpleDateFormat sdf1 = new SimpleDateFormat(ISO8601);
SimpleDateFormat sdf2 = new SimpleDateFormat(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
Date date = sdf1.parse(timeStr);
return sdf2.format(date);
} catch (Exception e) {
e.printStackTrace();
}
return timeStr;
}
public static String formatYMDDate(Date dateTime) {
DateFormat sdf = new SimpleDateFormat(DATE_FORMAT_YYYY_MM_DD);
return sdf.format(dateTime);
}
public static String formatHHHISS(Date dateTime) {
DateFormat sdf = new SimpleDateFormat(DATE_TIME_FORMAT_HH_MI_SS);
return sdf.format(dateTime);
}
public static String formatYMDHMSDate(Date dateTime) {
DateFormat sdf = new SimpleDateFormat(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
return sdf.format(dateTime);
}
//将时间格式转换成20191212格式
public static String formatYYYYMMDDDate(Date dateTime) {
DateFormat sdf = new SimpleDateFormat("yyyyMMdd");
return sdf.format(dateTime);
}
public static String getBefore1MinuteTime() {
Date curentDate = new Date();
Date before1MinuteDate = DateUtil.addDate(curentDate, 0, 0, 0, 0, -1, 0, 0);
String endDateTime = formatYMDHMSDate(before1MinuteDate);
DateFormat sdf = new SimpleDateFormat(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
Date endDateTimeISOISO8601 = null;
try {
endDateTimeISOISO8601 = sdf.parse(endDateTime);
} catch (ParseException e) {
logger.error("时间转化发生异常!");
}
String oneMinuteAgo = formatISO8601(endDateTimeISOISO8601);
return oneMinuteAgo;
}
public static String getBefore60seTime() {
Date curentDate = new Date();
Date before1MinuteDate = DateUtil.addDate(curentDate, 0, 0, 0, 0, 0, -60, 0);
String endDateTime = formatYMDHMSDate(before1MinuteDate);
DateFormat sdf = new SimpleDateFormat(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
Date endDateTimeISOISO8601 = null;
try {
endDateTimeISOISO8601 = sdf.parse(endDateTime);
} catch (ParseException e) {
logger.error("时间转化发生异常!");
}
String oneMinuteAgo = formatISO8601(endDateTimeISOISO8601);
return oneMinuteAgo;
}
public static String getBefore7day() {
Date curentDate = new Date();
Date before1MinuteDate = DateUtil.addDate(curentDate, 0, 0, -7, 0, 0, 0, 0);
String endDateTime = formatYMDHMSDate(before1MinuteDate);
DateFormat sdf = new SimpleDateFormat(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
Date endDateTimeISOISO8601 = null;
try {
endDateTimeISOISO8601 = sdf.parse(endDateTime);
} catch (ParseException e) {
logger.error("时间转化发生异常!");
}
String oneMinuteAgo = formatISO8601(endDateTimeISOISO8601);
return oneMinuteAgo;
}
public static List<String> getCurrentAndBefore7DaysBeginAndEndDateTime() {
List<String> list = new ArrayList<>();
Date curentDate = new Date();
Date before7DaysDate = DateUtil.addDate(curentDate, 0, 0, -6, 0, 0, 0, 0);
String endDateTime = formatYMDDate(curentDate) + " 00:00:00";
String beginDateTime = formatYMDDate(before7DaysDate) + " 23:59:59";
DateFormat sdf = new SimpleDateFormat(DATE_FORMAT_YYYY_MM_DD);
Date beginDateTimeISO8601 = null;
Date endDateTimeISOISO8601 = null;
try {
beginDateTimeISO8601 = sdf.parse(beginDateTime);
endDateTimeISOISO8601 = sdf.parse(endDateTime);
} catch (ParseException e) {
logger.error("时间转化发生异常!");
}
list.add(formatISO8601(beginDateTimeISO8601));
list.add(formatISO8601(endDateTimeISOISO8601));
return list;
}
public static List<String> getCurrentAndBefore30DaysBeginAndEndDateTime() {
List<String> list = new ArrayList<>();
Date curentDate = new Date();
Date before30DaysDate = DateUtil.addDate(curentDate, 0, 0, -29, 0, 0, 0, 0);
String endDateTime = formatYMDDate(curentDate) + " 00:00:00";
String beginDateTime = formatYMDDate(before30DaysDate) + " 23:59:59";
DateFormat sdf = new SimpleDateFormat(DATE_FORMAT_YYYY_MM_DD);
Date beginDateTimeISO8601 = null;
Date endDateTimeISOISO8601 = null;
try {
beginDateTimeISO8601 = sdf.parse(beginDateTime);
endDateTimeISOISO8601 = sdf.parse(endDateTime);
} catch (ParseException e) {
logger.error("时间转化发生异常!");
}
list.add(formatISO8601(beginDateTimeISO8601));
list.add(formatISO8601(endDateTimeISOISO8601));
return list;
}
/**
* 将2019-09-08T17:25:08.000+08:00格式的时间转化为DATE_FORMAT_YYYYMMDDHHmm = "yyyyMMddHHmm"格式的时间
*
* @param dateTime
* @return
*/
public static String getFormatYYMMDDhhmmss(String dateTime) {
try {
DateFormat oldSdf = new SimpleDateFormat(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
DateFormat newSdf = new SimpleDateFormat(DATE_TIME_FORMAT_YYYYMMDDHHMISS);
String dateTimeStr = dateTime.substring(0, 10) + " " + dateTime.substring(11, 19);
Date newDateTime = oldSdf.parse(dateTimeStr);
return newSdf.format(newDateTime);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
/**
* 获取当前日期时间
*
* @return
*/
public static String getCurrentDateTime() {
DateFormat sdf = new SimpleDateFormat(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
Date date = new Date();
String dateTime = sdf.format(date);
return dateTime;
}
/**
* 获取当前日期时间
*
* @return
*/
public static String getCurrentDateTimeWithYyyyMMdd() {
DateFormat sdf = new SimpleDateFormat(DATE_FORMAT_YYYYMMDD);
Date date = new Date();
String dateTime = sdf.format(date);
return dateTime;
}
/**
* 获取当前日期
*
* @return
*/
public static String getCurrentDate() {
DateFormat sdf = new SimpleDateFormat(DATE_FORMAT_YYYY_MM_DD);
Date date = new Date();
String resultDate = sdf.format(date);
return resultDate;
}
/**
* 获取当前日期时间,yyyyMMddHHmmss格式
*
* @return
*/
public static String getCurrentDateTimeWithYyyyMMddHHmmss() {
DateFormat sdf = new SimpleDateFormat(DATE_TIME_FORMAT_YYYYMMDDHHMISS);
Date date = new Date();
String dateTime = sdf.format(date);
return dateTime;
}
/**
* 获取当天开始日期时间
*
* @return
*/
public static String getCurrentBeginDateTime() {
DateFormat sdf = new SimpleDateFormat(DATE_FORMAT_YYYY_MM_DD);
Date date = new Date();
String currentDate = sdf.format(date);
String todatBeginDateTime = currentDate + " 00:00:00";
return todatBeginDateTime;
}
/**
* 获取当天向前推n天的日期时间
*
* @param n
* @return
*/
public static String getCurrentBeforeNDateTime(int n) {
DateFormat sdf = new SimpleDateFormat(DATE_FORMAT_YYYY_MM_DD);
Date curentDate = new Date();
Date beforeNDaysDateTime = DateUtil.addDate(curentDate, 0, 0, 0 - n, 0, 0, 0, 0);
String currentDate = sdf.format(beforeNDaysDateTime);
String todatBeforeNDaysDateTime = currentDate + " 00:00:00";
return todatBeforeNDaysDateTime;
}
/**
* 获取当天向前推n天的日期
*
* @param n
* @return
*/
public static String getCurrentBeforeNDate(int n) {
DateFormat sdf = new SimpleDateFormat(DATE_FORMAT_YYYY_MM_DD);
Date curentDate = new Date();
Date beforeNDaysDateTime = DateUtil.addDate(curentDate, 0, 0, 0 - n, 0, 0, 0, 0);
String currentDate = sdf.format(beforeNDaysDateTime);
return currentDate;
}
/**
* 获取给定日期向前推n天的日期
*
* @param n
* @return
*/
public static String getDateBeforeNDate(Date date, int n) {
DateFormat sdf = new SimpleDateFormat(DATE_FORMAT_YYYYMMDD);
Date beforeNDaysDateTime = DateUtil.addDate(date, 0, 0, 0 - n, 0, 0, 0, 0);
String resultDate = sdf.format(beforeNDaysDateTime);
return resultDate;
}
/**
* 获取指定时间的那天 00:00:00 的时间,返回格式yyyy-MM-dd HH:mm:ss
*
* @param date
* @return
*/
public static String getDayBeginTimeS(final Date date) {
SimpleDateFormat df = new SimpleDateFormat(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
Calendar c = Calendar.getInstance();
c.setTime(date);
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
return df.format(c.getTime());
}
/**
* 获取指定时间的那天 23:59:59的时间,返回格式yyyy-MM-dd HH:mm:ss
*
* @param date
* @return
*/
public static String getDayEndTimeS(final Date date) {
SimpleDateFormat df = new SimpleDateFormat(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
Calendar c = Calendar.getInstance();
c.setTime(date);
c.set(Calendar.HOUR_OF_DAY, 23);
c.set(Calendar.MINUTE, 59);
c.set(Calendar.SECOND, 59);
c.set(Calendar.MILLISECOND, 999);
return df.format(c.getTime());
}
/**
* 将20191203143836的String时间转化为以毫秒为单位的时间戳(String)
*
* @param dateTime
* @return
*/
public static String fromTimeToTimestampMs(String dateTime) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
try {
Date date = simpleDateFormat.parse(dateTime);
long ts = date.getTime();
String res = String.valueOf(ts);
return res;
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
/**
* 将以毫秒为单位的时间戳(String)转化为2019-12-03 14:38:36的String时间
*
* @param dateTime
* @return
*/
public static String fromTimestampMsToTime(String dateTime) {
String res;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
long lt = new Long(dateTime);
Date date = new Date(lt);
res = simpleDateFormat.format(date);
return res;
}
/**
* 将String转换为Date
*
* @param dateTime
* @return
*/
public static Date stringToDate(String dateTime) {
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date date = fmt.parse(dateTime);
return date;
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
/**
* 将String转换为String
*
* @param dateTime
* @param fromFormat
* @param Toformat
* @return
*/
public static String stringToString(String dateTime, String fromFormat, String Toformat) {
SimpleDateFormat fmt = new SimpleDateFormat(fromFormat);
try {
Date date = fmt.parse(dateTime);
return parseDateToStr(date, Toformat);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
public static java.sql.Date getSqlYesterday() {
return addSqlDates((Date) (new Date()), -1);
}
public static java.sql.Date addSqlDates(Date date, int num) {
return new java.sql.Date(addDays(date, num).getTime());
}
public static Integer formatDate(Date date) {
return Integer.parseInt(formatDate(date, "yyyyMMdd"));
}
}