JavaSe基础(20)-- 时间日期类

来源:互联网 发布:php static global 编辑:程序博客网 时间:2024/06/05 20:19

时间日期类

Date类

  • 是一个简单的操作类,可以获取完整的日期内部很多已经过时,不推荐使用
  • 所在包:java.util.Date
  • Date date = new Date();

Calendar(抽象类)

  • 可以将取得的时间精确到毫秒
  • 所在包:java.util.Calendar
  • Calendar 类是一个抽象类,通常使用其子类GregorianCalendar

    我们一般使用其子类。

  • 示例:

    //获取到 年 月 日 等日期GregorianCalendar gc = new GregorianCalendar();System.out.print(gc.get(Calendar.YEAR)+"年");  System.out.print(gc.get(Calendar.MONTH)+1 +"月" );  System.out.print(gc.get(Calendar.DAY_OF_MONTH)+"日");System.out.print("一个星期的第  " + gc.get(Calendar.DAY_OF_WEEK) +" 天");System.out.print("时间:");  System.out.print(gc.get(Calendar.HOUR_OF_DAY)+":");  System.out.print(gc.get(Calendar.MINUTE)+":");  System.out.println(gc.get(Calendar.SECOND));  

DateFormat(抽象类)

  • DateFormat用于设置日期格式
  • 所在包:java.text.DateFormat
  • DateFormat类是一个抽象类,可以使用getTimeInstance()、

    getDateInstance() 或 getDateTimeInstance 来新的创建日期-时间格式化程序

    String str = DateFormat.getDateTimeInstance().format(new Date());System.out.println( str);
  • 子类SimpleDateFormat

    SimpleDateFormat formatter  = new SimpleDateFormat ("yyyy-MM-dd KK:mm:ss a"); String fDate = formatter.format(new Date()); System.out.println( fDate);   
0 0
原创粉丝点击