jodd使用示例

来源:互联网 发布:vmware没有mac选项 编辑:程序博客网 时间:2024/05/21 21:50

JODD中的时间操作类 

package org.xiaochen.test.jodd;import java.util.TimeZone;import org.apache.log4j.Logger;import org.junit.Test;import jodd.datetime.JDateTime;/** * JODD中的时间操作类  * @author DJZHOU * */public class JDateTimeUtil {private Logger log = Logger.getLogger(JDateTimeUtil.class) ;@Testpublic void testConstructor(){/* * 构造函数的使用 */JDateTime jdt = new JDateTime();        // set current date and timejdt = new JDateTime(2012, 12, 21);      // set 21st December 2012, midnightjdt = new JDateTime(System.currentTimeMillis());jdt = new JDateTime(2012, 12, 21, 11, 54, 22, 124);  // set 21st December 2012, 11:54:22.124jdt = new JDateTime("2012-12-21 11:54:22.124");     // -//-jdt = new JDateTime("12/21/2012", "MM/DD/YYYY");    // set 21st December 2012, midnight}@Testpublic void testSet(){JDateTime jdt = new JDateTime();        // set current date and time/* * set方法的使用:设定日期时间 */jdt.set(2012, 12, 21, 11, 54, 22, 124);     // set 21st December 2012, 11:54:22.124jdt.set(2012, 12, 21);                      // set 21st December 2012, midnightjdt.setDate(2012, 12, 21);                  // change date to 21st December 2012, do not change te timejdt.setCurrentTime();                       // set current date and timejdt.setYear(1973);                          // change the yearjdt.setHour(22);                            // change the hourjdt.setTime(18, 00, 12, 853);   }@Testpublic void testGet(){JDateTime jdt = new JDateTime();        // set current date and time/* * get方法的使用:读取日期和时间 */jdt.getYear();jdt.getDateTimeStamp();log.warn(jdt.getDateTimeStamp());//获取当前时间log.warn(jdt.getJulianDate());log.warn(jdt.getDay()) ;log.warn(jdt.getDayOfMonth()) ;log.warn(jdt.getDayOfWeek()) ;log.warn(jdt.getDayOfYear()) ;log.warn(jdt.getEra()) ;log.warn(jdt.getFirstDayOfWeek()) ;log.warn(jdt.getFormat()) ;}@Testpublic void testAdd(){JDateTime jdt = new JDateTime();        // set current date and timejdt.add(1, 2, 3, 4, 5, 6, 7);    // add 1 year, 2 months, 3 days, 4 hours...jdt.add(4, 2, 0);           // add 4 years and 2 monthsjdt.addMonth(-120);         // go back 120 monthsjdt.subYear(1);             // go back one yearjdt.addHour(1234);          // add 1234 hours}@Testpublic void testAdd2(){JDateTime jdt = new JDateTime() ;log.warn(jdt.toString("YYYY-MM-DD")) ;jdt.addDay(-20) ;log.warn(jdt.toString("YYYY-MM-DD")) ;jdt.addDay(10, true) ;log.warn(jdt.toString("YYYY-MM-DD")) ;jdt.addYear(1);log.warn(jdt.toString("YYYY-MM-DD")) ;}@Testpublic void testFormat(){JDateTime jdt = new JDateTime();        // set current date and time/** * 转换说明YYYY年MM月DD日D 一周中的第几天 从周一算起MML 月,长型MMS 月,短行DL 一周中的第几天 长型 从周一算起DS 一周中的第几天 短型 从周一算起hh小时mm分钟ss秒mss毫秒DDD 一年中的第几天WW 一年中的第几周WWW 一年中的第几周并用W标识W 一个月中的第几周E 年代,公元前还是公元后TZL 时间长型TZS 时间短型 *  */log.warn(jdt.convertToDate()) ;log.warn(jdt.toString("YYYY-MM-DD"));log.warn(jdt.toString("YYYY-MM-DD hh:mm:ss"));//转换成字符串log.warn(jdt.toString("WW"));//本年度第几周log.warn(jdt.toString("YYYY")) ;}}

JODD操作properties文件

package org.xiaochen.test.jodd;import java.io.File;import java.io.IOException;import org.apache.log4j.Logger;import org.junit.Test;import jodd.props.Props;/** * JODD操作properties文件 * @author DJZHOU * */public class PropUtil {private static Logger log = Logger.getLogger(PropUtil.class) ;@Testpublic void propExam(){/* *读取prop文件中的属性值  */Props p = new Props();log.warn(URLUtil.getClassPath(this.getClass())+"/a.properties") ;try {p.load(new File(URLUtil.getClassPath(this.getClass())+"/a.properties"));} catch (IOException e) {e.printStackTrace();}String story = p.getValue("a");log.warn(p.getBaseValue("a")) ;log.warn(story);log.warn(null == p.getValue("a")) ;log.warn(p.toString()) ;p.setValue("c", "cc") ;}}

JODD操作email类

package org.xiaochen.test.jodd;import java.io.File;import java.io.IOException;import org.junit.Test;import jodd.io.FileUtil;import jodd.mail.Email;import jodd.mail.EmailAttachment;import jodd.mail.EmailMessage;import jodd.mail.SendMailSession;import jodd.mail.SmtpSslServer;import jodd.mail.att.ByteArrayAttachment;import jodd.mail.att.FileAttachment;import jodd.util.MimeTypes;/** * JODD操作email类 *  * @author DJZHOU *  */public class EmailUtil{public static void main(String[] args){Email email = Email.create();email.addMessage(new EmailMessage("消息"));email.addText("邮件内容");email.embedFile(new File("d:/console.txt"));email.from("771842634@qq.com").to("771842634@qq.com");email.subject("主题");SendMailSession mailSession = new SmtpSslServer("smtp.qq.com//发送端邮箱服务器协议", "发送端QQ邮箱", "发送端QQ邮箱密码").createSession();mailSession.open();mailSession.sendMail(email);mailSession.close();System.out.println("发送成功!...");}@Testpublic void test() throws IOException{Email email = new Email(); email.setFrom("infoxxx@jodd.org");email.setTo("igorxxxx@gmail.com");email.setSubject("test7"); EmailMessage textMessage = new EmailMessage("Hello!", MimeTypes.MIME_TEXT_PLAIN);email.addMessage(textMessage); EmailMessage htmlMessage = new EmailMessage(    "<html><META http-equiv=Content-Type content=\"text/html; charset=utf-8\">" +    "<body><h1>Hey!</h1><img src='cid:c.png'><h2>Hay!</h2></body></html>", MimeTypes.MIME_TEXT_HTML);email.addMessage(htmlMessage); EmailAttachment embeddedAttachment =    new ByteArrayAttachment(FileUtil.readBytes("d:\\c.png"), "image/png", "c.png", "c.png");email.attach(embeddedAttachment); EmailAttachment attachment = new FileAttachment(new File("d:\\b.jpg"), "b.jpg", "image/jpeg");email.attach(attachment);}}

String字符串的操作工具类

package org.xiaochen.test.jodd;import org.junit.Test;import jodd.util.StringUtil;/** * String字符串的操作工具类,太强大以至于我要发疯 *  * @author DJZHOU *  */public class StringExamUtil{@Testpublic void stringExam(){String exam = "abcdefg10101010abcdefg";String result = "";/* * replace 字符替换 */// 将字符串exam中的a替换成bresult = StringUtil.replace(exam, "a", "b");// 将字符串exam中的a替换成8,b替换成9result = StringUtil.replace(exam, new String[] { "a", "b" }, new String[] { "8", "9" });// 将字符串exam中的a替换成b 这里是替换字符result = StringUtil.replaceChar(exam, 'a', 'b');// 将字符串exam中的a替换成8,b替换成9 这里是替换字符result = StringUtil.replaceChars(exam, new char[] { 'a', 'b' }, new char[] { '8', '9' });// 将字符串exam中的第一个a替换成bresult = StringUtil.replaceFirst(exam, "a", "b");// 将字符串exam中的第一个a替换成b 这里是替换字符result = StringUtil.replaceFirst(exam, 'a', 'b');// 将字符串exam中的最后一个a替换成bresult = StringUtil.replaceLast(exam, "a", "b");// 将字符串exam中的最后一个a替换成b 这里是替换字符result = StringUtil.replaceLast(exam, 'a', 'b');// 将字符串exam中的a和A替换成FF b和B替换成gg 即忽略大小写result = StringUtil.replaceIgnoreCase(exam, new String[] { "a", "b" }, new String[] { "FF", "gg" });/* * remove 字符移除 */// 将字符串exam中的a移除result = StringUtil.remove(exam, "a");// 将字符串exam中的a移除 移除的是字符result = StringUtil.remove(exam, 'a');// 将字符串exam中的a b移除 移除的是字符 最后一个参数为无限参数result = StringUtil.removeChars(exam, 'a', 'b');// 将字符串exam中的a移除result = StringUtil.removeChars(exam, "a");/* * 判断字符串是否为空 */// 判断字符串exam是否为空System.out.println(StringUtil.isEmpty(exam));// 判断字符串exam是否不为空System.out.println(StringUtil.isNotEmpty(exam));// 判断字符串exam是否为空 这里的空为去掉空格之后System.out.println(StringUtil.isBlank("   "));// 判断字符串exam是否不为空 这里的空为去掉空格之后System.out.println(StringUtil.isNotBlank("   "));// 判断字符串(无限参数)是否都为空 他们之间的关系为并且System.out.println(StringUtil.isAllEmpty(exam, "  ", "null"));// 判断字符串(无限参数)是否都为空 这里的空为去掉空格之后 他们之间的关系为并且System.out.println(StringUtil.isAllBlank(exam, "  ", "null"));// 对比字符串exam中的第4索引的字符是不是dSystem.out.println(StringUtil.isCharAtEqual(exam, 4, 'd'));// 对比字符串exam中的第4索引的字符是不是 不是dSystem.out.println(StringUtil.isCharAtEscaped(exam, 4, 'd'));/* * equals安全的字符串对比是否相等 不需要考虑null.equals等问题 */// 判断字符串exam与aaa是否相等System.out.println(StringUtil.equals(exam, "aaa"));// 判断两个数组是否相等System.out.println(StringUtil.equals(new String[] { "aaa" }, new String[] { "aaa", "bbb" }));// 判断两个数组是否相等 且忽略大小写System.out.println(StringUtil.equalsIgnoreCase(new String[] { "aaa" }, new String[] { "aaa", "bbb" }));// 获取字符串bbb在数组中的索引System.out.println(StringUtil.equalsOne("bbb", new String[] { "aaa", "bbb" }));// 获取字符串bbb在数组中的索引 且忽略大小写System.out.println(StringUtil.equalsOneIgnoreCase("bbb", new String[] { "aaa", "bbb" }));/* * 首字母的更改 */// 首字母大写result = StringUtil.capitalize(exam);// 首字母小写result = StringUtil.uncapitalize(exam);/* * split字符串分割 */// 将字符串按 , 分割String[] array = StringUtil.split("1,2,3,4,5,6,7,8", ",");/* * indexOf 获取字符串中的字符索引 *//* * Strips, crops, trims and cuts */// 若这个字符串以a为开头,则去掉aresult = StringUtil.stripLeadingChar(exam, 'a');// 若这个字符串以g为结尾,则去掉gresult = StringUtil.stripTrailingChar(exam, 'g');// 若该字符串为"" 则返回null 若不是则返回字符串result = StringUtil.crop("");// 裁剪数组 将""变成nullStringUtil.cropAll(new String[] { "", " " });// 去掉字符串两边的空格result = StringUtil.trimDown("  aa  ");// 去掉字符串左边的空格result = StringUtil.trimLeft("  aa  ");// 去掉字符串右边的空格result = StringUtil.trimRight("  aa  ");// 去掉字符串右边的空格String[] array2 = new String[] { "  aa  ", "  b  b" };/* * 去掉数组内空格 */StringUtil.trimAll(array2);StringUtil.trimDownAll(array2);for (String string : array2){System.out.println(string);}/* * 切割字符串 */// 从字符串的f字符开始切割字符串 保留fresult = StringUtil.cutFromIndexOf(exam, 'f');// 从字符串的fg字符串开始切割字符串 保留fgresult = StringUtil.cutFromIndexOf(exam, "fg");// 检查字符串是否为abc开头,若为此开头,则切割掉abcresult = StringUtil.cutPrefix(exam, "abc");// 检查字符串是否为efg结尾,若为此结尾,则切割掉efgresult = StringUtil.cutSuffix(exam, "efg");// 检查字符串是否为efg开头或结尾,若为此开头或结尾,则切割掉efgresult = StringUtil.cutSurrounding(exam, "efg");// 检查字符串是否为abc开头efg结尾,若为为abc开头efg结尾,则切割掉result = StringUtil.cutSurrounding(exam, "abc", "efg");// 截取到字符串的f字符开始切割字符串 不保留fresult = StringUtil.cutToIndexOf(exam, 'f');// 截取到字符串的fg字符串开始切割字符串 不保留fgresult = StringUtil.cutToIndexOf(exam, "fg");/* * 其他很多小巧的方法,可以自行研究 */System.out.println(result);}}