Introduction to Java lesson 96 - date manipulation

Monkeys learn to program 2020-11-13 02:42:09
introduction java lesson date manipulation


It's not easy to code words , Please pay attention to the official account. , forward , give the thumbs-up , Just move your fingers , No money , Thank you, old fellow. !

1.1 Date And often used API

1.1.1 Java Time in

    · Java The time in use standard class library Date Class represents , It's in milliseconds from a fixed point in time ( Positive but negative ,long type ) Express a specific point in time ;

    ·  A fixed time point is called the era (epoch), yes UTC Time 1970 year 1 month 1 Japan 00:00:00;

    · UTC(Universal Time Coordinated The world adjusts its time ) And GMT(Greenwich Mean Time Greenwich mean time ) equally , It is a scientific standard time with practical purpose .

1.1.2 Date Class introduction

    · java.util.Date Class encapsulates date and time information .

    · Date Most of the methods used for time component calculation of class have been Calendar replace .

 Date date=new Date();
    // Current date and time information of the system
    System.out.println(date);
    //Sun Jan 06 11:52:55 CTS 2013
    long time=date.getTime();
    //1970 year 1 month 1 Millisecond of day to date 

1.1.3 setTime and getTime Method

 /**  Use setTime and getTime Set and get time  */
    public void testSetTime(){
        Date date=new Date();
        // Output the date and time at this time of the day
        System.out.println(date);
        long time=date.getTime();
        // Increase the number of milliseconds a day goes through
        time+=60*60*24*1000;
        date.setTime(time);
        // Output the date and time at this moment tomorrow
        System.out.println(date);
    }

setTime and getTime Method ( To continue 1)

 /**  Get current system time  */
    public void testGetTime(){
        Date date=new Date();
        System.out.println(date);
        
        //1970 year 1 month 1 The number of milliseconds between zero and the present
        long time=date.getTime();
        System.out.println(time);
    }

1.1.4 Date rewrite toString Method

    · Date Rewrote toString() Method , Use a string to describe the current Date The time represented by the object . The format is as follows :

    · Mon Feb 17 15:36:55 CST 2014

1.2 SimpleDateFormat

1.2.1 SimpleDateFormat brief introduction

    · java.text.SimpleDateFormat It's a concrete class that formats and parses dates in a locale related way . It allows formatting ( date -> Text )、 analysis ( Text -> date ) And standardization

    ·  Construction method

        SimpleDateFormat()

        SimpleDateFormat(String pattern) Construct... With the date format symbols of the given pattern and default locale SimpleDateFormat

    ·  Method

        final String format(Date date)  Date=>String

        Date parse(String source) throws ParseException  String=>Date

1.2.2 Date pattern matches characters

image.png

1.2.3 take Date Formatted as String

 /**  Date formatting  */
    public void testFormat(){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date=new Date();
        String dateStr=sdf.format(date);
        System.out.println(dateStr);
    }

1.2.4 take String It can be interpreted as Date

 /** and format Contrary to the method ,parse Method is used to convert a string representing time into Date object */
    public void testParse() throws Exception{
        String str="2013-12-25";
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Date date=sdf.parse(str);
        System.out.println(date);
    }

1.3 Calendar brief introduction

1.3.1 Calendar brief introduction

    · java.util.Calendar Class to encapsulate calendar information , Its main function is that its method can calculate the time component ;

    · Calendar Abstract class , Its specific subclass is for calendar systems in different countries , One of the most widely used is GregorianCalendar( Gregorian calendar , The universal solar calendar ), Corresponding to the vast majority of countries in the world / The standard calendar system used by the region .

1.3.2 getInstance Method

    · Calendar Provides a class method getInstance, To get a common object of this type

    · Calendar Of getInstance Method returns a Calendar object , Its calendar field has been initialized by the current date and time

        Calendar c=Calendar.getInstance();

getInstance Method ( To continue 1)

 /**  Use Calendar And subclass get time  */
    public void testGetInstance(){
        Calendar c=Calendar.getInstance();
        // Output Calendar The actual type of object
        System.out.println(c.getClass().getName());
        //getTime Method returns the corresponding Date object
        System.out.println(c.getTime());
        // establish GregorianCalendar object
        GregorianCalendar c1=new GregorianCalendar(2013,Calendar.DECEMBER,25);
        System.out.println(c1.getTime());
    }

1.3.3 Set date and time components     

 /**  Set date and weight  */
    public void testSet(){
        Calendar c=Calendar.getInstance();
        c.set(Calendar.YEAR,2014);
        c.set(Calendar.MONTH,Calendar.DECEMBER);
        c.set(Calendar.DATE,25);
        System.out.println(c.getTime());
        //Thu Dec 25 16:02:08 CST 2014
        c.set(Calendar.DATE,32);
        System.out.println(c.getTime());
        //Thu Jan 01 16:02:08 CST 2015
    
    }

 1.3.4 Get date and time components

    ·  Use Calendar Provided get Method and some constants can get date and time components

    · static int YEAR The field number of the prompt year

    · static int MONTH Field number indicating the month

    · static int DATE Indicates the day of the month

    · static int DAY_OF_WEEK Indicate a day of the week ,1 For Sunday

Get date and time components ( To continue 1)

 /**  Get time and weight  */
    public void testGet(){
        Calendar c=Calendar.getInstance();
        c.set(Calendar.YEAR,2014);
        c.set(Calendar.MONTH,Calendar.DECEMBER);
        c.set(Calendar.DATE,25);
        
        int dayOfWeek=c.get(Calendar.DAY_OF_WEEK);
        System.out.println(dayOfWeek);
        // Output is 5, On Thursday , Sunday is the day of the week 1 God .
    }

1.3.5 getActualMaximum Method

    · int getActualMaximum(int field) Given this Calendar Time value of , Returns the maximum value that a specified calendar field may have , for example :

int year=2014;
    Calendar c=Calendar.getInstance();
    c.set(Calendar.YEAR,year);
    c.set(Calendar.DATE,1);
    for(int month=Calendar.JANUARY;month<=Calendar.DECEMBER;month++){
        c.set(Calendar.MONTH,month);
        System.out.println(year+" year "+(month+1)+" month :"+c.getActualMaximum(Calendar.DATE)+" God ");
    }

1.3.6 add Method

    · void add(int filed,int mount) Add the value of the given time component to the value of the given , If the given value is negative, subtract the given value

 /** Output a year later and subtract 3 The date of the month */
    public void testAdd(){
        Calendar calendar=Calendar.getInstance();
        calendar.add(Calendar.YEAR,1);// Add a year
        calendar.add(Calendar.MONTH,-3);// reduce 3 Months
        System.out.println("year:"+calendar.get(Calendar.YEAR));
        System.out.println("month"+(calendar.get(Calendar.MONTH)+1));
        System.out.println("day:"+calendar.get(Calendar.DAY_OF_MONTH));
    }

1.3.7 setTime And getTime Method

    · Date getTime()

        Use Date describe Calendar Represents the date of and returns

    · void setTime(Date d)

         send Calendar Express Date The date described

 /** adopt Date Object set date , On the acquisition date */
    public void testsetTimeAndGetTime(){
        Calendar calendar=Calendar.getInstance();
        Date date=new Date();
        calendar.setTime(date);
        date=calendar.getTime();
    }

Official account , Get a learning video

版权声明
本文为[Monkeys learn to program]所创,转载请带上原文链接,感谢

  1. [front end -- JavaScript] knowledge point (IV) -- memory leakage in the project (I)
  2. This mechanism in JS
  3. Vue 3.0 source code learning 1 --- rendering process of components
  4. Learning the realization of canvas and simple drawing
  5. gin里获取http请求过来的参数
  6. vue3的新特性
  7. Get the parameters from HTTP request in gin
  8. New features of vue3
  9. vue-cli 引入腾讯地图(最新 api,rocketmq原理面试
  10. Vue 学习笔记(3,免费Java高级工程师学习资源
  11. Vue 学习笔记(2,Java编程视频教程
  12. Vue cli introduces Tencent maps (the latest API, rocketmq)
  13. Vue learning notes (3, free Java senior engineer learning resources)
  14. Vue learning notes (2, Java programming video tutorial)
  15. 【Vue】—props属性
  16. 【Vue】—创建组件
  17. [Vue] - props attribute
  18. [Vue] - create component
  19. 浅谈vue响应式原理及发布订阅模式和观察者模式
  20. On Vue responsive principle, publish subscribe mode and observer mode
  21. 浅谈vue响应式原理及发布订阅模式和观察者模式
  22. On Vue responsive principle, publish subscribe mode and observer mode
  23. Xiaobai can understand it. It only takes 4 steps to solve the problem of Vue keep alive cache component
  24. Publish, subscribe and observer of design patterns
  25. Summary of common content added in ES6 + (II)
  26. No.8 Vue element admin learning (III) vuex learning and login method analysis
  27. Write a mini webpack project construction tool
  28. Shopping cart (front-end static page preparation)
  29. Introduction to the fluent platform
  30. Webpack5 cache
  31. The difference between drop-down box select option and datalist
  32. CSS review (III)
  33. Node.js学习笔记【七】
  34. Node.js learning notes [VII]
  35. Vue Router根据后台数据加载不同的组件(思考-&gt;实现-&gt;不止于实现)
  36. Vue router loads different components according to background data (thinking - & gt; Implementation - & gt; (more than implementation)
  37. 【JQuery框架,Java编程教程视频下载
  38. [jQuery framework, Java programming tutorial video download
  39. Vue Router根据后台数据加载不同的组件(思考-&gt;实现-&gt;不止于实现)
  40. Vue router loads different components according to background data (thinking - & gt; Implementation - & gt; (more than implementation)
  41. 【Vue,阿里P8大佬亲自教你
  42. 【Vue基础知识总结 5,字节跳动算法工程师面试经验
  43. [Vue, Ali P8 teaches you personally
  44. [Vue basic knowledge summary 5. Interview experience of byte beating Algorithm Engineer
  45. 【问题记录】- 谷歌浏览器 Html生成PDF
  46. [problem record] - PDF generated by Google browser HTML
  47. 【问题记录】- 谷歌浏览器 Html生成PDF
  48. [problem record] - PDF generated by Google browser HTML
  49. 【JavaScript】查漏补缺 —数组中reduce()方法
  50. [JavaScript] leak checking and defect filling - reduce() method in array
  51. 【重识 HTML (3),350道Java面试真题分享
  52. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  53. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  54. [re recognize HTML (3) and share 350 real Java interview questions
  55. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  56. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  57. 【重识 HTML ,nginx面试题阿里
  58. 【重识 HTML (4),ELK原来这么简单
  59. [re recognize HTML, nginx interview questions]
  60. [re recognize HTML (4). Elk is so simple