Java Learning notes-date processing

Source: Internet
Author: User

Get system Current time using: Java.util.Date class, and this date is constructed as follows:

Non-parametric construction: public Date ()

Constructive: public date (long date)

First Instance:

import java.util.*;  Public class Datedemo {    publicstaticvoid  main (string[] args) {        //  TODO auto-generated method stub        date da=new  date ();        System.out.println (DA);    }}
View Code

However, the above output format is not easy to read.

Long can save file size and datetime data, and this attribute is well represented on the date class.

Construction Method: Public date (long date)

Change date to Long:public long GetTime ()

import java.util.*;  Public class Datedemo {    publicstaticvoid  main (string[] args) {        //  TODO auto-generated method stub        date da=new  date (System.currenttimemillis ());        System.out.println (DA);        System.out.println (Da.gettime ());}    }
View Code

To convert a date or data to a string, the class is available in Java: TheJava.text.simpleDateFormat class .

In SimpleDateFormat, the following methods are used mainly:

Construction method: Public SimpleDateFormat (String pattern)//PATTERN:YYYYMMDD

Format date as string:public final String format (date date)

format string as Date:public Date Parse (string source)throws ParseException

When instantiating a simpledateformat, you need to pass in some datetime tags, and all datetime tags are defined in the SimpleDateFormat class:

Year (yyyy) month (mm) (dd) (HH) minutes (mm) seconds (ss) milliseconds (SSS)

Example: Converting a date into a string

ImportJava.text.SimpleDateFormat;ImportJava.util.*; Public classDatedemo { Public Static voidMain (string[] args) {//TODO auto-generated Method StubDate da=NewDate (System.currenttimemillis ()); SimpleDateFormat SDF=NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss:SSS"); String Str=Sdf.format (DA);    System.out.println (str); }}
View CodeExample: Converting a string to a date
Importjava.text.ParseException;ImportJava.text.SimpleDateFormat;ImportJava.util.*; Public classDatedemo { Public Static voidMain (string[] args)throwsparseexception {simpledateformat sdf=NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss:SSS"); String Str= "2015-06-22 01:30:08:320"; Date da=sdf.parse (str);    System.out.println (DA); }}
View Code

Summarize conversions for various data types: Basic data Type wrapper class String Date

    • Basic data Type Packaging class: Automatic boxing and automatic unpacking
    • Basic data type->string: Using the valueof () method
    • The construction method of Long->date:date class
    • Date->long:gettime () method
    • String-> Basic Data type: wrapper class
    • String<->date:java.text.simpledateformat format () parse ()

Java Learning notes-date processing

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: info-contact@alibabacloud.com 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.