java工具类的制作与使用_Java常用工具类的使用

1.BigDecimal

在大多数的商业计算中,一般采用java.math.BigDecimal类来进行精确计算。

在使用BigDecimal类来进行计算的时候,主要分为以下步骤:

1、用float或者double变量构建BigDecimal对象。

2、通过调用BigDecimal的加,减,乘,除等相应的方法进行算术运算。

3、把BigDecimal对象转换成float,double,int等类型。

一般来说,可以使用BigDecimal的构造方法或者静态方法的valueOf()方法把基本类型的变量构建成BigDecimal对象。

BigDecimal b1 = new BigDecimal(Double.toString(0.48));

BigDecimal b2 = BigDecimal.valueOf(0.48);

对于常用的加,减,乘,除,BigDecimal类提供了相应的成员方法。

public BigDecimal add(BigDecimal value);

public BigDecimal subtract(BigDecimal value);

public BigDecimal multiply(BigDecimal value);

public BigDecimal divide(BigDecimal value);

进行相应的计算后,我们可能需要将BigDecimal对象转换成相应的基本数据类型的变量,可以使用doubleValue()等方法。

package com.ylfeiu.utils;

import java.math.BigDecimal;

public class Decimal {

public static void main(String[] args) {

BigDecimal big = new BigDecimal(Double.toString(15.2658));

BigDecimal big2 = new BigDecimal(3.141592653);

BigDecimal big3 = BigDecimal.valueOf(3.141592653);

System.out.println(big.divide(big2, 6, BigDecimal.ROUND_HALF_UP).doubleValue() + "\n");//6是精度位数,

//ROUND_HALF_UP是向上取整

System.err.println(big + "\n" + big2 + "\n" + big3); } }

2.SimpleDateFormat

SimpleDateFormat的继承关系:

java.lang.Object

|

+----java.text.Format

|

+----java.text.DateFormat

|

+----java.text.SimpleDateFormat

格式化字母涵义:

y 年  M 月 d 日

h 时 在上午或下午 (1~12) ,H 时 在一天中 (0~23)

m 分 s 秒S 毫秒

E 星期

D 一年中的第几天

F 一月中第几个星期几

w 一年中第几个星期

W 一月中第几个星期

a 上午 / 下午 标记符

k 时 在一天中 (1~24)

K 时 在上午或下午 (0~11)

z 时区

package com.ylfeiu.utils;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

/**

*

* @author Administrator

* 该类演示如何格式化时间

*/

public class FormatDateTime {

public static void main(String[] args) {

SimpleDateFormat myFmt = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒 E");// 月,时是大写字母

SimpleDateFormat myFmt1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss E");// 格式符号等价于now.toLocaleString(),也就是说now.toLocaleString()指定了格式

// 当然我们可以像这样自定义格式化yyyy/MM/dd HH:mm:ss

SimpleDateFormat myFmt2 = new SimpleDateFormat("一年中的第 D 天 一年中第w个星期 一月中第W个星期 在一天中k时 z时区");

Date now = new Date();

Calendar c = Calendar.getInstance();

System.err.println(c.get(Calendar.YEAR));//2014

System.err.println(now.getYear());//114?不推荐,已过时

System.out.println("==============================");

Calendar c3 = Calendar.getInstance();

c3.add(Calendar.DATE, 100);//往后推算:100天以后

int year = c3.get(Calendar.YEAR);

int month = c3.get(Calendar.MONTH) + 1;

int date = c3.get(Calendar.DATE);

System.out.println("100天以后是:"+year + "年" + month + "月" + date + "日");

System.out.println("==============================");

System.out.println(myFmt1.format(c.getTime()));

System.out.println(myFmt1.format(now));

System.out.println(myFmt.format(now));

System.out.println(myFmt2.format(now));

//System.out.println(now.toLocaleString());// 已经过时,不推荐使用了,而且月份没有了0(1-10)

System.err.println(now);//Tue Mar 11 17:07:00 CST 2014

}

}

3.随机数类Random

java 的随机数测试程序。共3种获取随机数的方法:

通过System.currentTimeMillis()来获取一个当前时间毫秒数的long型数字

通过Math.random()返回一个0到1之间的double值.

通过Random类来产生一个随机数,这个是专业的Random工具类,功能强大。

package com.ylfeiu.utils;

import java.util.Random;

public class RandomTest {

public static void main(String args[]) {

testSystemTimeMillis();

testMathRandom();

// 新建“种子为1000”的Random对象,并通过该种子去测试Random的API

testRandomAPIs(new Random(1000), " 1st Random(1000)");

testRandomAPIs(new Random(1000), " 2nd Random(1000)");

// 新建“默认种子”的Random对象,并通过该种子去测试Random的API

testRandomAPIs(new Random(), " 1st Random()");

testRandomAPIs(new Random(), " 2nd Random()");

}

/**

* 返回随机数-01:测试System的currentTimeMillis()

*/

private static void testSystemTimeMillis() {

// 通过

final long l = System.currentTimeMillis();

// 通过l获取一个[0, 100)之间的整数

final int i = (int) (l % 100);

System.out.printf(

"\n---- System.currentTimeMillis() ----\n l=%s i=%s\n", l, i);

}

/**

* 返回随机数-02:测试Math的random()

*/

private static void testMathRandom() {

// 通过Math的random()函数返回一个double类型随机数,范围[0.0, 1.0)

final double d = Math.random();

// 通过d获取一个[0, 100)之间的整数

final int i = (int) (d * 100);

System.out.printf("\n---- Math.random() ----\n d=%s i=%s\n", d, i);

}

/**

* 返回随机数-03:测试Random的API

*/

private static void testRandomAPIs(Random random, String title) {

final int BUFFER_LEN = 5;

// 获取随机的boolean值

boolean bool = random.nextBoolean();

// 获取随机的数组buf[]

byte[] buf = new byte[BUFFER_LEN];

random.nextBytes(buf);

// 获取随机的Double值,范围[0.0, 1.0)

double d = random.nextDouble();

// 获取随机的float值,范围[0.0, 1.0)

float f = random.nextFloat();

// 获取随机的int值

int i1 = random.nextInt();

// 获取随机的[0,100)之间的int值

int i2 = random.nextInt(100);

// 获取随机的long值

long l = random.nextLong();

System.out.printf(

"\n---- %s ----\nb=%s, d=%s(范围[0.0, 1.0)), f=%s(范围[0.0, 1.0)), i1=%s, i2=%s(i2指定的有范围,它有重载方法), l=%s, buf=[",

title, bool, d, f, i1, i2, l);

for (byte bt : buf)

System.out.printf("%s, ", bt);

System.out.println("]");

}

}

4.日历类Calendar

从JDK1.1版本开始,在处理日期和时间时,系统推荐使用Calendar类进行实现。在设计上,Calendar类的功能要比Date类强大很多,而且在实现方式上也比Date类要复杂一些,下面就介绍一下Calendar类的使用。

Calendar类是一个抽象类,在实际使用时实现特定的子类的对象,创建对象的过程对程序员来说是透明的,只需要使用getInstance方法创建即可。

其实Calendar类中还提供了很多其它有用的方法,下面简单的介绍几个常见方法的使用。

a、set()

package com.ylfeiu.utils;

import java.util.Calendar;

/**

* 该类计算指定两个日期之间的相差天数

*

* @author Administrator

*

*/

public class DateExample1 {

public static void main(String[] args) {

// 设置两个日期

// 日期:20130219

Calendar c1 = Calendar.getInstance();

c1.set(2013, 3 - 1, 19);

// 日期:20140311

Calendar c2 = Calendar.getInstance();

c2.set(2014, 4 - 1, 11);

// 转换为相对时间

long t1 = c1.getTimeInMillis();

long t2 = c2.getTimeInMillis();

// 计算天数

long days = (t2 - t1) / (24 * 60 * 60 * 1000);

System.out.println(days);

}

}

b.getTime()

c.add()

例子在SimpleDateFormat中有体现

package com.ylfeiu.utils;

import java.util.Calendar;

/**

* 打印当前月份日历

*

* @author Administrator

*

*/

public class GetCalendar {

public static void main(String[] args) {

// 获得当前时间

Calendar c = Calendar.getInstance();

// 设置代表的日期为1号

c.set(Calendar.DATE, 1);

// 获得1号是星期几

int start = c.get(Calendar.DAY_OF_WEEK);

// 获得当前月的最大日期数

int maxDay = c.getActualMaximum(Calendar.DATE);

// 输出标题

System.out.println("星期日星期一 星期二 星期三 星期四 星期五 星期六");

// 输出开始的空格

for (int i = 1; i

System.out.print("");

}

// 输出该月中的所有日期

for (int i = 1; i <= maxDay; i++) {

// 输出日期数字

System.out.print(" " + i);

// 输出分隔空格

System.out.print(" ");

//对前10位数字排版,因为它们只有一位数

if (i < 10) {

System.out.print(' ');

}

// 判断是否换行

if ((start + i - 1) % 7 == 0) {

System.out.println();

}

}

// 换行

System.out.println();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值