java jodd框架介绍及使用示例

简介: Jodd是一个普通开源Java包。你可以把Jodd想象成Java的"瑞士军刀",不仅小,锋利而且包含许多便利的功能。Jodd 提供的功能有: 提供操作Java bean, 可以从各种数据源加载Bean, 简化JDBC的接连与代码, 剖析SQL查询, 处理时间与日期, 操作与格式化String, 搜索本地硬盘上的文件, 帮助处理Servlet请求等。

Jodd是一个普通开源Java包。你可以把Jodd想象成Java的"瑞士军刀",不仅小,锋利而且包含许多便利的功能。Jodd 提供的功能有:
 提供操作Java bean,
 可以从各种数据源加载Bean,
 简化JDBC的接连与代码,
 剖析SQL查询,
 处理时间与日期,
 操作与格式化String,
 搜索本地硬盘上的文件,
 帮助处理Servlet请求等。
除此之外还包含一个很小,但实用的基于JSP的MVC框架。

 

jodd使用示例:

JODD中的时间操作类

jodd时间操作

 1 import jodd.datetime.JDateTime;
 2 
 3 public static void main(String[] args) {
 4         JDateTime jDateTime=new JDateTime();
 5         
 6         Date date1=jDateTime.convertToDate();//当前时间
 7         System.out.println("CostCountForDayDaemon.main()"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SS").format(date1));
 8         
 9         jDateTime.setTime(0, 0, 0, 0);//将当前时间的时分秒毫秒置成零点零分零秒零毫秒
10         Date jDate=jDateTime.convertToDate();//将该时间转化成修改后的时间 
11         System.out.println("CostCountForDayDaemon.main()"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SS").format(jDate));
12         
13         jDateTime.subDay(1);//将当前时间往前减一天
14         Date date=jDateTime.convertToDate();
15         System.out.println("CostCountForDayDaemon.main()"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SS").format(date));
16     }
17 
18 
19 CostCountForDayDaemon.main()2015-03-17 14:54:41 887
20 CostCountForDayDaemon.main()2015-03-17 00:00:00 00
21 CostCountForDayDaemon.main()2015-03-16 00:00:00 00
View Code

 

  1. import java.util.TimeZone;   
  2.    
  3. import org.apache.log4j.Logger;   
  4. import org.junit.Test;   
  5.    
  6. import jodd.datetime.JDateTime;   
  7.    
  8. /** 
  9.  * JODD中的时间操作类  
  10.  * @author DJZHOU 
  11.  * 
  12.  */   
  13. public class JDateTimeUtil {   
  14.     private Logger log = Logger.getLogger(JDateTimeUtil.class) ;   
  15.     @Test   
  16.     public void testConstructor()   
  17.     {   
  18.         /* 
  19.          * 构造函数的使用 
  20.          */   
  21.         JDateTime jdt = new JDateTime();        // set current date and time   
  22.         jdt = new JDateTime(2012, 12, 21);      // set 21st December 2012, midnight   
  23.         jdt = new JDateTime(System.currentTimeMillis());   
  24.         jdt = new JDateTime(2012, 12, 21, 11, 54, 22, 124);  // set 21st December 2012, 11:54:22.124   
  25.         jdt = new JDateTime("2012-12-21 11:54:22.124");     // -//-   
  26.         jdt = new JDateTime("12/21/2012", "MM/DD/YYYY");    // set 21st December 2012, midnight   
  27.     }   
  28.        
  29.     @Test   
  30.     public void testSet()   
  31.     {   
  32.         JDateTime jdt = new JDateTime();        // set current date and time   
  33.         /* 
  34.          * set方法的使用:设定日期时间 
  35.          */   
  36.         jdt.set(2012, 12, 21, 11, 54, 22, 124);     // set 21st December 2012, 11:54:22.124   
  37.         jdt.set(2012, 12, 21);                      // set 21st December 2012, midnight   
  38.         jdt.setDate(2012, 12, 21);                  // change date to 21st December 2012, do not change te time   
  39.         jdt.setCurrentTime();                       // set current date and time   
  40.         jdt.setYear(1973);                          // change the year   
  41.         jdt.setHour(22);                            // change the hour   
  42.         jdt.setTime(18, 00, 12, 853);      
  43.     }   
  44.        
  45.     @Test   
  46.     public void testGet()   
  47.     {   
  48.         JDateTime jdt = new JDateTime();        // set current date and time   
  49.         /* 
  50.          * get方法的使用:读取日期和时间 
  51.          */   
  52.         jdt.getYear();   
  53.         jdt.getDateTimeStamp();   
  54.         log.warn(jdt.getDateTimeStamp());//获取当前时间   
  55.         log.warn(jdt.getJulianDate());   
  56.         log.warn(jdt.getDay()) ;   
  57.         log.warn(jdt.getDayOfMonth()) ;   
  58.         log.warn(jdt.getDayOfWeek()) ;   
  59.         log.warn(jdt.getDayOfYear()) ;   
  60.         log.warn(jdt.getEra()) ;   
  61.         log.warn(jdt.getFirstDayOfWeek()) ;   
  62.         log.warn(jdt.getFormat()) ;   
  63.     }   
  64.        
  65.     @Test   
  66.     public void testAdd()   
  67.     {   
  68.         JDateTime jdt = new JDateTime();        // set current date and time   
  69.         jdt.add(1, 2, 3, 4, 5, 6, 7);    // add 1 year, 2 months, 3 days, 4 hours...   
  70.         jdt.add(4, 2, 0);           // add 4 years and 2 months   
  71.         jdt.addMonth(-120);         // go back 120 months   
  72.         jdt.subYear(1);             // go back one year   
  73.         jdt.addHour(1234);          // add 1234 hours   
  74.     }   
  75.        
  76.     @Test   
  77.     public void testAdd2()   
  78.     {   
  79.         JDateTime jdt = new JDateTime() ;   
  80.         log.warn(jdt.toString("YYYY-MM-DD")) ;   
  81.         jdt.addDay(-20) ;   
  82.         log.warn(jdt.toString("YYYY-MM-DD")) ;   
  83.         jdt.addDay(10, true) ;   
  84.         log.warn(jdt.toString("YYYY-MM-DD")) ;   
  85.         jdt.addYear(1);   
  86.         log.warn(jdt.toString("YYYY-MM-DD")) ;   
  87.     }   
  88.        
  89.     @Test   
  90.     public void testFormat()   
  91.     {   
  92.         JDateTime jdt = new JDateTime();        // set current date and time   
  93.         /** 
  94.          * 转换说明 
  95.             YYYY    年 
  96.             MM      月 
  97.             DD      日 
  98.             D       一周中的第几天 从周一算起 
  99.             MML     月,长型 
  100.             MMS     月,短行 
  101.             DL      一周中的第几天 长型 从周一算起 
  102.             DS      一周中的第几天 短型 从周一算起 
  103.             hh      小时 
  104.             mm      分钟 
  105.             ss      秒 
  106.             mss     毫秒 
  107.             DDD     一年中的第几天 
  108.             WW      一年中的第几周 
  109.             WWW     一年中的第几周并用W标识 
  110.             W       一个月中的第几周 
  111.             E       年代,公元前还是公元后 
  112.             TZL     时间长型 
  113.             TZS     时间短型 
  114.          *  
  115.          */   
  116.         log.warn(jdt.convertToDate()) ;   
  117.         log.warn(jdt.toString("YYYY-MM-DD"));   
  118.            
  119.         log.warn(jdt.toString("YYYY-MM-DD hh:mm:ss"));//转换成字符串   
  120.         log.warn(jdt.toString("WW"));//本年度第几周   
  121.         log.warn(jdt.toString("YYYY")) ;   
  122.     }   
  123. }   


 


 

JODD操作properties文件

  1. import java.io.File;   
  2. import java.io.IOException;   
  3.    
  4. import org.apache.log4j.Logger;   
  5. import org.junit.Test;   
  6.    
  7. import jodd.props.Props;   
  8.    
  9. /** 
  10.  * JODD操作properties文件 
  11.  * @author DJZHOU 
  12.  * 
  13.  */   
  14. public class PropUtil {   
  15.     private static Logger log = Logger.getLogger(PropUtil.class) ;   
  16.     @Test   
  17.     public void propExam(){   
  18.         /* 
  19.          *  读取prop文件中的属性值  
  20.          */   
  21.         Props p = new Props();   
  22.         log.warn(URLUtil.getClassPath(this.getClass())+"/a.properties") ;   
  23.         try {   
  24.             p.load(new File(URLUtil.getClassPath(this.getClass())+"/a.properties"));   
  25.         } catch (IOException e) {   
  26.             e.printStackTrace();   
  27.         }   
  28.         String story = p.getValue("a");   
  29.         log.warn(p.getBaseValue("a")) ;   
  30.         log.warn(story);   
  31.         log.warn(null == p.getValue("a")) ;   
  32.         log.warn(p.toString()) ;   
  33.         p.setValue("c", "cc") ;   
  34.     }   
  35. }   


 


 

JODD操作email类

  1. import java.io.File;   
  2. import java.io.IOException;   
  3.    
  4. import org.junit.Test;   
  5.    
  6. import jodd.io.FileUtil;   
  7. import jodd.mail.Email;   
  8. import jodd.mail.EmailAttachment;   
  9. import jodd.mail.EmailMessage;   
  10. import jodd.mail.SendMailSession;   
  11. import jodd.mail.SmtpSslServer;   
  12. import jodd.mail.att.ByteArrayAttachment;   
  13. import jodd.mail.att.FileAttachment;   
  14. import jodd.util.MimeTypes;   
  15.    
  16. /** 
  17.  * JODD操作email类 
  18.  *  
  19.  * @author DJZHOU 
  20.  *  
  21.  */   
  22. public class EmailUtil   
  23. {   
  24.     public static void main(String[] args)   
  25.     {   
  26.         Email email = Email.create();   
  27.         email.addMessage(new EmailMessage("消息"));   
  28.         email.addText("邮件内容");   
  29.         email.embedFile(new File("d:/console.txt"));   
  30.         email.from("771842634@qq.com").to("771842634@qq.com");   
  31.         email.subject("主题");   
  32.         SendMailSession mailSession = new SmtpSslServer("smtp.qq.com//发送端邮箱服务器协议", "发送端QQ邮箱", "发送端QQ邮箱密码").createSession();   
  33.         mailSession.open();   
  34.         mailSession.sendMail(email);   
  35.         mailSession.close();   
  36.         System.out.println("发送成功!...");   
  37.     }   
  38.     @Test   
  39.     public void test() throws IOException   
  40.     {   
  41.         Email email = new Email();   
  42.             
  43.         email.setFrom("infoxxx@jodd.org");   
  44.         email.setTo("igorxxxx@gmail.com");   
  45.         email.setSubject("test7");   
  46.             
  47.         EmailMessage textMessage = new EmailMessage("Hello!", MimeTypes.MIME_TEXT_PLAIN);   
  48.         email.addMessage(textMessage);   
  49.             
  50.         EmailMessage htmlMessage = new EmailMessage(   
  51.             "<html><META http-equiv=Content-Type content=\"text/html; charset=utf-8\">" +   
  52.             "<body><h1>Hey!</h1><img src='cid:c.png'><h2>Hay!</h2></body></html>",    
  53.         MimeTypes.MIME_TEXT_HTML);   
  54.         email.addMessage(htmlMessage);   
  55.             
  56.         EmailAttachment embeddedAttachment =   
  57.             new ByteArrayAttachment(FileUtil.readBytes("d:\\c.png"), "image/png", "c.png", "c.png");   
  58.         email.attach(embeddedAttachment);   
  59.             
  60.         EmailAttachment attachment = new FileAttachment(new File("d:\\b.jpg"), "b.jpg", "image/jpeg");   
  61.         email.attach(attachment);   
  62.     }   
  63. }   


 

String字符串的操作工具类

    1. import org.junit.Test;   
    2.    
    3. import jodd.util.StringUtil;   
    4.    
    5. /** 
    6.  * String字符串的操作工具类,太强大以至于我要发疯 
    7.  *  
    8.  * @author DJZHOU 
    9.  *  
    10.  */   
    11. public class StringExamUtil   
    12. {   
    13.     @Test   
    14.     public void stringExam()   
    15.     {   
    16.         String exam = "abcdefg10101010abcdefg";   
    17.         String result = "";   
    18.         /* 
    19.          * replace 字符替换 
    20.          */   
    21.         // 将字符串exam中的a替换成b   
    22.         result = StringUtil.replace(exam, "a", "b");   
    23.         // 将字符串exam中的a替换成8,b替换成9   
    24.         result = StringUtil.replace(exam, new String[] { "a", "b" }, new String[] { "8", "9" });   
    25.         // 将字符串exam中的a替换成b 这里是替换字符   
    26.         result = StringUtil.replaceChar(exam, 'a', 'b');   
    27.         // 将字符串exam中的a替换成8,b替换成9 这里是替换字符   
    28.         result = StringUtil.replaceChars(exam, new char[] { 'a', 'b' }, new char[] { '8', '9' });   
    29.         // 将字符串exam中的第一个a替换成b   
    30.         result = StringUtil.replaceFirst(exam, "a", "b");   
    31.         // 将字符串exam中的第一个a替换成b 这里是替换字符   
    32.         result = StringUtil.replaceFirst(exam, 'a', 'b');   
    33.         // 将字符串exam中的最后一个a替换成b   
    34.         result = StringUtil.replaceLast(exam, "a", "b");   
    35.         // 将字符串exam中的最后一个a替换成b 这里是替换字符   
    36.         result = StringUtil.replaceLast(exam, 'a', 'b');   
    37.         // 将字符串exam中的a和A替换成FF b和B替换成gg 即忽略大小写   
    38.         result = StringUtil.replaceIgnoreCase(exam, new String[] { "a", "b" }, new String[] { "FF", "gg" });   
    39.         /* 
    40.          * remove 字符移除 
    41.          */   
    42.         // 将字符串exam中的a移除   
    43.         result = StringUtil.remove(exam, "a");   
    44.         // 将字符串exam中的a移除 移除的是字符   
    45.         result = StringUtil.remove(exam, 'a');   
    46.         // 将字符串exam中的a b移除 移除的是字符 最后一个参数为无限参数   
    47.         result = StringUtil.removeChars(exam, 'a', 'b');   
    48.         // 将字符串exam中的a移除   
    49.         result = StringUtil.removeChars(exam, "a");   
    50.         /* 
    51.          * 判断字符串是否为空 
    52.          */   
    53.         // 判断字符串exam是否为空   
    54.         System.out.println(StringUtil.isEmpty(exam));   
    55.         // 判断字符串exam是否不为空   
    56.         System.out.println(StringUtil.isNotEmpty(exam));   
    57.         // 判断字符串exam是否为空 这里的空为去掉空格之后   
    58.         System.out.println(StringUtil.isBlank("   "));   
    59.         // 判断字符串exam是否不为空 这里的空为去掉空格之后   
    60.         System.out.println(StringUtil.isNotBlank("   "));   
    61.         // 判断字符串(无限参数)是否都为空 他们之间的关系为并且   
    62.         System.out.println(StringUtil.isAllEmpty(exam, "  ", "null"));   
    63.         // 判断字符串(无限参数)是否都为空 这里的空为去掉空格之后 他们之间的关系为并且   
    64.         System.out.println(StringUtil.isAllBlank(exam, "  ", "null"));   
    65.         // 对比字符串exam中的第4索引的字符是不是d   
    66.         System.out.println(StringUtil.isCharAtEqual(exam, 4, 'd'));   
    67.         // 对比字符串exam中的第4索引的字符是不是 不是d   
    68.         System.out.println(StringUtil.isCharAtEscaped(exam, 4, 'd'));   
    69.         /* 
    70.          * equals安全的字符串对比是否相等 不需要考虑null.equals等问题 
    71.          */   
    72.         // 判断字符串exam与aaa是否相等   
    73.         System.out.println(StringUtil.equals(exam, "aaa"));   
    74.         // 判断两个数组是否相等   
    75.         System.out.println(StringUtil.equals(new String[] { "aaa" }, new String[] { "aaa", "bbb" }));   
    76.         // 判断两个数组是否相等 且忽略大小写   
    77.         System.out.println(StringUtil.equalsIgnoreCase(new String[] { "aaa" }, new String[] { "aaa", "bbb" }));   
    78.         // 获取字符串bbb在数组中的索引   
    79.         System.out.println(StringUtil.equalsOne("bbb", new String[] { "aaa", "bbb" }));   
    80.         // 获取字符串bbb在数组中的索引 且忽略大小写   
    81.         System.out.println(StringUtil.equalsOneIgnoreCase("bbb", new String[] { "aaa", "bbb" }));   
    82.         /* 
    83.          * 首字母的更改 
    84.          */   
    85.         // 首字母大写   
    86.         result = StringUtil.capitalize(exam);   
    87.         // 首字母小写   
    88.         result = StringUtil.uncapitalize(exam);   
    89.         /* 
    90.          * split字符串分割 
    91.          */   
    92.         // 将字符串按 , 分割   
    93.         String[] array = StringUtil.split("1,2,3,4,5,6,7,8", ",");   
    94.         /* 
    95.          * indexOf 获取字符串中的字符索引 
    96.          */   
    97.         /* 
    98.          * Strips, crops, trims and cuts 
    99.          */   
    100.         // 若这个字符串以a为开头,则去掉a   
    101.         result = StringUtil.stripLeadingChar(exam, 'a');   
    102.         // 若这个字符串以g为结尾,则去掉g   
    103.         result = StringUtil.stripTrailingChar(exam, 'g');   
    104.         // 若该字符串为"" 则返回null 若不是则返回字符串   
    105.         result = StringUtil.crop("");   
    106.         // 裁剪数组 将""变成null   
    107.         StringUtil.cropAll(new String[] { "", " " });   
    108.         // 去掉字符串两边的空格   
    109.         result = StringUtil.trimDown("  aa  ");   
    110.         // 去掉字符串左边的空格   
    111.         result = StringUtil.trimLeft("  aa  ");   
    112.         // 去掉字符串右边的空格   
    113.         result = StringUtil.trimRight("  aa  ");   
    114.         // 去掉字符串右边的空格   
    115.         String[] array2 = new String[] { "  aa  ", "  b  b" };   
    116.         /* 
    117.          * 去掉数组内空格 
    118.          */   
    119.         StringUtil.trimAll(array2);   
    120.         StringUtil.trimDownAll(array2);   
    121.         for (String string : array2)   
    122.         {   
    123.             System.out.println(string);   
    124.         }   
    125.         /* 
    126.          * 切割字符串 
    127.          */   
    128.         // 从字符串的f字符开始切割字符串 保留f   
    129.         result = StringUtil.cutFromIndexOf(exam, 'f');   
    130.         // 从字符串的fg字符串开始切割字符串 保留fg   
    131.         result = StringUtil.cutFromIndexOf(exam, "fg");   
    132.         // 检查字符串是否为abc开头,若为此开头,则切割掉abc   
    133.         result = StringUtil.cutPrefix(exam, "abc");   
    134.         // 检查字符串是否为efg结尾,若为此结尾,则切割掉efg   
    135.         result = StringUtil.cutSuffix(exam, "efg");   
    136.         // 检查字符串是否为efg开头或结尾,若为此开头或结尾,则切割掉efg   
    137.         result = StringUtil.cutSurrounding(exam, "efg");   
    138.         // 检查字符串是否为abc开头efg结尾,若为为abc开头efg结尾,则切割掉   
    139.         result = StringUtil.cutSurrounding(exam, "abc", "efg");   
    140.         // 截取到字符串的f字符开始切割字符串 不保留f   
    141.         result = StringUtil.cutToIndexOf(exam, 'f');   
    142.         // 截取到字符串的fg字符串开始切割字符串 不保留fg   
    143.         result = StringUtil.cutToIndexOf(exam, "fg");   
    144.         /* 
    145.          * 其他很多小巧的方法,可以自行研究 
    146.          */   
    147.         System.out.println(result);   
    148.     }   
    149. }  
相关文章
|
29天前
|
算法 Java 数据处理
Java集合框架的优缺点
Java集合框架的优缺点
|
1月前
|
Java 开发工具
【GDAL-java的四个常用代码示例】
【GDAL-java的四个常用代码示例】
33 0
|
1月前
|
运维 监控 Java
推荐一款好用的Java分布式任务调度框架!
推荐一款好用的Java分布式任务调度框架!
165 0
|
4天前
|
Java 大数据 云计算
Spring框架:Java后台开发的核心
【4月更文挑战第15天】Spring框架在Java后台开发中占据核心位置,因其控制反转(IoC)、面向切面编程(AOP)、事务管理等特性提升效率和质量。Spring提供数据访问集成、RESTful Web服务和WebSocket支持。优势包括高效开发、灵活扩展、强大生态圈和广泛应用。应用于企业级应用、微服务架构及云计算大数据场景。掌握Spring对Java开发者至关重要。
|
7天前
|
存储 Java 编译器
Java集合丛林:深入了解集合框架的秘密
Java集合丛林:深入了解集合框架的秘密
13 0
Java集合丛林:深入了解集合框架的秘密
|
11天前
|
存储 Java 数据库连接
java使用mp持久化框架,写入5000个字符,但是VARCHAR(255) 会报错
使用Java的MyBatis Plus框架时,如果尝试将超过VARCHAR(255)限制的字符串(如5000个字符)存入数据库,会抛出异常。解决方法是将列类型改为TEXT。可通过在实体类属性上添加`@TableField(typeHandler = JdbcType.CLOB)`注解,如`private String content;`,将属性映射到CLOB类型列,以存储更长字符串。
9 0
|
11天前
|
存储 Java
java反射——设计框架的灵魂
java反射——设计框架的灵魂
|
16天前
|
前端开发 安全 Java
使用Java Web框架:Spring MVC的全面指南
【4月更文挑战第3天】Spring MVC是Spring框架的一部分,用于构建高效、模块化的Web应用。它基于MVC模式,支持多种视图技术。核心概念包括DispatcherServlet(前端控制器)、HandlerMapping(请求映射)、Controller(处理请求)、ViewResolver(视图解析)和ModelAndView(模型和视图容器)。开发流程涉及配置DispatcherServlet、定义Controller、创建View、处理数据、绑定模型和异常处理。
使用Java Web框架:Spring MVC的全面指南
|
18天前
|
Java 关系型数据库 数据库连接
52 类 110 个常用 Java 组件和框架整理
52 类 110 个常用 Java 组件和框架整理
13 0
|
30天前
最全三大框架整合(使用映射)——DeptAction.java
最全三大框架整合(使用映射)——DeptAction.java
4 0