有关时间转换的一些方法

原创:转载请注明出处

1、获取当天结束日期

/**
     * author:GaoFei
     * date:2017-01-09
     * what:根据string日期获得当天结束时间
     *
     * @param dateString
     * @return stringDate
     */
    private String provideDayForEndTime(String dateString){
        if(dateString == null){return null;}
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
        Date currDate = null;
        try {
            currDate = sdf.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar start = Calendar.getInstance();
        start.setTime(currDate);
        start.set(Calendar.HOUR, 0);
        start.set(Calendar.MINUTE, 0);
        start.set(Calendar.SECOND, 0);
        Calendar end = Calendar.getInstance();

        end.setTime(start.getTime());
        end.set(Calendar.HOUR,23);
        end.set(Calendar.MINUTE,59);
        end.set(Calendar.SECOND,59);
        return TimeUtil.setSolrDate(end.getTime().toLocaleString());
    }

2、获得时间戳的前一天截止时间点

/**
     * 获得时间戳的前一天时间
     */
    public static Timestamp getTimeStampBefore(Timestamp timeStamp) {
        Date date;
        date = new Date(timeStamp.getTime());
        if(date == null) {
            return null;
        }
        long dateLong = date.getTime() - 24 * 60 * 60 * 1000;
        Date date2 = new Date(dateLong);
        int hourAmPm;
        if(date2.getHours()<12){
            hourAmPm = 23;
        }else{
            hourAmPm = 11;
        }
        Calendar start = Calendar.getInstance();
        start.setTime(date2);

        start.set(Calendar.HOUR, hourAmPm);
        start.set(Calendar.MINUTE, 59);
        start.set(Calendar.SECOND, 59);
        return new Timestamp(start.getTime().getTime());
    }

3、获得solr时区错误转换时间

/**
     * author:GaoFei
     * date:2016-12-21
     * what:将字符串时间格式进行转换:Tue Dec 20 04:14:33 CST 2016 转换成 2016-12-21
     *
     * @param dateStr
     * @return
     */
    public static String getSolrDateString(String dateStr) {
        if(dateStr == null){return null;}
//        SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US);
        SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
        Date date = null;
        try {
            date = sdf.parse(dateStr);
            long dateLong = date.getTime() - 8*60*60*1000;
            date = new Date(dateLong);
        } catch (ParseException e) {
            e.printStackTrace();
        }
//        String formatStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(dateStr);
        String formatStr2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);

        return formatStr2;
    }

4、将时间戳转换成solr时间

/**
     * author:Gao
/**
     * 将solr时间转为字符串时间
     *
     * @param date
     * @return
     */
    public static String solrDateToNormalDate(String date) {
        if (date == null) {
            return date;
        }
        try {
            date = date.replace("T"," ");
            date = date.replace("Z","");
        } catch (Exception e) {
            e.printStackTrace();
            return date;
        }
        return date;
    }
 
  

 

Fei
     * date:2016-12-26
     * 将时间戳转为solr时间
     * @param timestamp
     * @return
     */
    public static String getSolrDate(Timestamp timestamp) {
        if (timestamp == null) {
            return null;
        }
        String[] dates = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(timestamp).split(" ");
        if (dates.length < 2) {
            return null;
        }
        return dates[0] + "T" + dates[1] + "Z";
    }

转载于:https://www.cnblogs.com/gaoxufei/p/6480546.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值