Jodd技術總結

JODD中的時間操作類

  [java]

  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) ;

  @Test

  public void testConstructor()

  {

  /*

  * 構造函數的使用

  */

  JDateTime jdt = new JDateTime();        // set current date and time

  jdt = new JDateTime(2012, 12, 21);      // set 21st December 2012, midnight

  jdt = new JDateTime(System.currentTimeMillis());

  jdt = new JDateTime(2012, 12, 21, 11, 54, 22, 124);  // set 21st December 2012, 11:54:22.124

  jdt = new JDateTime("2012-12-21 11:54:22.124");     // -//-

  jdt = new JDateTime("12/21/2012", "MM/DD/YYYY");    // set 21st December 2012, midnight

  }

  @Test

  public 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.124

  jdt.set(2012, 12, 21);                      // set 21st December 2012, midnight

  jdt.setDate(2012, 12, 21);                  // change date to 21st December 2012, do not change te time

  jdt.setCurrentTime();                       // set current date and time

  jdt.setYear(1973);                          // change the year

  jdt.setHour(22);                            // change the hour

  jdt.setTime(18, 00, 12, 853);

  }

  @Test

  public 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()) ;

  }

  @Test

  public void testAdd()

  {

  JDateTime jdt = new JDateTime();        // set current date and time

  jdt.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 months

  jdt.addMonth(-120);         // go back 120 months

  jdt.subYear(1);             // go back one year

  jdt.addHour(1234);          // add 1234 hours

  }

  @Test

  public 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")) ;

  }

  @Test

  public 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"));//本年度第幾周


JODD操作properties文件

  [java]

  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) ;

  @Test

  public 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類

  [java]

  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("[email protected]")。to("[email protected]");

  email.subject("主題");

  SendMailSession mailSession = new SmtpSslServer("smtp.qq.com//發送端郵箱服務器協議", "發送端QQ郵箱", "發送端QQ郵箱密碼")。createSession();

  mailSession.open();

  mailSession.sendMail(email);

  mailSession.close();

  System.out.println("發送成功!…");

  }

  @Test

  public void test() throws IOException

  {

  Email email = new Email();

  email.setFrom("[email protected]");

  email.setTo("[email protected]");

  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字符串的操作工具類

  [java]

  package org.xiaochen.test.jodd;

  import org.junit.Test;

  import jodd.util.StringUtil;

  /**

  * String字符串的操作工具類,太強大以至於我要發瘋

  *

  * @author DJZHOU

  *

  */

  public class StringExamUtil

  {

  @Test

  public void stringExam()

  {

  String exam = "abcdefg10101010abcdefg";

  String result = "";

  /*

  * replace 字符替換

  */

  // 將字符串exam中的a替換成b

  result = StringUtil.replace(exam, "a", "b");

  // 將字符串exam中的a替換成8,b替換成9

  result = 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替換成b

  result = StringUtil.replaceFirst(exam, "a", "b");

  // 將字符串exam中的第一個a替換成b 這裏是替換字符

  result = StringUtil.replaceFirst(exam, 'a', 'b');

  // 將字符串exam中的最後一個a替換成b

  result = 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索引的字符是不是d

  System.out.println(StringUtil.isCharAtEqual(exam, 4, 'd'));

  // 對比字符串exam中的第4索引的字符是不是 不是d

  System.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爲開頭,則去掉a

  result = StringUtil.stripLeadingChar(exam, 'a');

  // 若這個字符串以g爲結尾,則去掉g

  result = StringUtil.stripTrailingChar(exam, 'g');

  // 若該字符串爲"" 則返回null 若不是則返回字符串

  result = StringUtil.crop("");

  // 裁剪數組 將""變成null

  StringUtil.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字符開始切割字符串 保留f

  result = StringUtil.cutFromIndexOf(exam, 'f');

  // 從字符串的fg字符串開始切割字符串 保留fg

  result = StringUtil.cutFromIndexOf(exam, "fg");

  // 檢查字符串是否爲abc開頭,若爲此開頭,則切割掉abc

  result = StringUtil.cutPrefix(exam, "abc");

  // 檢查字符串是否爲efg結尾,若爲此結尾,則切割掉efg

  result = StringUtil.cutSuffix(exam, "efg");

  // 檢查字符串是否爲efg開頭或結尾,若爲此開頭或結尾,則切割掉efg

  result = StringUtil.cutSurrounding(exam, "efg");

  // 檢查字符串是否爲abc開頭efg結尾,若爲爲abc開頭efg結尾,則切割掉

  result = StringUtil.cutSurrounding(exam, "abc", "efg");

  // 截取到字符串的f字符開始切割字符串 不保留f

  result = StringUtil.cutToIndexOf(exam, 'f');

  // 截取到字符串的fg字符串開始切割字符串 不保留fg

  result = StringUtil.cutToIndexOf(exam, "fg");

  /*

  * 其他很多小巧的方法,可以自行研究

  */

  System.out.println(result);

  }

  }


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章