时间的一些处理
System.currentTimeMillis() //第一种 Date date = new Date(); System.out.println(date.getTime()); //第二种
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH_mm_ss"); //可以自定义一个格式 String dataString = sdf.format(new date());
String timeString="2019-11-16_15_03_14"; Date date3 =new SimpleDateFormat("yyyy-MM-dd_HH_mm_ss").parse(timeString); System.out.println(date3);
long time2=System.currentTimeMillis(); Date date2 =new Date(time2-1000); System.out.println(sdf.format(date2));
方便计算时间使用
Calendar now = Calendar.getInstance(); System.out.println(now.get(Calendar.MONTH)+1);//获取当前的月份,月份需要加1 System.out.println("------2小时之前时间---------"); now.add(Calendar.HOUR_OF_DAY, -2); System.out.println(sdf.format(now.getTime())) Calendar now = Calendar.getInstance(); System.out.println("年:" + now.get(Calendar.YEAR)); System.out.println("月:" + (now.get(Calendar.MONTH) + 1)); System.out.println("日:" + now.get(Calendar.DAY_OF_MONTH)); System.out.println("时:" + now.get(Calendar.HOUR_OF_DAY)); System.out.println("分:" + now.get(Calendar.MINUTE)); System.out.println("秒:" + now.get(Calendar.SECOND));