Java学习笔记之常用方法类(三) Data和Calendar类、Math、BigInteger和Random类、Random类

以下是博主学java的时候记的一些笔记,分享给大家,如果有错误或者以为的话,可以在下方留言

Date和Calendar类

使用Calendar类中的static方法getInstance()初始化一个Calendar对象。

Calendar calendar = Calendar.getInstance();

calendar.get(Calendar.MOUTH);  返回一个整数,如果为0,表示在一月,依次类推。

calendar.get(DAY_OF_WEEK);  返回一个整数,如果是1,代表星期日,依次类推。

例:

<span style="font-size:18px;">package com.cannerer;

import java.util.Calendar;

import java.util.Date;

 

public class DateAndCalendar {

public static void main(String []args){

Date date=new Date();

System.out.println(date);

Calendar calendar=Calendar.getInstance();

int 月=calendar.get(Calendar.MONTH);

int 年=calendar.get(Calendar.YEAR),

日=calendar.get(Calendar.DAY_OF_MONTH),

    星期=calendar.get(Calendar.DAY_OF_WEEK),

hour=calendar.get(Calendar.HOUR_OF_DAY),

minute=calendar.get(Calendar.MINUTE),

second=calendar.get(Calendar.SECOND);

System.out.println("年:"+年+"\n月:"+(月+1)+"\n日:"+日+"\n星期:"+(星期-1)+"\n小时:"+hour+"\n分钟:"+minute+"\n秒:"+second);

}

}</span>


答案:

<span style="font-size:18px;">Mon Jan 04 03:03:28 CST 2016

年:2016

月:1

日:4

星期:1

小时:3

分钟:3

秒:28</span>

 

Calendar对象可以调用以下方法将日历翻到任意时间,year取负数表示公元前。

public final void set(int year,int mouth,int date)

public final void set(int year,int mouth,int hour,int date)

public final void set(int year,int mouth,int hour,int minute,int second)

例:

<span style="font-size:18px;">package com.xiti;

 

import java.util.Calendar;

import java.util.Scanner;

 

public class rili {

public static void main(String []args){

Scanner sca=new Scanner(System.in);

System.out.println("请输入年份:");

int year=sca.nextInt();

System.out.println("请输入月份:");

int mouth=sca.nextInt();

JianBiao jb=new JianBiao();

jb.setYear(year);

jb.setMouth(mouth);

System.out.println("\t"+year+"年"+mouth+"月"+"日历");

char []a="日一二三四五六".toCharArray();             //将字符串转化成单个字符存放在字符数组a中

for(char i:a){

System.out.printf("%5c",i);

}

String []as=jb.getArray();                          

for(int i=0;i<as.length;i++){

if(i%7==0){

System.out.println();

}

System.out.printf("%4s",as[i]);

}

}

}

class JianBiao{

int year,mouth;

void setYear(int year){

this.year=year;

}

void setMouth(int mouth){

this.mouth=mouth;

}

String[] getArray(){

String []a=new String[42];                 //给字符串数组a申请一个空间大小为42的字符串空间

Calendar calendar=Calendar.getInstance();

calendar.set(year,mouth-1,1);

int x=calendar.get(calendar.DAY_OF_WEEK);

int day=0;

if(mouth==1||mouth==3||mouth==5||mouth==7||mouth==8||mouth==10||mouth==12){

day=31;

}

if(mouth==4||mouth==6||mouth==9||mouth==11){

day=30;

}

if(mouth==2){

if((year%4==0)&&(year%100!=0)||(year%400==0)){

day=29;

}

else{

day=28;

}

}

for(int i=0;i<x;i++){

a[i]="";

}

for(int i=x,n=1;i<day+x;i++,n++){

a[i]=String.valueOf(n);                //将Int型的n转化成String型的n,赋给a[i]

}

for(int i=day+x;i<a.length;i++){

a[i]="";

}

return a;

}

}</span>


答案:

请输入年份:

1995

请输入月份:

11

1995年11月日历

  日   一  二 三  四  五  六

                   1   2   3

   4   5   6   7   8   9  10

  11  12  13  14  15  16  17

  18  19  20  21  22  23  24

  25  26  27  28  29  30    

            

日期的格式化

<span style="color:#000000;">package com.riqi;

 

import java.text.SimpleDateFormat;

import java.util.Date;

 

public class riqilx {

public static void main(String []args){

Date date=new Date();

SimpleDateFormat sin=new SimpleDateFormat("yyyy年M月d日H时m分s秒");       //将sin对象初始化以上模式

String str=sin.format(date);   //将date替换成sin的格式

System.out.println(str);

String s=String.format("%tc", new Date());

System.out.println(s);

}

}</span>

答案:

2016年1月4日2时30分46秒

星期一 一月 04 02:30:46 CST 2016

 

Math,BigIntegerRandom


Math

以下是Math类的常用方法:

public static long abs(double a)   返回a的绝对值

public static double max(double a,double b)  返回a,b的最大值

public static double min(double a,double b)  返回a,b的最小值

public static double random()  产生一个0-1之间的随机数(不含0,1)

public static double pow(double a,double b)  返回ab次幂

public static double sqrt(double a)  返回a的平方根

public static double log(double a)  返回a的对数

public static double sin(double a)  返回正弦值

public static double asin(double a)  返回反正弦值


BigInteger


程序有时需要处理大数据,BigInteger类提供了任意精度的整数运算。

构造方法 : public BigInteger(String val)

如果val含有非数字字符就会发生NumberFormatException异常

以下是BigInteger类的常用方法:

public BigInteger add(BigInteger val) 返回当前大整数对象与val大整数的和

public BigInteger subtract(BigInteger val)  返回两者的差

public BigInteger multiply(BigInteger val)  返回两者的积

public BigInteger divide(BigInteger val)    返回两者的商

public BigInteger remainder(BigInteger val)  返回前者对后者的取余

public int compareTo(BigInteger val)  返回两者的比较结果,返回值分别为1-10,分别表示当前大整数大于,小于,等于val

public BigInteger abs()   返回当前大整数的绝对值

public BigInteger pow(int a)   返回当前大整数对象的a次幂

public BigInteger toString()   返回当前大整数对象的十进制的字符串表示

public BigInteger toString(int p)   返回当前大整数对象p进制的字符串表示

:

package com.MathBigRan;

import java.math.BigInteger;

import java.util.Random;

 

public class MathBigInterRan {

public static void main(String []args){

int a,b;

a=1;

b=2;

int c=Math.max(a, b);

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

System.out.println(c);

Random random=new Random();

System.out.println(random.nextInt(100));

System.out.println(random.nextBoolean());

/***

 *     处理大整数的方法

 */

BigInteger sum=new BigInteger("0"),

   big=new BigInteger("20"),

   low=new BigInteger("1"),

   high=new BigInteger("1"),

           jij=new BigInteger("1");

while(jij.compareTo(big)<=0){

sum=sum.add(high);

jij=jij.add(low);

high=high.multiply(jij);

}

System.out.println(sum);

}

}

答案:

 

0.542956044299147

2

96

false

2561327494111820313

Random

以下是随机产生一个整数的方法

Random random = new Random();

random.nextInt();

如果想要产生一个0m之间的一个整数

Random random = new Random();

random.nextInt(m);

如果想要随机得到truefalse两个boolean

Random random = new Random();

random.nextBoolean();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值