Date类和Calendar

Date类(util包下,但大部分方法已被弃用):

  1. 空参构造表示的当前时间;参数为0的构造表示1970年1月1日8点整;因为电脑时区为东八区;
  2. getTime()方法和System.currentTimeMills()方法结果一致,返回的都是long
  3. 将时间转化为给定格式的字符串:
    Date date = new Date();        //获取当前时间
    SimpleDateFormat simpleDate= new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");    //创建日期格式化类对象,参数为想得到的时间格式。
    String currentTime=simpleDate.format(date);        //将Date类型的时间转化为String
  4. .将给定时间字符串转化为Date类型:
    String  Str="2018年12月12日 12:00:00";        //给定的时间字符串
    SimpleDateFormat simpleDate= new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");    //应与上方时间格式一致
    Date date = simpleDate.parse(str);        //将字符串转化为Date对象(此处应有异常处理,检测所给str是否与格式匹配)

 Calendar

猜你喜欢

转载自blog.csdn.net/weixin_42547717/article/details/83515333