Java 经常使用类Math、System、时间相关Calender和Date

在线api,能够查询想要找的类的方法

http://tool.oschina.net/apidocs/apidoc?api=jdk-zhjava

Math类

两个常量
E:天然对数的底数 2.718
PI:圆周率
经常使用方法:
static double abs(double a)
static double cbrt(double a)
static double sqrt(double a)
static double ceil(double a)
static double floor(double a)
static long max(long a, long b)
static int min(int a, int b)
static double pow(double a, double b)
static double random()
static long round(double a) 24.5 24.2 -24.6api

System

1.用地描述系统资源的类型,该类不用建立对象,直接使用静态变量和静态方法便可。
2.经常使用字段
  System.out.println();
  System.in
  System.out
  System.err
3.经常使用方法
  gc():强制垃圾回收器回收内存中的垃圾
  exit(int status):结束虚拟机
  static long currentTimeMillis():返回当前时间 毫秒 dom

Date

1.类 Date 表示特定的瞬间,根据构造方法使用的不一样,表示没的瞬间。
2.Date()
  Date(long date)
3.boolean after(Date when)
  boolean before(Date when)
  long getTime()
  void setTime(long time)
2.DateFormat
  DateFormat是一个抽象类,不能建立对象,提供一个实现类SimpleDateFormat
  String format(Date date)
  Date parse(String source)
练习:键盘录入一个本身的生日,计算今天是本身出生的第几天。ide

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

public class Demo02 {
    public static void main(String[] args) throws ParseException {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        System.out.println("请输入出生年月日xxxx-xx-xx:");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date d = sdf.parse(str);
        Date d1 = new Date();
        long d2 = d1.getTime()-d.getTime();
        System.out.println(d2/1000/3600/24/365);
        sc.close();
    }
}
练习

Calendar

1.表示一个精确的瞬间,是一个包含了不少字段值的对象。
2.Calendar在util包下,使用的时候须要导包
3.Calendar是一个抽象类,不能直接建立对象
  1.使用子类来建立对象
  2.使用父类中的某个方法,来获取一个对象。
4.获取对象的方式
  Calendar getInstance() 能够获取当前时间的对象。
5.经常使用方法
  1.get set方法
  2. void add(int field, int amount)
  3. long getTimeInMillis()
6.set方法
  void set(int field, int value)
  void set(int year, int month, int date)
  void set(int year, int month, int date, int hourOfDay, int minute, int second)spa

毫秒值 Date类型和Calendar类型的相互转换
  1.毫秒值和Date转换
    毫秒值-->Date:构造方法
    Date-->毫秒值:getTime()
  2.毫秒值和Calendar转换
    毫秒值-->Calendar:setTimeInMillis(long millis)
    Calendar-->毫秒:getTimeMillis()
  3.Date和Calendar的转换
    Date-->calendar:setTime()
    Calendar-->Date:getTime();.net

相关文章
相关标签/搜索