编程知识 cdmana.com

Introduction to Java lesson 96 - 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]所创,转载请带上原文链接,感谢

Scroll to Top