package com.fanjia.day20200815;
import org.junit.Test;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Created by FanJia on 2020/8/15 19:22
*/
public class Demo02 {
@Test
//Date 构造函数
//
public void text1(){
Date date = new Date();
Date date1 = new Date(0L);
System.out.println(date);
System.out.println(date1);
}
//getTime()
//setTime()
//getMonth()
//getHours()
//compareTo()
@Test
public void text2(){
Date date = new Date();
long time = date.getTime();
System.out.println(time);
date.setTime(0L);
long time1 = date.getTime();
System.out.println(time1);
Date date1 = new Date();
int month = date1.getMonth();
System.out.println(month+1);
int hours = date1.getHours();
System.out.println(hours);
int i = date1.compareTo(date);
System.out.println(i);
int i1 = date.compareTo(date1);
System.out.println(i1);
int i2 = date.compareTo(date);
System.out.println(i2);
}
//new SimpleDateFormat(yyyy-MM-dd);
//format(Date date);
//parse("1999-09-09");
@Test
public void test3() throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date time = sdf.parse("1999-09-09 09:09:09");
System.out.println(time);
Date date = new Date();
String formatTime = sdf.format(date);
System.out.println(formatTime);
}
//Calculate survival years
@Test
public void test4() throws ParseException {
String s1 = "1999-09-09";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = simpleDateFormat.parse(s1);
long time1 = date.getTime();
long time2= System.currentTimeMillis();
System.out.println("years of birth:" +(time2 - time1)/1000/60/60/24/365);
}
//BigDecimal.valueOf(2)
@Test
public void test5(){
BigDecimal bd = new BigDecimal(2);
System.out.println(bd);
}
@Test
public void test6(){
BigDecimal bd1 = new BigDecimal(10.11+"");
BigDecimal bd2 = new BigDecimal(3.2356+"");
BigDecimal add = bd1.add(bd2);
System.out.println(add);
System.out.println(bd1.multiply(bd2));
System.out.println(bd1.subtract(bd2));
System.out.println(bd1.divide(bd2,5, RoundingMode.HALF_UP));
}
@Test
public void test7(){
BigInteger bi = new BigInteger(8+"");
BigInteger bi2 = new BigInteger(3+"");
System.out.println(bi.add(bi2));
System.out.println(bi.subtract(bi2));
System.out.println(bi.multiply(bi2));
System.out.println(bi.divide(bi2));
}
}
Date方法,SimpleDateFormat方法,BigDecimal,BigInterger方法的使用,
于 2020-08-15 20:36:54 首次发布