编程知识 cdmana.com

Getting started with Java lesson 98 -- formatting dates using the format method

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. !

problem

     As you can see from the previous case , Output date - The default format of time display is "Tue Jan 14 18:02:38 CST 2014", This output format is not in line with the way we usually look at time , Our daily time format is "2014 year 1 month 13 Japan 18:02:38" Or for "2014-01-13 18:02:38".

     In this case, the current time and date are required to be set as "2014-01-13 18:02:38" This format is output to the console .

programme

    1) Instantiation SimpleDateFormat Class object , This class enables users to select a custom date - Time format mode , The code is as follows :

SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

     In the above code , Set date - The time format is "yyyy-MM-dd HH:mm:ss".

    2) First , Instantiation Date Class object , Get current date - Time ; then , call SimpleDateFormat Class format Method , take Date The object of the class is formatted as a date - Time string , The code is as follows :

    Date date=new Date();
    String dateStr=sdf.format(date);

step

     To implement this case, follow the steps below .

Step one : New classes and test methods

     First, create a new class named TestSimpleDateFormat Class , And create a new test method in this class testFormat, The code is as follows :

    package day03;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import org.junit.Test;
    
    public class TestSimpleDateFormat{
        /**
         * test format Method 
         */
         @Test
         public void testFormat(){
         }
    }

Step two : Set a custom date - Time format

     Instantiation SimpleDateFormat Class object , This class enables users to select a custom date - Time format mode , The code is as follows :

    package day03;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import org.junit.Test;
    
    public class TestSimpleDateFormat{
        /**
         * test format Method 
         */
         @Test
         public void testFormat(){
             SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
         }
    }

Step three : Use format Methods will Date The object of the class is formatted as a date - Time string

     First , Instantiation Date Class object , Get current date - Time ; then , call SimpleDateFormat Class format Method , take Date The object of the class is formatted as a date - Time string , The code is as follows :

    package day03;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import org.junit.Test;
    
    public class TestSimpleDateFormat{
        /**
         * test format Method 
         */
         @Test
         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);
         }
    }

Step four : function

     function testFormat Method , The running results are as follows :

    2014-01-15 16:03:41

     Observe the output , You can see that the current date time has been converted to "yyyy-MM-dd HH:mm:ss" Format .

Official account , Get a learning video

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

Scroll to Top