取没有date的邮件发送时间

      有些邮件由于某些原因可能会没有dete属性,因此用getSentDate();跟getReceivedDate();方法是不到时间的,

在这种情况下就只能通过邮件的头信息来获取时间信息。如图:

 

public String getHeader(){
        Enumeration temp;
        String ReceivedDate="";
        Date date=null;
        String[] lines = new String[]{"Received"};//获取Received的信息
        try {
            temp=mimeMessage.getMatchingHeaderLines(lines);
                String Received=(String) temp.nextElement();
                if(Received.split(";").length>1){
                    SimpleDateFormat format = new SimpleDateFormat(dateformat);
                    SimpleDateFormat sdf = new SimpleDateFormat(" EEE, dd MMM yyyy HH:mm:ss Z",Locale.US);//根据Received里提取的时间信息格式决定(ps:必须保持一致)
                        ReceivedDate=Received.split(";")[1];
                         try {
                            date=sdf.parse(ReceivedDate);//将提取的时间转成date
                        } catch (ParseException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        
                     ReceivedDate=format.format(date);//转成自己想要的时间格式
                }
        } catch (MessagingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return ReceivedDate;
    }

 

方法如下:

 

转载于:https://www.cnblogs.com/hgxbo/p/5472762.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值