栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

记第一次使用ES查询

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

记第一次使用ES查询

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 依赖

    org.elasticsearch.client
    transport
    ${elasticsearch.version}
5.0之前没有提供rest client jar包,RestClient已经包含在elasticsearch 的jar包里,如下图:

 

Rest Client(已引入transport不能够再引入 rest,包会发生冲突)

     org.elasticsearch
     elasticsearch
     6.5.4



    org.elasticsearch.client
    elasticsearch-rest-client
    6.5.4

下面是TransportClient 的使用示例

服务器ES用的是2.1.1版本
        
            org.elasticsearch
            elasticsearch
            2.1.1
        
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;



@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;



@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 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 mapList = new ArrayList<>();
            List vehiclePassesList = new ArrayList<>();

            total = response.getHits().totalHits();

            for (SearchHit searchHit : response.getHits()) {
                Map 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;
    }


    

    public List getAllVehiclePass(String index, String fieldSta, String fieldRange, String startTime, String endTime, String crossingIds) {

        Integer pageSize = PAGE_SIZE;
        List vehiclePasses = new ArrayList<>();
        PageInfo pageInfo = new PageInfo();
        pageInfo = staVehiclePass(index, fieldSta, fieldRange, 1, pageSize, startTime, endTime, crossingIds);
        List vehiclePasseList = (List) 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 tempVehiclePasseList = (List) tempPageInfo.getList();
                vehiclePasses.addAll(tempVehiclePasseList);

            }
        }
        return vehiclePasses;
    }

    public List> vehiclePassToListString(List vehiclePasses) {
        List> dataList = new ArrayList>();
        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 vehiclePasses = getAllVehiclePass(index, fieldSta, fieldRange, startTime, endTime, crossingIds);
        List> 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> 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 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;



@Component
public class ExcelUtil {

    private static Logger logger = LoggerFactory.getLogger(ExcelUtil.class);

    
    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"};

    
    public static boolean isMSBrowser(HttpServletRequest request) {

        String userAgent = request.getHeader("User-Agent");

        for (String signal : IEBrowserSignals) {
            if (userAgent.contains(signal))
                return true;
        }
        return false;
    }

    
    public static void exportExcel(XSSFWorkbook workbook, int sheetNum,
                            String sheetTitle, String[] headers, List> 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 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);

    
    public static final String DATE_FORMAT_YYYY = "yyyy";


    
    public static final String DATE_FORMAT_YYYYMM = "yyyyMM";

    
    public static final String DATE_FORMAT_YYYY_MM = "yyyy-MM";


    
    public static final String DATE_FORMAT_YYMMDD = "yyMMdd";

    
    public static final String DATE_FORMAT_YY_MM_DD = "yy-MM-dd";

    
    public static final String DATE_FORMAT_YYYYMMDD = "yyyyMMdd";

    
    public static final String DATE_FORMAT_YYYY_MM_DD = "yyyy-MM-dd";

    
    public static final String DATE_FORMAT_POINTYYYYMMDD = "yyyy.MM.dd";

    
    public static final String DATE_TIME_FORMAT_YYYY年MM月DD日 = "yyyy年MM月dd日";

    
    public static final String DATE_FORMAT_YYYYMMDDHHmm = "yyyyMMddHHmm";

    
    public static final String DATE_TIME_FORMAT_YYYYMMDD_HH_MI = "yyyyMMdd HH:mm";

    
    public static final String DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI = "yyyy-MM-dd HH:mm";

    
    public static final String DATE_TIME_FORMAT_YYYYMMDDHHMISS = "yyyyMMddHHmmss";

    
    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";

    
    public static final String DATE_TIME_FORMAT_YYYYMMDDHHMISSSSS = "yyyyMMddHHmmssSSS";

    
    
    public static final String DATE_FORMAT_MMDDHHMI = "MM-dd HH:mm";

    
    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";
    

    
    public static Integer getYear(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.YEAR);
    }

    
    public static Integer getMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.MONTH) + 1;
    }

    
    public static Integer getDay(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int day = cal.get(Calendar.DATE);//获取日
        return day;
    }

    
    public static String parseDateToStr(Date time, String timeFromat) {
        DateFormat dateFormat = new SimpleDateFormat(timeFromat);
        return dateFormat.format(time);
    }

    
    public static String parseTimestampToStr(Timestamp timestamp, String timeFromat) {
        SimpleDateFormat df = new SimpleDateFormat(timeFromat);
        return df.format(timestamp);
    }

    
    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);
        }
    }

    
    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;
        }
    }

    
    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;
    }

    
    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;
        }
    }

    
    public static Date strToDate(String strTime) {
        if (strTime == null || strTime.trim().length() <= 0)
            return null;

        Date date = null;
        List list = new ArrayList(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;
    }

    
    public static List 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 list = new ArrayList();

        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;
        }
    }

    
    public static List 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 list = new ArrayList();

        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;
        }
    }

    
    public static List getYearListOfYears(int before, int behind) {
        if (before < 0 || behind < 0) {
            return null;
        }
        List list = new ArrayList();
        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;
    }

    
    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);
    }

    
    public static HashMap getWeekTimeOfYear(int year) {
        HashMap map = new linkedHashMap();
        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;

    }

    
    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;
    }

    
    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();
    }

    
    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();
    }

    
    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());
    }

    
    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());
    }

    
    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();
    }

    
    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();
    }

    
    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];
    }

    
    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];
    }

    
    public static Integer getDayWeekOfDate2(Date date) {
        Calendar aCalendar = Calendar.getInstance();
        aCalendar.setTime(date);
        int weekDay = aCalendar.get(Calendar.DAY_OF_WEEK);
        return weekDay;
    }

    
    public static boolean validateIsDate(String strTime) {
        if (strTime == null || strTime.trim().length() <= 0)
            return false;

        Date date = null;
        List list = new ArrayList(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;
    }

    
    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();
    }

    
    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();
    }


    
    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;
    }

    
    public static Long getDistanceTimestamp(Date startDate, Date endDate) {
        long daysBetween = (endDate.getTime() - startDate.getTime() + 1000000) / (3600 * 24 * 1000);
        return daysBetween;
    }

    
    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;
    }

    
    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;
    }

    
    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;
    }

    
    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;
    }

    
    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;
    }

    
    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();
    }

    
    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();
    }


    
    public static String formatISO8601(Date dateTime) {
        DateFormat sdf = new SimpleDateFormat(ISO8601_DATE_FORMAT);
        return sdf.format(dateTime);
    }

    
    public static Date fromISO8601(String dateTime) {
        try {
            return new SimpleDateFormat(ISO8601_DATE_FORMAT).parse(dateTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    
    public static String formatDateTime(Date dateTime) {
        DateFormat sdf = new SimpleDateFormat(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
        return sdf.format(dateTime);
    }

    
    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;
    }


    
    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;
    }

    
    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;
    }

    
    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 getCurrentAndBefore7DaysBeginAndEndDateTime() {
        List 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 getCurrentAndBefore30DaysBeginAndEndDateTime() {
        List 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;
    }

    
    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;
    }

    
    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;
    }

    
    public static String getCurrentDateTimeWithYyyyMMdd() {
        DateFormat sdf = new SimpleDateFormat(DATE_FORMAT_YYYYMMDD);
        Date date = new Date();
        String dateTime = sdf.format(date);
        return dateTime;
    }

    
    public static String getCurrentDate() {
        DateFormat sdf = new SimpleDateFormat(DATE_FORMAT_YYYY_MM_DD);
        Date date = new Date();
        String resultDate = sdf.format(date);
        return resultDate;
    }

    
    public static String getCurrentDateTimeWithYyyyMMddHHmmss() {
        DateFormat sdf = new SimpleDateFormat(DATE_TIME_FORMAT_YYYYMMDDHHMISS);
        Date date = new Date();
        String dateTime = sdf.format(date);
        return dateTime;
    }

    
    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;
    }

    
    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;
    }

    
    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;
    }

    
    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;
    }


    
    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());
    }

    
    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());
    }


    
    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;
    }

    
    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;
    }

    
    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;
    }

    
    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"));
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/771050.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号