日期类之SimpleDateFormat

1.System 类下的currentTimeMillis();
2.Date类:java.util.Date及其子类java.sql.Date
                 如何创建实例:其下的方法:toString(), getTime()
               (以及其子类java.sql.Date)
3.SimpleDateFormat类 国际化用的
4.Calendar类

5.Math类

6.BigInteger :可以支持任意精度的整数

  BigDecimal : 支持任何精度的定点数。

package com.aff.date;

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

import org.junit.Test;

/*
 *与时间相关的类
 *1.System 类下的currentTimeMillis();
 *2.Date类:java.util.Date及其子类java.sql.Date     
 *          如何创建实例:其下的方法:toString(), getTime()
 *          以及其子类java.sql.Date)
 *3.SimpleDateFormat类 国际化用的
 *4.Calendar类
 */
public class TestDate {

//BigInteger :可以支持任意精度的整数
//BigDecimal : 支持任何精度的定点数。
    @Test
    public void test6(){
     /*        
BigInteger 构造器

      BigInteger(String val)
public BigInteger abs() public BigInteger add(BigInteger val) public BigInteger subtract(BigInteger val) public BigInteger multiply(BigInteger val) public BigInteger divide(BigInteger val) public BigInteger remainder(BigInteger val) public BigInteger pow(int exponent) public BigInteger[] divideAndRemainder(BigInteger val)

BigDecimal 构造器
public BigDecimal(double val) public BigDecimal(String val)

public BigDecimal add(BigDecimal augend) public BigDecimal subtract(BigDecimal subtrahend) public BigDecimal multiply(BigDecimal multiplicand) public BigDecimal divide(BigDecimal divisor, int scale, int roundingMode)
*/ }


//Matn类:提供了一系列静态方法用于科学计算;其方法的参数和返回值类型一般为double型。
    @Test
    public void test5(){
    /*        
      abs     绝对值
      acos,asin,atan,cos,sin,tan  三角函数
      sqrt     平方根
      pow(double a,doble b)     a的b次幂
      log    自然对数
      exp    e为底指数
      max(double a,double b)
      min(double a,double b)
      random()      返回0.0到1.0的随机数
      long round(double a)     double型数据a转换为long型(四舍五入)
      toDegrees(double angrad)     弧度—>角度
      toRadians(double angdeg)     角度—>弧度
        */
    }

//Calendar类:是一个抽象基类,主用用于完成日期字段之间相互操作的功能。 @Test public void test4(){ /* public void set(int field,int value) public void add(int field,int amount) public final Date getTime() public final void setTime(Date date) */ //获取Calendar实例的方法 Calendar c = Calendar.getInstance(); } //计算总天数的方法 private int getDay(String date1, String date2) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd"); Date d1 = sdf.parse(date1); Date d2 = sdf.parse(date2); long milliTime = d2.getTime()-d1.getTime(); return (int) milliTime/1000/3600/24+1; } @Test public void test3() throws ParseException { // 三天打鱼两天晒网 String str1 = "1990-01-01";//传入文本,然后转为日期,再计算天数 String str2 = "2020-25-02"; //String str2 = "1990-01-03"; int dates = getDay(str1, str2); if (dates % 5 == 0 || dates % 5 == 4) { System.out.println("晒网"); }else{ System.out.println("打鱼"); } } @Test public void test2() throws ParseException { /* * java.text.SimpleDateFormat类易于国际化 * 格式化:日期--->文本 使用SimpleDateFormat的format()方法 * 解析:文本---->日期 使用public Date parse(String source) */ // 1.格式化1 SimpleDateFormat sdf = new SimpleDateFormat(); String date = sdf.format(new Date()); // 日期转换成表示日期的字符串 System.out.println(date);// 20-4-2 下午2:11 // 2. 格式化2 SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss"); date = sdf1.format(new Date()); System.out.println(date);// 2020-04-02 02:16:02 // 3.解析 Date date1 = sdf.parse("20-4-2 下午2:11"); System.out.println(date1);// Thu Apr 02 14:11:00 CST 2020 Date date2 = sdf1.parse("2020-04-02 02:16:02");// 需要和上面的一一对应, System.out.println(date2);// Thu Jan 02 02:16:02 CST 2020 } // java.util.Date不易于国际化 @Test public void test1() { // java.sql.Date d2 = new java.sql.Date(234364369845234L); // System.out.println(d2);// 9396-09-16 // 创建一个Date的实例 Date d1 = new Date(); System.out.println(d1);// Thu Apr 02 13:52:26 CST 2020 System.out.println(d1.getTime());// 1585806746542 Date d2 = new Date(1585806746542L); System.out.println(d2);// Thu Apr 02 13:52:26 CST 2020 } }
All that work will definitely pay off
原文地址:https://www.cnblogs.com/afangfang/p/12620070.html