构造方法
| Date() 分配 Date 对象并初始化此对象,以表示分配它的时间(精确到毫秒)。 |
- 新建一个日期类;
Date nowTime = new Date();
System.out.println(nowTime); //Fri Dec 31 15:28:40 CST 2021
日期格式化(SimpleDateFormat)
当我们对输出格式有要求时,会用到SimpleDateFormat这个类
SimpleDateFormat是java.text包下的,专门负责日期格式化的。 yyyy 年 MM 月 dd 日 HH 时 mm 分 ss 秒 SSS 毫秒 在日期格式中,除了y M d H m s S这些字符不能随意写之外,剩下的符号自己随意组织
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
//SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
//SimpleDateFormat sdf = new SimpleDateFormat("yy/MM/dd HH:mm:ss");
String nowTimeStr = sdf.format(nowTime);
System.out.println(nowTimeStr);
String ---> Date;
//假设现在有一个日期字符串String,如何切换成Date类型?
String time = "2008-08-08 08:08:08 888";
//注:字符串日期格式要和SimpleDateFormat格式相同 否则异常:ParseException
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
Date dateTime = sdf2.parse(time);
System.out.println(dateTime); //Fri Aug 08 08:08:08 CST 2008
时间戳 (currentTimeMillis方法)
currentTimeMillis,System类包下的方法
- 获取自1970年1月1日 00:00:00 000到当前系统时间的总毫秒数
long s = System.currentTimeMillis();
System.out.println(s);//1640939183355
- 获取昨天此时的时间
public static void main(String[] args) {
//这个时间是什么时间?
//1970-01-01 00:00:00 001
Date time = new Date(1); //参数是一个毫秒
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
String strTime = sdf.format(time);
//北京是东8区
System.out.println(strTime); //1970-01-01 08:00:00 001
------------------------------------------------------------
//获取昨天的此时的时间
Date time2 = new Date(System.currentTimeMillis() - 1000*60*60*24);
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
String strTime2 = sdf.format(time2);
//北京是东8区
System.out.println(strTime2); //2021-12-30 16:50:47 679
}
public static void main(String[] args){
//需求:统计一个方法执行所耗费的时长
//在调用目标方法前记录一个毫秒数
long begin = System.currentTimeMillis();
print();
//在调用目标方法后记录一个毫秒数
long end = System.currentTimeMillis();
System.out.println("耗费时长 = "+(end-begin)); //耗费时长 = 31
}
public static void print(){
for (int i = 0; i < 1000; i++) {
System.out.println("i = "+i); //打印增加了时长
}
}
- 简单总结System方法
DecimalFormat 是 NumberFormat 的一个具体子类,用于格式化十进制数字。帮你用最快的速度将数字格式化为你需要的样子。
//表示,千分位,保留2个小数
DecimalFormat df = new DecimalFormat("###,###.##");
String s = df.format(1234.567);
System.out.println(s); //1,234.56
//不够位数补0
DecimalFormat df2 = new DecimalFormat("###,###.0000");
String s2 = df2.format(1234.56);
System.out.println(s2); //1,234.5600
(BigDecimal)
//精度级高的100
BigDecimal v1 = new BigDecimal(100);
BigDecimal v2 = new BigDecimal(200);
//求和
BigDecimal v3 = v1.add(v2); //300
//除法
BigDecimal v4 = v2.divide(v1); //2
随机数(Random)
//创建随机数对象
Random random = new Random();
//随机产生一个int类型取值范围内的数字。
int num1 = random.nextInt();
System.out.println(num1); //-228851400
//产生[0 - 100]之间的随机数,不包括101
random.nextInt(101);
小测试:生成5个不重复的随机数,放到数组中
public static void main(String[] args) {
//生成5个不重复的随机数,放到数组中
Random random = new Random();
int[] a = new int[5];
int index = 0;
while(index < a.length){
int r = random.nextInt(6);
if(panDuan(a,r)){
a[index++] = r;
}
}
System.out.println(Arrays.toString(a));
}
//判断数组是否含有该数方法
public static boolean panDuan(int []a,int r){
for (int i = 0; i < a.length; i++) {
if(r == a[i]){
return false;
}
}
return true;
}



