得当前时间的工具类

  • 2019 年 10 月 5 日
  • 筆記

原文链接:https://zhidao.baidu.com/question/878271990996467412.html

Date date=new Date();DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String time=format.format(date);    不同的方法介绍如下:    1、通过Date类来获取当前时间。    Date day=new Date()    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")    System.out.println(df.format(day))    2、通过System类中的currentTimeMillis方法来获取当前时间。    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   System.out.println(df.format(System.currentTimeMillis()))    3、通过Calendar类来获取当前时间。    Calendar c = Calendar.getInstance();//可以对每个时间域单独修改    int year = c.get(Calendar.YEAR)    int month = c.get(Calendar.MONTH)    int date = c.get(Calendar.DATE)    int hour = c.get(Calendar.HOUR_OF_DAY)  int minute = c.get(Calendar.MINUTE)    int second = c.get(Calendar.SECOND)    System.out.println(year + "/" + month + "/" + date + " " +hour + ":" +minute + ":" + second)    4、通过Date类来获取当前时间。    Date date = new Date()    String year = String.format("%tY", date)    String month = String.format("%tB", date)    String day = String.format("%te", date)    System.out.println("今天是:"+year+"-"+month+"-"+day)