Date processing in Java

Source: Internet
Author: User

One, the date and the conversion of long type data

 Public class hello{    publicstaticvoidthrows  Exception    {         // date converted to long        New Date ();         long Currentdateinlong = currentdateindate.gettime ();        System.out.println (currentdateindate);        System.out.println (Currentdateinlong);     } }

Two, a long type is converted to a date

 Public class hello{    publicstaticvoidthrows  Exception    {         // date converted to long        long Currentdateinlong = system.currenttimemillis ();         New Date (currentdateinlong);        System.out.println (currentdateindate);        System.out.println (Currentdateinlong);     } }

Third, date formatting (format the date as a string)

 Public classhello{ Public Static voidMain (string[] args)throwsException {//date converted to long        LongCurrentdateinlong =System.currenttimemillis (); Date currentdateindate=NewDate (Currentdateinlong); SimpleDateFormat SDF=NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss. SSSS "); String currentdateinstring=Sdf.format (currentdateindate);    System.out.print (currentdateinstring); } }

Iv. parsing of dates (converting strings to dates)

 Public class hello{    publicstaticvoidthrows  Exception    {        = " 2016-08-07 19:24:03.0528 ";         New SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss. SSSS ");         = Sdf.parse (currentdateinstring);        System.out.print (currentdateindate);    } }

V. Using the Calendar class to get the parts of a date

 Public class hello{    publicstaticvoidthrows  Exception    {        = calendar.getinstance ();        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));}    }

Date processing in Java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.