zoukankan      html  css  js  c++  java
  • 1.4 JAVA日期处理

    一、JAVA日期

    参考链接:https://www.runoob.com/java/java-date-time.html

    • 1.日期两个构造函数

    1.第一个构造函数使用当前日期和时间来初始化对象。Date( )

    2.第二个构造函数接收一个参数,该参数是从1970年1月1日起的毫秒数。Date(long millisec)

    • 2.JAVA日期常用方法

    java.util 包提供了 Date 类来封装当前的日期和时间。

    Date( ) date = new Date(); 当前时间

    1.int compareTo(Date date)
    比较当调用此方法的Date对象和指定日期。两者相等时候返回0。调用对象在指定日期之前则返回负数。调用对象在指定日期之后则返回正数。

    2.before(Date date),after(Date date)

    在前返回ture,后flase

    3.equals(Object date) 相等返回true,否则返回false

    4.String toString( )把此 Date 对象转换为为 String格式

    二、格式化日期

    • 2.1 SimpleDateFormat

    是一个以语言环境敏感的方式来格式化和分析日期的类。SimpleDateFormat 允许你选择任何用户自定义日期时间格式来运行。

    注意:有的格式大写,有的格式小写,例如 MM 是月份,mm 是分;HH 是 24 小时制,而 hh 是 12 小时制。

          Date dNow = new Date( );
          SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd hh:mm:ss");
          System.out.println("当前时间为: " + ft.format(dNow));
    • 2.2 Calendar类

    使用Calendar 类操作时间,

    Calendar c = Calendar.getInstance();//默认是当前日期
    c1.set(2009, 6, 12);//把Calendar对象c1的年月日分别设这为:2009、6、12
    
    • 2.3 使用printf格式化日期

    • 2.4 实际应用

    1.确定调用方法时间间隔

    //1.确定方法调用时间
    long time2 = System.nanoTime();
    dao.queretime();
    long time3 = System.nanoTime();
    logger.info((time3 - time2) / 1000000 + "】毫秒
    //2.测量实际间隔方法2
             long start = System.currentTimeMillis( );
             System.out.println(new Date( ) + "
    ");
             Thread.sleep(5*60*10);
             System.out.println(new Date( ) + "
    ");
             long end = System.currentTimeMillis( );
             long diff = end - start;
             System.out.println("Difference is : " + diff);
        

    2.其他类型时间相互转换,修改时间提前5分钟

    //1.XMLGregorianCalendar 转换为Date在时间修改提前五分钟
                XMLGregorianCalendar date = map.get("startDate");
                GregorianCalendar ca = date.toGregorianCalendar();
                Date now = ca.getTime();
                Date beforTime = new Date(now.getTime() - 300000);
                GregorianCalendar cal = new GregorianCalendar();
                cal.setTime(beforTime);
                XMLGregorianCalendar gc = DatatypeFactory.newInstance().newXMLGregorianCalendar(cal);
                map.put("startDate",gc);
    //2.Date直接修改提前5分钟
            Date now =  map.get("startDate");
            Date beforTime = new Date(now.getTime() - 300000);
            map.put("startDate",beforTime);
            dao.updateStartDate(serviceCode);


  • 相关阅读:
    UVA11375
    uva11806(容斥原理)
    uva10325(容斥原理)
    hdu4135(容斥原理)
    CF798
    多线程
    (转载)SVN 提交操作缩写(A D M R) .
    上不了网,如何判断
    (转载)myeclipse项目名称重命名
    mysql模糊查询
  • 原文地址:https://www.cnblogs.com/Smileing/p/11867276.html
Copyright © 2011-2022 走看看