时间日期相关类总结

 

时间日期相关类总结

Date类

 A.构造方法

  Date();根据当前系统时间创建时间对象

   B.成员方法

  Date(long time)根据传入的毫秒值时间创建出日期对象

 

 

DateFormat类&SimpleDateFormat类

 A.构造方法

  SimpleDateFormat(String s):根据指定模板创建日期格式化对象

   B.成员方法

  String format(Date d):根据指定格式化日期对象

  Date parse(String s):根据指定解析格字符串

 

 

Calendar 类

 A.创建对象的方式

  Calendar = Calendar.newinstance(); 获取日历类对象

   B.成员方法

  int get(int n) 获取指定日历字段信息

  void set(int n ,int value):将指定日历字段设置为指定的值

  void add(int n int value):将指定日历字段加减指定的值

 

复制代码
public static void main(String[] args) throws ParseException {
    //空参构造方法
    Date d = new Date();
    System.out.println(d);//Sat Jun 13 10:29:25 CST 2020
    System.out.println(d.toLocaleString());//13-Jun-2020 10:29:25
    Date d2 = new Date(3000L);
    System.out.println(d2.toLocaleString());//01-Jan-1970 08:00:03
    Date d3 = new Date(0L);
    System.out.println(d3.toLocaleString());//01-Jan-1970 08:00:00

    //创建日期格式化对象
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
    String format = sdf.format(d);
    System.out.println(format);//2020年06月13日 10:32:55
    String str = "2008年09月17日 08:17:00";
    Date parse = sdf.parse(str);
    System.out.println(parse);//Wed Sep 17 08:17:00 CST 2008
}
复制代码
复制代码
public static void main(String[] args) {
    //获取日历类对象
    Calendar c = Calendar.getInstance();

    //get方法
    int year = c.get(Calendar.YEAR);
    System.out.println(year);
    System.out.println("--------------");

    int month = c.get(Calendar.YEAR);
    System.out.println(month);
    System.out.println("--------------");

    //set方法
    c.set(Calendar.YEAR, 2088);
    year = c.get(Calendar.YEAR);
    System.out.println(year);
    System.out.println("--------------");

    //add方法
    c.add(Calendar.YEAR, 2);
    year = c.get(Calendar.YEAR);
    System.out.println(year);
}
复制代码

 

posted @   夫君  阅读(28)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示