java小知识点

时间:2022-07-01 15:15:27

1 判断是否为win系统

int version=System.getProperty("os.name").toLowerCase().indexOf("windows");//os=-1则不是windows系统

2 两个日期间隔的天数

        String startDay="1989-10-28";
String endDay="1989-12-01";
SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM-dd");
long a=(sdf1.parse(endDay).getTime()-sdf1.parse(startDay).getTime())/(86400000);

3 object转化为String时,null处理方法

Object aObject=null;//toString会报错,+""直接为null,用(aObject==null?"":aObject.toString())
System.out.println("结果为"+(aObject==null?"":aObject.toString())+";"+aObject+"");

其他

        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(day)+"; day 原型:"+sdf.parse(sdf.format(day)));
System.out.println(sdf.format(now)+"; day 原型:"+sdf.parse(sdf.format(now)).getTime());
System.out.println(sdf.format(calendar.getTime())+"; day 原型:"+calendar.getTime()+" ; "+calendar.getTimeInMillis());
calendar.setTime(day);
calendar.setTimeInMillis(now);
System.out.println(calendar);