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

java常用工具类 Date日期、Mail邮件工具类

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

java常用工具类 Date日期、Mail邮件工具类

本文实例为大家分享了java常用工具类的具体实现代码,供大家参考,具体内容如下

package com.jarvis.base.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class DateHelper {

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

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

 
 public static String formatDate(Date date) {
 return formatDate(date, pattern_time);
 }

 
 public static String formatDate(Date date, String pattern) {
 SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
 return dateFormat.format(date);
 }

 
 public static Date parseString(String dateStr) {
 return parseString(dateStr, "yyyy-MM-dd HH:mm:ss");
 }

 
 public static Date parseString(String dateStr, String pattern) {
 SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
 try {
 if (!StringHelper.isEmpty(dateStr)) {
 return dateFormat.parse(dateStr);
 }
 } catch (ParseException ex) {
 ex.printStackTrace();
 System.err.println(dateStr + "转换成日期失败,可能是不符合格式:" + pattern);
 }
 return null;
 }

 
 public static String getWeekStr(Date date) {
 Calendar calendar = Calendar.getInstance();
 calendar.setTime(date);
 int week = calendar.get(7);
 --week;
 String weekStr = "";
 switch (week) {
 case 0:
 weekStr = "星期日";
 break;
 case 1:
 weekStr = "星期一";
 break;
 case 2:
 weekStr = "星期二";
 break;
 case 3:
 weekStr = "星期三";
 break;
 case 4:
 weekStr = "星期四";
 break;
 case 5:
 weekStr = "星期五";
 break;
 case 6:
 weekStr = "星期六";
 }
 return weekStr;
 }

 
 public static long getDateMiliDispersion(Date date1, Date date2) {
 if ((date1 == null) || (date2 == null)) {
 return 0L;
 }

 long time1 = date1.getTime();
 long time2 = date2.getTime();

 return time1 - time2;
 }

 
 public static int getDateDiff(Date date1, Date date2) {
 if ((date1 == null) || (date2 == null)) {
 return 0;
 }
 long time1 = date1.getTime();
 long time2 = date2.getTime();

 long diff = time1 - time2;

 Long longValue = new Long(diff / 86400000L);
 return longValue.intValue();
 }

 
 public static Date getDataDiff(Date date, int day) {
 if (date == null) {
 return null;
 }
 long time = date.getTime();
 time -= 86400000L * day;
 return new Date(time);
 }

 
 public static int getCurrentWeek() {
 Calendar calendar = Calendar.getInstance();
 int week = calendar.get(7);
 --week;
 if (week == 0) {
 week = 7;
 }
 return week;
 }

 
 public static String getCurrentWeekStr() {
 return getWeekStr(new Date());
 }

 
 public static int getCurrentYear() {
 Calendar calendar = Calendar.getInstance();
 return calendar.get(1);
 }

 
 public static int getCurrentMonth() {
 Calendar calendar = Calendar.getInstance();
 return calendar.get(2) + 1;
 }

 
 public static int getCurrentDay() {
 Calendar calendar = Calendar.getInstance();
 return calendar.get(5);
 }

 
 public static int getUnixTime(String str) {
 if ((str == null) || ("".equals(str))) {
 return 0;
 }
 try {
 long utime = Long.parseLong(str) * 1000L;
 Date date1 = new Date(utime);

 Date date = new Date();

 long nowtime = (date.getTime() - date1.getTime()) / 1000L;
 return (int) nowtime;
 } catch (Exception e) {
 e.printStackTrace();
 System.err.println("获取时差失败");
 }
 return 0;
 }

 
 public static String formatString(String dateTime) {
 if ((dateTime != null) && (dateTime.length() >= 8)) {
 String formatDateTime = dateTime.replaceAll("-", "");
 formatDateTime = formatDateTime.replaceAll(":", "");
 String date = formatDateTime.substring(0, 8);
 return date;
 }

 return "";
 }

 
 public static int getTimesper(String str) {
 if ((str == null) || ("".equals(str))) {
 return 0;
 }
 try {
 Date date1 = new Date(Long.parseLong(str));
 Date date = new Date();
 long nowtime = (date.getTime() - date1.getTime()) / 1000L;
 return (int) nowtime;
 } catch (Exception e) {
 e.printStackTrace();
 System.err.println("日期转换出错");
 }
 return 0;
 }

 
 public static String formatDateTime(String dateTime) {
 if ((dateTime != null) && (dateTime.length() >= 8)) {
 String formatDateTime = dateTime.replaceAll("-", "");
 formatDateTime = formatDateTime.replaceAll(":", "");
 String date = formatDateTime.substring(0, 8);
 String time = formatDateTime.substring(8).trim();
 for (int i = time.length(); i < 6; ++i) {
 time = time + "0";
 }
 return date + time;
 }

 return "";
 }

 
 public static String formatDateTime(Date date) {
 String dateTime = formatDate(date);
 return formatDateTime(dateTime);
 }
}

Mail邮件工具类

package com.jarvis.base.util;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.apache.commons.mail.MultiPartEmail;
import org.apache.commons.mail.SimpleEmail;

 
public class MailHelper
{
 
 public static void sendSimpleEmail(String host, String username, String password, String subject, String contents,
   String toEmailAddress, String fromEmailAddress) throws EmailException
 {
  SimpleEmail email = new SimpleEmail();
  email.setHostName(host);
  email.setAuthentication(username, password);
  email.addTo(toEmailAddress);
  email.setFrom(fromEmailAddress, fromEmailAddress);
  email.setSubject(subject);
  email.setContent((Object)contents, "text/plain;charset=GBK");
  email.send();
 }

 
 public static void sendSimpleEmail(String host, String username, String password, String subject, String contents, String [] toEmailAddress, String fromEmailAddress) throws EmailException
 {
  SimpleEmail email = new SimpleEmail();
  email.setHostName(host);
  email.setAuthentication(username, password);
  //发送给多个人
  for (int i = 0; i < toEmailAddress.length; i++)
  {
   email.addTo(toEmailAddress[i], toEmailAddress[i]);
  }
  email.setFrom(fromEmailAddress, fromEmailAddress);
  email.setSubject(subject);
  email.setContent((Object)contents, "text/plain;charset=GBK");
  email.send();
 }

 

 public static void sendMultiPartEmail(String host, String username, String password, String subject,
    String contents, String toEmailAddress, String fromEmailAddress,
    String []multiPaths) throws MalformedURLException, EmailException
 {
  List attachmentList = new ArrayList();
  if (multiPaths != null)
  {
   for (int i = 0; i < multiPaths.length; i++)
   {
    EmailAttachment attachment = new EmailAttachment();
    if (multiPaths[i].indexOf("http") == -1) //判断当前这个文件路径是否在本地 如果是:setPath 否则 setURL;
    {
     attachment.setPath(multiPaths[i]);
    }
    else
    {
     attachment.setURL(new URL(multiPaths[i]));
    }
    attachment.setDisposition(EmailAttachment.ATTACHMENT);
    attachment.setDescription("");
    attachmentList.add(attachment);
   }
  }

  //发送邮件信息
  MultiPartEmail email = new MultiPartEmail();
  email.setHostName(host);
  email.setAuthentication(username, password);
  email.addTo(toEmailAddress);
  email.setFrom(fromEmailAddress, fromEmailAddress);
  email.setSubject(subject);
  email.setMsg(contents); //注意这个不要使用setContent这个方法 setMsg不会出现乱码
  for (int i = 0; i < attachmentList.size(); i++) //添加多个附件
  {
   email.attach((EmailAttachment) attachmentList.get(i));
  }
  email.send();
 }

 

 public static void sendMultiPartEmail(String host, String username, String password, String subject,
    String contents, String[] toEmailAddress, String fromEmailAddress,
    String []multiPaths) throws MalformedURLException, EmailException
 {
  List attachmentList = new ArrayList();
  if (multiPaths != null)
  {
   for (int i = 0; i < multiPaths.length; i++)
   {
    EmailAttachment attachment = new EmailAttachment();
    if (multiPaths[i].indexOf("http") == -1) //判断当前这个文件路径是否在本地 如果是:setPath 否则 setURL;
    {
     attachment.setPath(multiPaths[i]);
    }
    else
    {
     attachment.setURL(new URL(multiPaths[i]));
    }
    attachment.setDisposition(EmailAttachment.ATTACHMENT);
    attachment.setDescription("");
    attachmentList.add(attachment);
   }
  }

  //发送邮件信息
  MultiPartEmail email = new MultiPartEmail();
  email.setHostName(host);
  email.setAuthentication(username, password);
  //发送给多个人
  for (int i = 0; i < toEmailAddress.length; i++)
  {
   email.addTo(toEmailAddress[i], toEmailAddress[i]);
  }
  email.setFrom(fromEmailAddress, fromEmailAddress);
  email.setSubject(subject);
  email.setMsg(contents); //注意这个不要使用setContent这个方法 setMsg不会出现乱码
  for (int i = 0; i < attachmentList.size(); i++) //添加多个附件
  {
   email.attach((EmailAttachment) attachmentList.get(i));
  }
  email.send();
 }


 
 public static void sendHtmlEmail(String host, String username, String password, String subject, String contents, String toEmailAddress, String fromEmailAddress) throws EmailException
 {
  HtmlEmail email = new HtmlEmail();
  //email.setDebug(true);
  email.setHostName(host);
  email.setAuthentication(username, password);
  email.addTo(toEmailAddress);
  email.setFrom(fromEmailAddress, fromEmailAddress);
  email.setSubject(subject);
  email.setHtmlMsg(CharHelper.GBKto8859(contents));
  email.send();
 }

 
 public static void sendHtmlEmail(String host, String username, String password, String subject, String contents, String[] toEmailAddress, String fromEmailAddress) throws EmailException
 {
  HtmlEmail email = new HtmlEmail();
  email.setHostName(host);
  email.setAuthentication(username, password);
  //发送给多个人
  for (int i = 0; i < toEmailAddress.length; i++)
  {
   email.addTo(toEmailAddress[i], toEmailAddress[i]);
  }
  email.setFrom(fromEmailAddress, fromEmailAddress);
  email.setSubject(subject);
  email.setHtmlMsg(CharHelper.GBKto8859(contents));
  email.send();
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

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

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

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