How does Java get the current time and how do I format it?

Source: Internet
Author: User

Cause: When writing code, it is often used to get the current timestamp and date, as summarized below

public void TestDate () {
SimpleDateFormat df = new SimpleDateFormat ("Yyyy-mm-dd");//Set Date format

Date date = new Date ();
String datestring = date.tostring ();
Long times = Date.gettime ();
System.out.println ("date.tostring ():" +date.tostring ());
System.out.println ("current timestamp (milliseconds):" + times);

Calendar C = calendar.getinstance ();
System.out.println ("Current date:" + c.get (calendar.date));
SYSTEM.OUT.PRINTLN ("Current year:" + C.get (calendar.year));
Month starting from 0, 0-11
System.out.println ("Current Month:" + c.get (calendar.month));
12-hour system, 0-11
System.out.println ("12 Hour System: Current hour:" + c.get (calendar.hour));
System.out.println ("24 hour System: Current hour:" + c.get (calendar.hour_of_day));
System.out.println ("Current minute:" + c.get (Calendar.minute));
System.out.println ("Current second:" + c.get (Calendar.second));
System.out.println ("One Day of the year:" + C.get (calendar.day_of_year));
System.out.println ("current timestamp (milliseconds):" + C.gettimeinmillis ());
System.out.println ("The ToString () method of the calendar is generally used as debugging:" +c.tostring ());
SimpleDateFormat df = new SimpleDateFormat ("YyyyMMdd");
String currentdatestr = Df.format (New Date ());
System.out.println ("Current date yyyymmdd:" +currentdatestr);

}


  

Output Result:

Date.tostring (): Wed 11:39:05 CST 2017
Current timestamp (milliseconds): 1501645145298
Current Date: 2
Current year: 2017
Current Month: 7
12-hour System: Current hour: 11
24-hour System: Current hour: 11
Current minute: 39
Current seconds: 5
Day of the year: 214
Current timestamp (milliseconds): 1501645145302
The ToString () method of the calendar is generally used as debugging: java.util.gregoriancalendar[time=1501645145302,arefieldsset=true,areallfieldsset= True,lenient=true,zone=sun.util.calendar.zoneinfo[id= "Asia/shanghai", offset=28800000,dstsavings=0,usedaylight= False,transitions=19,lastrule=null],firstdayofweek=1,minimaldaysinfirstweek=1,era=1,year=2017,month=7,week_of_ Year=31,week_of_month=1,day_of_month=2,day_of_year=214,day_of_week=4,day_of_week_in_month=1,am_pm=0,hour=11, Hour_of_day=11,minute=39,second=5,millisecond=302,zone_offset=28800000,dst_offset=0]
Current date yyyymmdd:20170802

How does Java get the current time and how do I format it?

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