1.获取系统当前时间:import java.util.Date;
1.1、获取到的日期格式是java默认日期格式
Date time = new Date();
System.out.println(time);//Mon Oct 05 20:18:48 CST 2020
2.1、SimpleDateFormat,专门负责日期格式化
2.2、yyyy年(年是4位) MM月(月是2位) dd日 HH时 mm分 ss秒 SSS毫秒
2.3、注意:在日期格式中除了y M d H m s S不能随便写,剩下字符随意组织。
2.4、SimpleDateFormat sdf = new SimpleDateFormat();有无参构造方法。
2.5、将Date类型转换为String类型。
格式为:2020/10/5 下午8:21
String s = "yyyy-MM-dd HH:mm:ss SSS"; SimpleDateFormat sdf = new SimpleDateFormat(s); String time2 = sdf.format(time); System.out.println(time2);//2020-10-05 20:18:48 966
3.1、SimpleDateFormat sdf = new SimpleDateFormat("格式不能随便写,要和日期字符串格式相同");
3.2、格式不一致,会有异常java.text.ParseException
SimpleDateFormat sdf1 = new SimpleDateFormat(s); try { Date time4 = sdf1.parse(time3); System.out.println(time4);//Tue Aug 18 22:31:16 CST 2020 } catch (ParseException e) { e.printStackTrace(); }
4.统计一个方法耗时:
4.1、获取自1970年1月1日00:00:00 000到当前系统时间的总毫秒数。
long lo = System.currentTimeMillis(); System.out.println(lo);//1601901078452
4.2统计一个方法耗时:
步骤:在调用方法之前记录一个毫秒数,在调用方法之前记录一个毫秒数,相减。
long begin = System.currentTimeMillis(); print(); long end = System.currentTimeMillis(); System.out.println("耗费时常" + (end-begin)); //print()方法 public static void print() { for (int i = 0; i < 100000; i++) { System.out.println(i); } }
4.3、获取昨天的此时时间:
Date t1 = new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24);
1.关于数字的格式化
# 代表任意数字 , 代表千分位 .代表小数点 0代表不够时补0
2.BigDecimal 属于大数据,精度极高。不属于基本数据类型,属于java对象(引用数据类型)
这是SUN提供的一个类。专门用在财务软件当中。java.math.BigDecimal
public class Test { public static void main(String[] args) { String sdf = "###,###.0000"; DecimalFormat df = new DecimalFormat(sdf); String s = df.format(1234.56); System.out.println(s); BigDecimal b = new BigDecimal(100); BigDecimal b1 = new BigDecimal(200); //BigDecimal b2 = b1 + b;这样不行,b与b1都是引用,不能直接使用+求和 BigDecimal b2 = b.add(b1); System.out.println(b2); } }
public class Test { public static void main(String[] args) { //创建随机数对象 Random r = new Random(); //随机产生一个int类型取值范围内的数字。 int n = r.nextInt(); System.out.println(n); //产生【0-100】之间的随机数 //nextInt翻译为:下一个int类型数据是101 n = r.nextInt(101); System.out.println(n); } }
1.语法:
enum 枚举类型名{
枚举值一,枚举值二
}
2.枚举编译之后生成class文件,是一种引用数据类型,枚举中每一个值都可以看作常量。
public class Test { public static void main(String[] args) { Result r = divde(10, 0); System.out.println(r == Result.SUCCESS? "成功":"失败"); } public static Result divde(int a,int b) { try { int c = a/b; return Result.SUCCESS; } catch(Exception e) { return Result.FAIT; } } } enum Result{ SUCCESS,FAIT }
上一篇:八、String类、包装类
下一篇:持续更新中
原文:https://www.cnblogs.com/arick/p/13771524.html