五、常用类(二)Date类

今天的博客主题

      基础篇 --》常用类 --》Date类


Date概述

Date 表示特定的瞬间,精确到毫秒。

在JDK1.1的时候就出现了。提供了诸多针对日期操作的方法。

Date源码

public class Date  
  implements java.io.Serializable, Cloneable, Comparable<Date>
{
    /**
     * Allocates a <code>Date</code> object and initializes it so that
     * it represents the time at which it was allocated, measured to the
     * nearest millisecond.
     *
     * @see     java.lang.System#currentTimeMillis()
     */
    public Date() {
        this(System.currentTimeMillis());
    }

    /**
     * Allocates a <code>Date</code> object and initializes it to
     * represent the specified number of milliseconds since the
     * standard base time known as "the epoch", namely January 1,
     * 1970, 00:00:00 GMT.
     *
     * @param   date   the milliseconds since January 1, 1970, 00:00:00 GMT.
     * @see     java.lang.System#currentTimeMillis()
     */
    public Date(long date) {
        fastTime = date;
    }
}

其余方法都忽略,主要看下Date类里的两个构造方法。

有构造方法这个类才可以被实例化,被创建出来。

第一个构造器是无参构造器,通过调用System的currentTimeMillis()方法来获取当前时间戳,这个时间戳是从1970年到当前时间的毫秒级数据。

第二个构造器,可以将一个毫秒级的数据定义为Date格式的日期。

 

代码分析

public class DateDemo {
    public static void main(String[] args) {
       //使用Date类需要 import java.util.Date; 导入这个包才可以使用
    //通过new关键创建date对象
    Date date = new Date();

       //输出系统当前时间
    System.out.println(date);
       //输出1970-1-1 00:00:00到现在这个时间所过的毫秒数
    System.out.println(date.getTime());

       // 将毫秒级数据转换为Date格式日期
    Date date2 = new Date(1550847957698L);
       System.out.println(date2);

       //获取当前年份,年份是从1900年之后开始算的
  int year = date.getYear();
  System.out.print("今天是");
  System.out.print(1900 + year);
  System.out.print("年");

  //获取当前月份,月份默认从0开始
  int month = date.getMonth();
  System.out.print(1 + month);
  System.out.print("月");

  //获取当前几号
  int date1 = date.getDate();
  System.out.print(date1 + "号" );

  //获取当前星期几
  int day = date.getDay();
  System.out.println("星期" + day);

  //获取当前时间小时
  int hours = date.getHours();
  System.out.print("北京时间" + hours + "点");

  //获取当前时间分钟
  int minutes = date.getMinutes();
  System.out.print(minutes + "分");

  //获取当前时间秒
  int seconds = date.getSeconds();
  System.out.print(seconds + "秒");
    }
}
输出结果:
Fri Feb 22 23:06:13 CST 2019
1550847973189
Fri Feb 22 23:05:57 CST 2019
今天是2019年2月22号星期5
北京时间23点06分13秒

通过上面看出直接获取时间是Date类型的,格式是英文格式。看着不太方便。

SimpleDateFormat这个类就派上用场了,一般工作中这个类用的最多的就是日期之间的转换。

就是把这种Date格式类型的转成String字符串格式类型的,转换成大家经常熟知的那种时间格式

public class DateDemo {
    public static void main(String[] args) {
        //通过new关键创建date对象
     Date date = new Date();
        System.out.println("转换前:" + date);

         //需要 import java.text.SimpleDateFormat; 这个包才可以使用
     //时间转换类
     SimpleDateFormat sdf = new SimpleDateFormat();
         String format = sdf.format(date);
         System.out.println("转换后:" + format);
    }
}
输出结果:
转换前:Fri Feb 22 15:52:44 CST 2019
转换后:19-2-22 下午3:52

这是什么鬼?怎么转换成这样子的了。

看下SimpleDateFormat类构造函数

/**
 * Constructs a <code>SimpleDateFormat</code> using the default pattern and
 * date format symbols for the default
 * {@link java.util.Locale.Category#FORMAT FORMAT} locale.
 * <b>Note:</b> This constructor may not support all locales.
 * For full coverage, use the factory methods in the {@link DateFormat}
 * class.
 */
public SimpleDateFormat() {
    this("", Locale.getDefault(Locale.Category.FORMAT));
    applyPatternImpl(LocaleProviderAdapter.getResourceBundleBased().getLocaleResources(locale)
                     .getDateTimePattern(SHORT, SHORT, calendar));
}

/**
 * Constructs a <code>SimpleDateFormat</code> using the given pattern and
 * the default date format symbols for the default
 * {@link java.util.Locale.Category#FORMAT FORMAT} locale.
 * <b>Note:</b> This constructor may not support all locales.
 * For full coverage, use the factory methods in the {@link DateFormat}
 * class.
 * <p>This is equivalent to calling
 * {@link #SimpleDateFormat(String, Locale)
 *     SimpleDateFormat(pattern, Locale.getDefault(Locale.Category.FORMAT))}.
 * @see java.util.Locale#getDefault(java.util.Locale.Category)
 * @see java.util.Locale.Category#FORMAT
 * @param pattern the pattern describing the date and time format
 * @exception NullPointerException if the given pattern is null
 * @exception IllegalArgumentException if the given pattern is invalid
 */
public SimpleDateFormat(String pattern)
{
    this(pattern, Locale.getDefault(Locale.Category.FORMAT));
}

同样是两个构造函数,一个有参一个无参。

刚才我们调用的是无参的构造方法。在这个无参的构造方法上有官方注释描述:大概说是使用默认格式和日期符号

有参构造方法上也有注释:说是可以指定的格式和日期符号。

 

时间格式:中间分割符可用(/、-、*、:、空格、汉字)

格式

意义

示例

YYYY/yyyy

代表4位数年份

2019

yy

代表2位数的年份

2019

MM

代表月份

02

MMM

代表汉语月份

二月

dd

代表几号

22

hh

代表小时(12小时制)

01-12

HH

代表小时(24小时制)

00-23

mm

代表分钟

00-59

ss

代表秒

00-59

SS/SSS

代表毫秒

000-999

使用日期格式之后在看一下

public class DateDemo {
    public static void main(String[] args) {
         //通过new关键创建date对象
     Date date = new Date();
         System.out.println("转换前:" + date);

         //需要 import java.text.SimpleDateFormat; 这个包才可以使用
     //时间转换类
     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
         String format = sdf.format(date);
         System.out.println("转换后:" + format);
    }
}
输出结果:
转换前:Fri Feb 22 16:17:53 CST 2019
转换后:2019-02-22 16:17:53:146

这样是不是就比较方便看了。毫秒根据需求可加可不加。

 

LocalDateTime

在JDK1.8之后新推出了LocalDateTime也是用来操作日期时间的。等后面汇总JDK1.8新特性的时候在着重说一下。

出了这个类之后,Date类基本就快淘汰了

 


这个Date类在工作当中也是比较常用的。这些常用的类,里面的方法比较多,真正用得到的不是很多,还是以后发现用的到的补充进来。

 

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值