common-lang

最近用了一下common-lang包中的一些类和方法,感觉对同学们做项目时会有些用,所以写了几个类传上来,供大学互相学习。

一、分数类的使用:

view plaincopy to clipboardprint?
01.import org.apache.commons.lang.math.Fraction;  
02./* 
03. * 分数的测试类 
04. */ 
05.public class FractionDemo {  
06.    public static void main(String[] args) {  
07.        // 直接输出一些常量  
08.        System.out.println(Fraction.FOUR_FIFTHS);  
09.        // 通过字符串得到  
10.        System.out.println(Fraction.getFraction("6/11").doubleValue());  
11.        // 分子分母  
12.        Fraction n1 = Fraction.getFraction(3, 8);   //相当于3/8  
13.        Fraction n2 = Fraction.getFraction(1, 1, 2);  //相当于1又1/2  
14.        System.out.println(n2.toProperString());  
15.        Fraction n3 = n1.add(n2);  
16.        System.out.println(n3);  
17.        //得到分子,得到分母  
18.        Fraction f1 = Fraction.getFraction(0.2131287);  
19.        System.out.println(f1);  
20.        System.out.println(f1.getNumerator());  
21.        System.out.println(f1.getDenominator());  
22.        //两个分数相乘  
23.        System.out.println(n1.multiplyBy(n2));  
24.    }  
25.} 
import org.apache.commons.lang.math.Fraction;
/*
 * 分数的测试类
 */
public class FractionDemo {
 public static void main(String[] args) {
  // 直接输出一些常量
  System.out.println(Fraction.FOUR_FIFTHS);
  // 通过字符串得到
  System.out.println(Fraction.getFraction("6/11").doubleValue());
  // 分子分母
  Fraction n1 = Fraction.getFraction(3, 8); //相当于3/8
  Fraction n2 = Fraction.getFraction(1, 1, 2);  //相当于1又1/2
  System.out.println(n2.toProperString());
  Fraction n3 = n1.add(n2);
  System.out.println(n3);
  //得到分子,得到分母
  Fraction f1 = Fraction.getFraction(0.2131287);
  System.out.println(f1);
  System.out.println(f1.getNumerator());
  System.out.println(f1.getDenominator());
  //两个分数相乘
  System.out.println(n1.multiplyBy(n2));
 }
}

二、日期工具类的使用:

view plaincopy to clipboardprint?
01.import java.util.Date;  
02.import org.apache.commons.lang.time.DateFormatUtils;  
03.import org.apache.commons.lang.time.DateUtils;  
04.import org.apache.commons.lang.time.FastDateFormat;  
05./* 
06. * 日期工具类的使用 
07. */ 
08.public class TestDateAbout {  
09.    public static void main(String[] args) {  
10.        String pattern = "yyyy-MM-dd";  
11.        //格式化输出  
12.        System.out.println(DateFormatUtils.format(new Date(), pattern));  
13.        // 向后加5天  
14.        Date d1 = DateUtils.addDays(new Date(), 5);  
15.        Date d2 = DateUtils.addMonths(new Date(), 7);  
16.        System.out.println(DateFormatUtils.format(d1, pattern));  
17.        System.out.println(DateFormatUtils.format(d2, pattern));  
18.        // 快速格式化  
19.        FastDateFormat fastDateFormat = FastDateFormat.getDateInstance(FastDateFormat.FULL);  
20.        System.out.println(fastDateFormat.format(new Date()));  
21.        //  
22.    }  
23.} 
import java.util.Date;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang.time.DateUtils;
import org.apache.commons.lang.time.FastDateFormat;
/*
 * 日期工具类的使用
 */
public class TestDateAbout {
 public static void main(String[] args) {
  String pattern = "yyyy-MM-dd";
  //格式化输出
  System.out.println(DateFormatUtils.format(new Date(), pattern));
  // 向后加5天
  Date d1 = DateUtils.addDays(new Date(), 5);
  Date d2 = DateUtils.addMonths(new Date(), 7);
  System.out.println(DateFormatUtils.format(d1, pattern));
  System.out.println(DateFormatUtils.format(d2, pattern));
  // 快速格式化
  FastDateFormat fastDateFormat = FastDateFormat.getDateInstance(FastDateFormat.FULL);
  System.out.println(fastDateFormat.format(new Date()));
  //
 }
}

三、字符类工具类的使用:

view plaincopy to clipboardprint?
01./* 
02. * 字符集工具类 
03. */ 
04.public void charSetUtilsDemo() {  
05.    System.out.println("**CharSetUtilsDemo**");  
06.    System.out.println("计算字符串中包含某字符数.");  
07.    System.out.println(CharSetUtils.count("The quick brown fox jumps over the lazy dog.",  
08.            "aeiou"));  
09.    System.out.println("删除字符串中某字符.");  
10.    System.out.println(CharSetUtils.delete("The quick brown fox jumps over the lazy dog.",  
11.            "aeiou"));  
12.    System.out.println("保留字符串中某字符.");  
13.    System.out.println(CharSetUtils.keep("The quick brown fox jumps over the lazy dog.",  
14.            "aeiou"));  
15.    System.out.println("合并重复的字符.");  
16.    System.out.println(CharSetUtils.squeeze("a bbbbbb c dd", "b d"));  
17.} 
/*
 * 字符集工具类
 */
public void charSetUtilsDemo() {
 System.out.println("**CharSetUtilsDemo**");
 System.out.println("计算字符串中包含某字符数.");
 System.out.println(CharSetUtils.count("The quick brown fox jumps over the lazy dog.",
   "aeiou"));
 System.out.println("删除字符串中某字符.");
 System.out.println(CharSetUtils.delete("The quick brown fox jumps over the lazy dog.",
   "aeiou"));
 System.out.println("保留字符串中某字符.");
 System.out.println(CharSetUtils.keep("The quick brown fox jumps over the lazy dog.",
   "aeiou"));
 System.out.println("合并重复的字符.");
 System.out.println(CharSetUtils.squeeze("a bbbbbb c dd", "b d"));
}

四、字符串工具类:

view plaincopy to clipboardprint?
01./** 
02. * 字符串工具类 
03. */ 
04.public static void main(String[] args) {  
05.    // 删除空格  
06.    System.out.println(StringUtils.deleteWhitespace(" abc d "));  
07.    // 代替字符串中的字符  
08.    System.out.println(StringUtils.replaceChars("abc", 'a', 'b'));  
09.    // 只包含数字  
10.    System.out.println("是否是数字:" + StringUtils.isNumeric("12"));  
11.    // 只包含字母和汉字  
12.    System.out.println(StringUtils.isAlpha("abc#$"));  
13.    // 删除指定的字符  
14.    System.out.println(StringUtils.remove("abc", 'b'));  
15.    // 重复30次  
16.    System.out.println(StringUtils.repeat("=", 30));  
17.} 
/**
 * 字符串工具类
 */
public static void main(String[] args) {
 // 删除空格
 System.out.println(StringUtils.deleteWhitespace(" abc d "));
 // 代替字符串中的字符
 System.out.println(StringUtils.replaceChars("abc", 'a', 'b'));
 // 只包含数字
 System.out.println("是否是数字:" + StringUtils.isNumeric("12"));
 // 只包含字母和汉字
 System.out.println(StringUtils.isAlpha("abc#$"));
 // 删除指定的字符
 System.out.println(StringUtils.remove("abc", 'b'));
 // 重复30次
 System.out.println(StringUtils.repeat("=", 30));
}

中文简介: commons-lang.jar、Apache Commons包中的一个,包含了一些数据类型工具类,是java.lang.*的扩展。必须使用的jar包。 Jar文件包含的类: META-INF/MANIFEST.MFMETA-INF/LICENSE.txtMETA-INF/NOTICE.txtorg.apache.commons.lang.ArrayUtils.class org.apache.commons.lang.BitField.class org.apache.commons.lang.BooleanUtils.class org.apache.commons.lang.CharEncoding.class org.apache.commons.lang.CharRange.class org.apache.commons.lang.CharSet.class org.apache.commons.lang.CharSetUtils.class org.apache.commons.lang.CharUtils.class org.apache.commons.lang.ClassUtils.class org.apache.commons.lang.Entities$ArrayEntityMap.class org.apache.commons.lang.Entities$BinaryEntityMap.class org.apache.commons.lang.Entities$EntityMap.class org.apache.commons.lang.Entities$HashEntityMap.class org.apache.commons.lang.Entities$LookupEntityMap.class org.apache.commons.lang.Entities$MapIntMap.class org.apache.commons.lang.Entities$PrimitiveEntityMap.class org.apache.commons.lang.Entities$TreeEntityMap.class org.apache.commons.lang.Entities.class org.apache.commons.lang.IllegalClassException.class org.apache.commons.lang.IncompleteArgumentException.class org.apache.commons.lang.IntHashMap$Entry.class org.apache.commons.lang.IntHashMap.class org.apache.commons.lang.LocaleUtils.class org.apache.commons.lang.NotImplementedException.class org.apache.commons.lang.NullArgumentException.class org.apache.commons.lang.NumberRange.class org.apache.commons.lang.NumberUtils.class org.apache.commons.lang.ObjectUtils$Null.class org.apache.commons.lang.ObjectUtils.class org.apache.commons.lang.RandomStringUtils.class org.apache.commons.lang.SerializationException.class org.apache.commons.lang.SerializationUtils.class org.apache.commons.lang.StringEscapeUtils.class org.apache.commons.lang.StringUtils.class org.apache.commons.lang.SystemUtils.class org.apache.commons.lang.UnhandledException.class org.apache.commons.lang.Validate.class org.apache.commons.lang.WordUtils.class org.apache.commons.lang.builder.CompareToBuilder.class org.apache.commons.lang.builder.EqualsBuilder.class org.apache.commons.lang.builder.HashCodeBuilder.class org.apache.commons.lang.builder.ReflectionToStringBuilder$1.class org.apache.commons.lang.builder.ReflectionToStringBuilder.class org.apache.commons.lang.builder.StandardToStringStyle.class org.apache.commons.lang.builder.ToStringBuilder.class org.apache.commons.lang.builder.ToStringStyle$DefaultToStringStyle.class org.apache.commons.lang.builder.ToStringStyle$MultiLineToStringStyle.class org.apache.commons.lang.builder.ToStringStyle$NoFieldNameToStringStyle.class org.apache.commons.lang.builder.ToStringStyle$ShortPrefixToStringStyle.class org.apache.commons.lang.builder.ToStringStyle$SimpleToStringStyle.class org.apache.commons.lang.builder.ToStringStyle.class org.apache.commons.lang.enum.Enum$Entry.class org.apache.commons.lang.enum.Enum.class org.apache.commons.lang.enum.EnumUtils.class org.apache.commons.lang.enum.ValuedEnum.class org.apache.commons.lang.enums.Enum$Entry.class org.apache.commons.lang.enums.Enum.class org.apache.commons.lang.enums.EnumUtils.class org.apache.commons.lang.enums.ValuedEnum.class org.apache.commons.lang.exception.ExceptionUtils.class org.apache.commons.lang.exception.Nestable.class org.apache.commons.lang.exception.NestableDelegate.class org.apache.commons.lang.exception.NestableError.class org.apache.commons.lang.exception.NestableException.class org.apache.commons.lang.exception.NestableRuntimeException.class org.apache.commons.lang.math.DoubleRange.class org.apache.commons.lang.math.FloatRange.class org.apache.commons.lang.math.Fraction.class org.apache.commons.lang.math.IntRange.class org.apache.commons.lang.math.JVMRandom.class org.apache.commons.lang.math.LongRange.class org.apache.commons.lang.math.NumberRange.class org.apache.commons.lang.math.NumberUtils.class org.apache.commons.lang.math.RandomUtils.class org.apache.commons.lang.math.Range.class org.apache.commons.lang.mutable.Mutable.class org.apache.commons.lang.mutable.MutableBoolean.class org.apache.commons.lang.mutable.MutableByte.class org.apache.commons.lang.mutable.MutableDouble.class org.apache.commons.lang.mutable.MutableFloat.class org.apache.commons.lang.mutable.MutableInt.class org.apache.commons.lang.mutable.MutableLong.class org.apache.commons.lang.mutable.MutableObject.class org.apache.commons.lang.mutable.MutableShort.class org.apache.commons.lang.text.CompositeFormat.class org.apache.commons.lang.text.StrBuilder$StrBuilderReader.class org.apache.commons.lang.text.StrBuilder$StrBuilderTokenizer.class org.apache.commons.lang.text.StrBuilder$StrBuilderWriter.class org.apache.commons.lang.text.StrBuilder.class org.apache.commons.lang.text.StrLookup$MapStrLookup.class org.apache.commons.lang.text.StrLookup.class org.apache.commons.lang.text.StrMatcher$CharMatcher.class org.apache.commons.lang.text.StrMatcher$CharSetMatcher.class org.apache.commons.lang.text.StrMatcher$NoMatcher.class org.apache.commons.lang.text.StrMatcher$StringMatcher.class org.apache.commons.lang.text.StrMatcher$TrimMatcher.class org.apache.commons.lang.text.StrMatcher.class org.apache.commons.lang.text.StrSubstitutor.class org.apache.commons.lang.text.StrTokenizer.class org.apache.commons.lang.time.DateFormatUtils.class org.apache.commons.lang.time.DateUtils$DateIterator.class org.apache.commons.lang.time.DateUtils.class org.apache.commons.lang.time.DurationFormatUtils$Token.class org.apache.commons.lang.time.DurationFormatUtils.class org.apache.commons.lang.time.FastDateFormat$CharacterLiteral.class org.apache.commons.lang.time.FastDateFormat$NumberRule.class org.apache.commons.lang.time.FastDateFormat$PaddedNumberField.class org.apache.commons.lang.time.FastDateFormat$Pair.class org.apache.commons.lang.time.FastDateFormat$Rule.class org.apache.commons.lang.time.FastDateFormat$StringLiteral.class org.apache.commons.lang.time.FastDateFormat$TextField.class org.apache.commons.lang.time.FastDateFormat$TimeZoneDisplayKey.class org.apache.commons.lang.time.FastDateFormat$TimeZoneNameRule.class org.apache.commons.lang.time.FastDateFormat$TimeZoneNumberRule.class org.apache.commons.lang.time.FastDateFormat$TwelveHourField.class org.apache.commons.lang.time.FastDateFormat$TwentyFourHourField.class org.apache.commons.lang.time.FastDateFormat$TwoDigitMonthField.class org.apache.commons.lang.time.FastDateFormat$TwoDigitNumberField.class org.apache.commons.lang.time.FastDateFormat$TwoDigitYearField.class org.apache.commons.lang.time.FastDateFormat$UnpaddedMonthField.class org.apache.commons.lang.time.FastDateFormat$UnpaddedNumberField.class org.apache.commons.lang.time.FastDateFormat.class org.apache.commons.lang.time.StopWatch.class
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值