zoukankan      html  css  js  c++  java
  • Java中的日期

    Date类(java.util.Date)

    时间原点:1970年1月1日 8点0分0秒。

    创建日期对象:

    package blog;
    
    import java.util.Date;
    
    public class Datetest01 {
        public static void main(String[] args) {
            Date nowDate = new Date();
            System.out.println("当前时间是:"+nowDate);
            Date date1 = new Date(10000);
            Date date2 = new Date(-10000);
            System.out.println("以5000定义的时间为:"+date1);
            System.out.println("以5000定义的时间为:"+date2);
        }
    }

    从上面的例子我们可以发现:当直接new Date()时得到的其实是现在的时间。然后当以参数为10000或者-10000时,它是以原点为基准点以毫秒为单位取计算然后得到时间。

    下面是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;
        }

    getTime()

    在上面的测试代码最后加入两行:

    System.out.println("使用getTime():"+nowDate.getTime());
    System.out.println("使用getTime():"+date1.getTime());

    我们发现:getTime()获取的是日期距日期原点的毫秒数

    日期格式转换

    y 代表年
    M 代表月
    d 代表日
    H 代表24进制的小时
    h 代表12进制的小时
    m 代表分钟
    s 代表秒
    S 代表毫秒

    日期转字符串:

    package blog;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    public class DateTest02 {
        public static void main(String[] args) {
            SimpleDateFormat formate1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            SimpleDateFormat formate2 = new SimpleDateFormat("yyyy/MM/dd HH/mm/ss SSS");
            SimpleDateFormat formate3 = new SimpleDateFormat("HH:mm:ss yyyy-MM-dd");
            Date date = new Date();
            String s1 = formate1.format(date);
            String s2 = formate2.format(date);
            String s3 = formate3.format(date);
            System.out.println("formate1格式下时间为:"+s1);
            System.out.println("formate2格式下时间为:"+s2);
            System.out.println("formate3格式下时间为:"+s3);
        }
    }

    字符串转日期

    package blog;
    
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    public class Datetest03 {
        public static void main(String[] args) {
            SimpleDateFormat formate1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            SimpleDateFormat formate2 = new SimpleDateFormat("yyyy/MM/dd HH/mm/ss SSS");
            SimpleDateFormat formate3 = new SimpleDateFormat("HH:mm:ss yyyy-MM-dd");
            String str1 = "2019-4-20 20:49:00";
            String str2 = "2019/4/20 20/49/00 520";
            String str3 = "20:49:00 2019-4-20";
            try {
                Date date1 = formate1.parse(str1);
                Date date2 = formate2.parse(str2);
                Date date3 = formate3.parse(str3);
                System.out.println("时间格式1为:"+date1);
                System.out.println("时间格式2为:"+date2);
                System.out.println("时间格式3为:"+date3);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            
        }
    }

    ---恢复内容结束---

    ---恢复内容结束---

  • 相关阅读:
    关于easyUI tree的节点属性和自定义属性
    $.messager.prompt
    SQL SERVER 查询被锁的表、解锁表
    C# 连接Oracle 11g 无需安装Oracle客户端
    Oracle 常用语句备份
    Oracle中sys和system用户的区别
    Oracle 数据库创建、表空间创建、用户创建 步骤
    Oracle 创建数据库卡死在85%
    yield return 的使用方法
    Oracle SQL Developer 查询时间格式
  • 原文地址:https://www.cnblogs.com/dong973711/p/10742709.html
Copyright © 2011-2022 走看看