您的位置:首页 > 编程语言 > Java开发

【原】Java学习笔记031 - 常用类

2017-03-23 18:57 453 查看
package cn.temptation;

public class Sample01 {
public static void main(String[] args) {
/*
* 类 Math:包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。
*
* Math类的常用字段:
* static double E :比任何其他值都更接近 e(即自然对数的底数)的 double 值。
* static double PI :比任何其他值都更接近 pi(即圆的周长与直径之比)的 double 值。
*
* Math类的常用成员方法:
* 1、static int abs(int a) :返回 int 值的绝对值。
* 2、static double ceil(double a) :返回最小的(最接近负无穷大)double 值,该值大于等于参数,并等于某个整数。
* 3、static double floor(double a) :返回最大的(最接近正无穷大)double 值,该值小于等于参数,并等于某个整数。
* 4、static int max(int a, int b) :返回两个 int 值中较大的一个。
* 5、static int min(int a, int b) :返回两个 int 值中较小的一个。
* 6、static double pow(double a, double b) :返回第一个参数的第二个参数次幂的值。
* 7、static double random() :返回带正号的 double 值,该值大于等于 0.0 且小于 1.0。
* 8、static long round(double a) :返回最接近参数的 long。
* 9、static double sqrt(double a) :返回正确舍入的 double 值的正平方根。
*
*/
System.out.println(Math.E);            // 2.718281828459045
System.out.println(Math.PI);        // 3.141592653589793

System.out.println(Math.abs(-2));    // 2

System.out.println(Math.ceil(12.34));    // 13.0
System.out.println(Math.ceil(12.56));    // 13.0

System.out.println(Math.floor(12.34));    // 12.0
System.out.println(Math.floor(12.56));    // 12.0

System.out.println(Math.max(2, 3));        // 3
System.out.println(Math.min(2, 3));        // 2

System.out.println(Math.pow(2, 3));        // 8.0

System.out.println(Math.random());        // 0.6111530715212237

System.out.println(Math.round(12.34));    // 12
System.out.println(Math.round(12.56));    // 13

// 需求:获取指定范围内的随机数
System.out.println(getRandom(50, 100));
}

/**
* 获取指定范围内的随机数
* @param start        起始值
* @param end        终止值
* @return            指定范围内的随机数
*/
public static int getRandom(int start, int end) {
return (int)(Math.random() * (end - start)) + start;
}
}


package cn.temptation;

import java.util.Random;

public class Sample02 {
public static void main(String[] args) {
/*
* 类 Random:此类的实例用于生成伪随机数流。此类使用 48 位的种子,使用线性同余公式 (linear congruential form) 对其进行了修改。
*
* Random类的构造函数:
* Random() :创建一个新的随机数生成器。
* Random(long seed) :使用单个 long 种子创建一个新的随机数生成器。
*
* Random类的常用成员方法:
* 1、int nextInt() :返回下一个伪随机数,它是此随机数生成器的序列中均匀分布的 int 值。
* 2、int nextInt(int n) :返回一个伪随机数,它是取自此随机数生成器序列的、在 0(包括)和指定值(不包括)之间均匀分布的 int 值。
*         参数:n - 要返回的随机数的范围。必须为正数。
*         返回:下一个伪随机数,在此随机数生成器序列中 0(包括)和 n(不包括)之间均匀分布的 int 值。
* 3、void setSeed(long seed) :使用单个 long 种子设置此随机数生成器的种子。
*/
Random random1 = new Random();
System.out.println(random1.nextInt());

Random random2 = new Random(123);
System.out.println(random2.nextInt());            // -1188957731

Random random3 = new Random();
System.out.println(random3.nextInt(5));

Random random4 = new Random();
random4.setSeed(123);
System.out.println(random4.nextInt());            // -1188957731
}
}


package cn.temptation;

import java.util.Arrays;

public class Sample03 {
public static void main(String[] args) {
/*
* 类 System:包含一些有用的类字段和方法。它不能被实例化。
*
* System类的常用成员方法:
* 1、static void exit(int status) :终止当前正在运行的 Java 虚拟机。
* 2、static long currentTimeMillis() :返回以毫秒为单位的当前时间。
*         返回:当前时间与协调世界时 1970 年 1 月 1 日午夜之间的时间差(以毫秒为单位测量)。
* 3、static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length) :从指定源数组中复制一个数组,复制从指定的位置开始,到目标数组的指定位置结束。
*/
// JVM的正常关闭
//        System.exit(0);
System.out.println("--------------------------------");

// 返回以毫秒为单位的当前时间。(距离协调世界时 1970 年 1 月 1 日午夜)
System.out.println(System.currentTimeMillis());

// 需求:分别统计使用字符串拼接耗时   和  使用字符串缓冲区拼接耗时
//         以拼接字符串"java"和字符串"is simple"

long start1 = System.currentTimeMillis();
System.out.println(start1);

String info = "java";
for (int i = 1; i <= 10000; i++) {
info += "is simple";
}

long end1 = System.currentTimeMillis();
System.out.println(end1);

System.out.println("字符串拼接耗时为:" + (end1 - start1));

long start2 = System.currentTimeMillis();
System.out.println(start2);

StringBuffer sb = new StringBuffer("java");
for (int i = 1; i <= 10000; i++) {
sb.append("is simple");
}

long end2 = System.currentTimeMillis();
System.out.println(end2);

System.out.println("字符串缓冲区拼接耗时为:" + (end2 - start2));

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

int[] arr1 = { 1, 2, 3, 4, 5 };
int[] arr2 = { 6, 7, 8, 9, 10 };
System.out.println(Arrays.toString(arr1));        // [1, 2, 3, 4, 5]
System.out.println(Arrays.toString(arr2));        // [6, 7, 8, 9, 10]

System.arraycopy(arr1, 1, arr2, 2, 2);

System.out.println(Arrays.toString(arr1));        // [1, 2, 3, 4, 5]
System.out.println(Arrays.toString(arr2));        // [6, 7, 2, 3, 10]
}
}


package cn.temptation;

import java.math.BigInteger;

public class Sample04 {
public static void main(String[] args) {
/*
* 类 BigInteger:不可变的任意精度的整数。可以让超出Integer范围的整数进行运算
*
* BigInteger类的常用构造函数:
* BigInteger(String val) :将 BigInteger 的十进制字符串表示形式转换为 BigInteger。
*
*/
System.out.println("Integer包装类的最大值为:" + Integer.MAX_VALUE);        // Integer包装类的最大值为:2147483647

Integer i = new Integer(2147483647);
System.out.println(i);                    // 2147483647

// 语法错误:The literal 2147483648 of type int is out of range
//        Integer j = new Integer(2147483648);
//        System.out.println(j);

BigInteger j = new BigInteger("2147483648");
System.out.println(j);                    // 2147483648
}
}


package cn.temptation;

import java.math.BigInteger;

public class Sample05 {
public static void main(String[] args) {
/*
* BigInteger类的常用成员方法:
* 1、BigInteger add(BigInteger val) :返回其值为 (this + val) 的 BigInteger。
* 2、BigInteger subtract(BigInteger val) :返回其值为 (this - val) 的 BigInteger。
* 3、BigInteger multiply(BigInteger val) :返回其值为 (this * val) 的 BigInteger。
* 4、BigInteger divide(BigInteger val) :返回其值为 (this / val) 的 BigInteger。
* 5、BigInteger[] divideAndRemainder(BigInteger val) :返回包含 (this / val) 后跟 (this % val) 的两个 BigInteger 的数组。
*/
BigInteger i = new BigInteger("2");
BigInteger j = new BigInteger("3");

System.out.println("add:" + i.add(j));                // add:5
System.out.println("subtract:" + i.subtract(j));    // subtract:-1
System.out.println("multiply:" + i.multiply(j));    // multiply:6
System.out.println("divide:" + i.divide(j));        // divide:0

BigInteger[] arr = i.divideAndRemainder(j);
System.out.println("商:" + arr[0]);                    // 商:0
System.out.println("余数:" + arr[1]);                // 余数:2
}
}


package cn.temptation;

import java.math.BigDecimal;

public class Sample06 {
public static void main(String[] args) {
//        System.out.println(0.09 + 0.01);        // 0.09999999999999999
//        System.out.println(1.0 - 0.314);        // 0.6859999999999999
//        System.out.println(1.0414 * 100);        // 104.14000000000001
//        System.out.println(1.301 / 100);        // 0.013009999999999999

// 由上可以看到,在计算时,直接使用double类型或float类型很容易发生了精度丢失的问题
// 针对浮点数精度丢失的问题,Java提供了  BigDecimal类型

/*
* 类 BigDecimal:不可变的、任意精度的有符号十进制数。
*
* BigDecimal类的构造函数:
* BigDecimal(double val) :将 double 转换为 BigDecimal,后者是 double 的二进制浮点值准确的十进制表示形式。
* BigDecimal(String val) :将 BigDecimal 的字符串表示形式转换为 BigDecimal。
*
* BigDecimal类的成员方法:
* 1、BigDecimal add(BigDecimal augend) :返回一个 BigDecimal,其值为 (this + augend),其标度为 max(this.scale(), augend.scale())。
* 2、BigDecimal subtract(BigDecimal subtrahend) :返回一个 BigDecimal,其值为 (this - subtrahend),其标度为 max(this.scale(), subtrahend.scale())。
* 3、BigDecimal multiply(BigDecimal multiplicand) :返回一个 BigDecimal,其值为 (this × multiplicand),其标度为 (this.scale() + multiplicand.scale())。
* 4、 BigDecimal divide(BigDecimal divisor) :返回一个 BigDecimal,其值为 (this / divisor),其首选标度为 (this.scale() - divisor.scale());
如果无法表示准确的商值(因为它有无穷的十进制扩展),则抛出 ArithmeticException。
* 5、BigDecimal divide(BigDecimal divisor, int roundingMode) :返回一个 BigDecimal,其值为 (this / divisor),其标度为 this.scale()。
*/
//        BigDecimal i = new BigDecimal(0.09);
//        System.out.println(i);                    // 0.0899999999999999966693309261245303787291049957275390625
//        BigDecimal j = new BigDecimal(0.01);
//        System.out.println(j);                    // 0.01000000000000000020816681711721685132943093776702880859375
//
//        System.out.println(i.add(j));            // 0.09999999999999999687749774324174723005853593349456787109375

BigDecimal i = new BigDecimal("0.09");
System.out.println(i);                    // 0.09
BigDecimal j = new BigDecimal("0.01");
System.out.println(j);                    // 0.01

System.out.println(i.add(j));            // 0.10
System.out.println("------------------------------------------");

BigDecimal m = new BigDecimal("1.0");
System.out.println(m);                    // 1.0
BigDecimal n = new BigDecimal("0.314");
System.out.println(n);                    // 0.314

System.out.println(m.subtract(n));        // 0.686
System.out.println("------------------------------------------");

BigDecimal x = new BigDecimal("1.0414");
System.out.println(x);                    // 1.0414
BigDecimal y = new BigDecimal("100");
System.out.println(y);                    // 100

System.out.println(x.multiply(y));        // 104.1400
System.out.println("------------------------------------------");

BigDecimal a = new BigDecimal("1.301");
System.out.println(a);                    // 1.301
BigDecimal b = new BigDecimal("100");
System.out.println(b);                    // 100

System.out.println(a.divide(b));        // 0.01301
System.out.println("------------------------------------------");

System.out.println(a.divide(b, BigDecimal.ROUND_HALF_UP));        // 0.013  四舍五入
}
}


package cn.temptation;

import java.util.Date;

public class Sample07 {
public static void main(String[] args) {
/*
* 类 Date:表示特定的瞬间,精确到毫秒。
*
* Date类的常用构造函数:
* Date() :分配 Date 对象并初始化此对象,以表示分配它的时间(精确到毫秒)。
* Date(long date) :分配 Date 对象并初始化此对象,以表示自从标准基准时间(称为“历元(epoch)”,即 1970 年 1 月 1 日 00:00:00 GMT)以来的指定毫秒数。
*
* Date类的常用成员方法:
* 1、long getTime() :返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数。
* 2、void setTime(long time) :设置此 Date 对象,以表示 1970 年 1 月 1 日 00:00:00 GMT 以后 time 毫秒的时间点。
*
*/
// 表示当前日期和时间
Date date1 = new Date();
System.out.println(date1);        // Mon Mar 20 14:04:19 CST 2017

// 表示当前日期和时间
Date date2 = new Date(System.currentTimeMillis());
System.out.println(date2);        // Mon Mar 20 14:04:19 CST 2017

Date date3 = new Date();
System.out.println(date3.getTime());                // 1489990111712
System.out.println(System.currentTimeMillis());        // 1489990111712

date3.setTime(1000);
System.out.println(date3);                            // Thu Jan 01 08:00:01 CST 1970
// 注意:CST  中国所在时区,东八区            GMT   格林尼治时间   标准时间
}
}


package cn.temptation;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Sample08 {
public static void main(String[] args) {
/*
* 类 DateFormat:日期/时间格式化子类的抽象类,它以与语言无关的方式格式化并解析日期或时间。
* 日期/时间格式化子类(如 SimpleDateFormat)允许进行格式化(也就是日期 -> 文本)、解析(文本-> 日期)和标准化。
* 将日期表示为 Date 对象,或者表示为从 GMT(格林尼治标准时间)1970 年 1 月 1 日 00:00:00 这一刻开始的毫秒数。
*
* 类 SimpleDateFormat:一个以与语言环境有关的方式来格式化和解析日期的具体类。它允许进行格式化(日期 -> 文本)、解析(文本 -> 日期)和规范化。
*
* SimpleDateFormat类的构造函数:
* SimpleDateFormat() :用默认的模式和默认语言环境的日期格式符号构造 SimpleDateFormat。
* SimpleDateFormat(String pattern) :用给定的模式和默认语言环境的日期格式符号构造 SimpleDateFormat。
*
*
* DateFormat类的常用成员方法:
* 1、String format(Date date) :将一个 Date 格式化为日期/时间字符串。
* 2、Date parse(String source) :从给定字符串的开始解析文本,以生成一个日期。
*/
// 创建日期对象
Date date1 = new Date();
// 创建日期/时间格式化对象
SimpleDateFormat sdf1 = new SimpleDateFormat();

String str1 = sdf1.format(date1);
System.out.println(str1);                // 17-3-20 下午2:16

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

// 创建日期对象
Date date2 = new Date();
// 创建日期/时间格式化对象
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");

String str2 = sdf2.format(date2);
System.out.println(str2);                // 2017年03月20日 14:19:54

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

String str3 = "2013-02-14 12:12:12";
// SimpleDateFormat构造函数中的模式Pattern需要和字符串中日期时间的格式一致才能转换
SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

// 字符串转换为日期和时间
Date date3 = new Date();
try {
date3 = sdf3.parse(str3);            // Thu Feb 14 12:12:12 CST 2013
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println(date3);
}
}


package cn.temptation;

import java.util.Date;

public class Sample09 {
public static void main(String[] args) {
Date date1 = new Date();
String str1 = DateUtil.dateToString(date1, "yyyy-MM-dd HH:mm:ss");
System.out.println(str1);        // 2017-03-20 14:32:40

Date date2 = DateUtil.stringToDate("2017-03-20", "yyyy-MM-dd");
System.out.println(date2);        // Mon Mar 20 00:00:00 CST 2017
}
}


package cn.temptation;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
* 日期工具类
*/
public class DateUtil {
// 构造函数
public DateUtil() {

}

// 成员方法
/**
* 日期转字符串
* @param date        日期对象
* @param pattern    显示模式
* @return            字符串
*/
public static String dateToString(Date date, String pattern) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
return sdf.format(date);
}

/**
* 字符串转日期
* @param str            字符串
* @param pattern        显示模式
* @return                日期对象
*/
public static Date stringToDate(String str, String pattern) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);

Date result = new Date();
try {
result = sdf.parse(str);
} catch (ParseException e) {
e.printStackTrace();
}

return result;
}
}


package cn.temptation;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class Sample10 {
public static void main(String[] args) throws ParseException {
// 需求:通过键盘录入生日(格式为:yyyy-MM-dd),计算来到这个世界多少天了?

// 思路:
// 1、接收键盘录入的生日字符串,需要做格式检查
// 2、生日字符串转换为日期
// 3、日期转换成毫秒
// 4、转换为天数:(当前日期时间的毫秒数  - 生日日期时间的毫秒数) / 1000 / 60 / 60 / 24

// 1、接收键盘录入的生日字符串,需要做格式检查
String birthday = "";
boolean flag = true;

System.out.println("输入生日日期:");
Scanner input = new Scanner(System.in);
if (input.hasNextLine()) {
birthday = input.nextLine();
// 检查格式是否为yyyy-MM-dd,考虑使用正则表达式(最简单的正则,只判断是不是数字)
String regex = "[0-9]{4}-[0-9]{2}-[0-9]{2}";
flag = birthday.matches(regex);
System.out.println(flag ? "是生日日期" : "不是生日日期");
} else {
System.out.println("输入错误!");
}
input.close();

if (flag) {        // 日期格式正确,才进行计算
// 2、生日字符串转换为日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse(birthday);

// 3、日期转换成毫秒
long birthdayTime = date.getTime();

// 4、转换为天数:(当前日期时间的毫秒数  - 生日日期时间的毫秒数) / 1000 / 60 / 60 / 24
System.out.println("来到这个世界" + (System.currentTimeMillis() - birthdayTime) / 1000 / 60 / 60 / 24 + "天");
}
}
}


package cn.temptation;

import java.util.Calendar;

public class Sample11 {
public static void main(String[] args) {
/*
* 类 Calendar:是一个抽象类,它为特定瞬间与一组诸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等 日历字段之间的转换提供了一些方法,
*         并为操作日历字段(例如获得下星期的日期)提供了一些方法。
*         瞬间可用毫秒值来表示,它是距历元(即格林威治标准时间 1970 年 1 月 1 日的 00:00:00.000,格里高利历)的偏移量。
*
* Calendar 提供了一个类方法 getInstance,以获得此类型的一个通用的对象。
* Calendar 的 getInstance 方法返回一个 Calendar 对象,其日历字段已由当前日期和时间初始化
*
* 类 GregorianCalendar:GregorianCalendar 是 Calendar 的一个具体子类,提供了世界上大多数国家/地区使用的标准日历系统。
*
* Calendar类的常用成员方法:
* 1、static Calendar getInstance() :使用默认时区和语言环境获得一个日历。
* 2、int get(int field) :返回给定日历字段的值。
* 3、void set(int year, int month, int date) :设置日历字段 YEAR、MONTH 和 DAY_OF_MONTH 的值。
* 4、abstract  void add(int field, int amount) :根据日历的规则,为给定的日历字段添加或减去指定的时间量。
*/
Calendar rightnow = Calendar.getInstance();
System.out.println(rightnow);                    // java.util.GregorianCalendar

// 获取年
int year = rightnow.get(Calendar.YEAR);
System.out.println("年份为:" + year);                        // 2017
// 获取月
int month = rightnow.get(Calendar.MONTH);                    // 在格里高利历和罗马儒略历中一年中的第一个月是 JANUARY,它为 0;最后一个月取决于一年中的月份数。
System.out.println("月份为:" + month);                        // 2
// 获取日
int date = rightnow.get(Calendar.DATE);
System.out.println("日为:" + date);                            // 20

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

Calendar calendar1 = Calendar.getInstance();
calendar1.set(2013, 2, 14);
System.out.println(calendar1.get(Calendar.YEAR) + "年" + calendar1.get(Calendar.MONTH) + "月" + calendar1.get(Calendar.DATE) + "日");    // 2013年2月14日

Calendar calendar2 = Calendar.getInstance();
calendar2.add(Calendar.YEAR, -4);
System.out.println(calendar2.get(Calendar.YEAR));            // 2013
}
}


package cn.temptation;

import java.util.Calendar;
import java.util.Scanner;

public class Sample12 {
public static void main(String[] args) {
// 需求:根据录入的年份,获取该年的2月份有多少天?

// 思路:
// 1、获取键盘录入的年份
// 2、通过年份在Calendar对象中设置为输入年份的3月1日
// 3、从3月1日倒退1天
// 4、倒退后,查看位于2月的第多少天

// 1、获取键盘录入的年份
int year = 0;
System.out.println("输入年份:");
Scanner input = new Scanner(System.in);
if (input.hasNextInt()) {
year = input.nextInt();
} else {
System.out.println("输入错误!");
return;
}
input.close();

Calendar calendar = Calendar.getInstance();
// 2、通过年份在Calendar对象中设置为输入年份的3月1日
calendar.set(year, 2, 1);        // 注意:月份设置为2,表示的是3月

// 3、从3月1日倒退1天
calendar.add(calendar.DAY_OF_MONTH, -1);

// 4、倒退后,查看位于2月的第多少天
int dayOfMonth = calendar.get(calendar.DAY_OF_MONTH);

System.out.println(year + "年中的二月有:" + dayOfMonth + "天");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: