Java gets the current date and time two ways to share _java

Source: Internet
Author: User
Tags date now dateformat
There are two ways of doing this:
Method One: Use Java.util.Date class to implement, and combine Java.text.DateFormat class to realize time format, look at the following code:
Copy Code code as follows:

Import java.util.*;
Import java.text.*;
The following default time dates are displayed in Chinese language mode
The general language on the default Chinese can be, the date of the format defaults to medium style, such as: 2008-6-16 20:54:53
The date and time shown below are based on the date class, and you can also use the Calendar class to implement see Class Testdate2.java
public class TestDate {
public static void Main (string[] args) {
Date now = new Date ();
Calendar cal = Calendar.getinstance ();

DateFormat D1 = dateformat.getdateinstance (); Default style (medium style, for example: 2008-6-16 20:54:53), under Default language (Chinese)
String str1 = D1.format (now);
DateFormat D2 = Dateformat.getdatetimeinstance ();
String str2 = D2.format (now);
DateFormat d3 = Dateformat.gettimeinstance ();
String STR3 = D3.format (now);
DateFormat D4 = Dateformat.getinstance (); Display a date and time using the short style
String STR4 = D4.format (now);
DateFormat d5 = Dateformat.getdatetimeinstance (dateformat.full,dateformat.full); Show date, week, time (accurate to second)
String STR5 = D5.format (now);
DateFormat d6 = dateformat.getdatetimeinstance (Dateformat.long,dateformat.long); Displays the date. Time (accurate to second)
String STR6 = D6.format (now);
DateFormat D7 = dateformat.getdatetimeinstance (Dateformat.short,dateformat.short); Show date, time (exact to minute)
String STR7 = D7.format (now);
DateFormat D8 = Dateformat.getdatetimeinstance (Dateformat.medium,dateformat.medium); Show date, time (exact to minute)
String str8 = D8.format (now);//It works best when compared to the short style
System.out.println ("Show time in Date: + Now")//This method displays the results and calendar.getinstance (). GetTime ()
System.out.println ("with Dateformat.getdateinstance () after the format of the time is:" + str1);
System.out.println ("with Dateformat.getdatetimeinstance () after the format of the time is:" + str2);
System.out.println ("with Dateformat.gettimeinstance () after the format of the time is:" + STR3);
System.out.println ("with Dateformat.getinstance () after the format of the time is:" + STR4);

System.out.println ("Dateformat.getdatetimeinstance (dateformat.full,dateformat.full) after the format of the time is:" + STR5);
System.out.println ("Dateformat.getdatetimeinstance (Dateformat.long,dateformat.long) after the format of the time is:" + STR6);
System.out.println ("Dateformat.getdatetimeinstance (Dateformat.short,dateformat.short) after the format of the time is:" + STR7);
System.out.println ("Dateformat.getdatetimeinstance (Dateformat.medium,dateformat.medium) after the format of the time is:" + str8);
}
}

Run Result:
Copy Code code as follows:

Time displayed in Date: Mon June 20:54:53 CST 2008
Format time with Dateformat.getdateinstance (): 2008-6-16
Format time with Dateformat.getdatetimeinstance (): 2008-6-16 20:54:53
After the dateformat.gettimeinstance () format time: 20:54:53
After the dateformat.getinstance () format time: 08-6-16 8:54
After the dateformat.getdatetimeinstance (dateformat.full,dateformat.full) format time: June 16, 2008 Monday 08:54 P.M. 53 sec CST
After the dateformat.getdatetimeinstance (dateformat.long,dateformat.long) format time: June 16, 2008 08:54 P.M. 53 seconds
After the dateformat.getdatetimeinstance (dateformat.short,dateformat.short) format time is: 08-6-16 8:54
After the dateformat.getdatetimeinstance (dateformat.medium,dateformat.medium) format time: 2008-6-16 20:54:53

Method Two: Use Java.util.Calendar class to realize, look below:
Copy Code code as follows:

Import java.util.*;
Import java.text.*;
The following is the use of the Calendar class to implement DateTime, and the date class is relatively simple
public class TestDate2 {
public static void Main (string[] args) {

Calendar CA = Calendar.getinstance ();
int year = Ca.get (calendar.year);//Get Years
int Month=ca.get (calendar.month);//Get month
int Day=ca.get (calendar.date);//Get Day
int Minute=ca.get (calendar.minute);//min
int Hour=ca.get (calendar.hour);//hour
int Second=ca.get (calendar.second);//sec
int weekofyear = Ca.get (Calendar.day_of_week);

System.out.println ("Use Calendar.getinstance (). GetTime () mode to display the time:" + ca.gettime ());
System.out.println ("Calendar date is:" + year + "years" + month + "Month" + Day + "Days");

SYSTEM.OUT.PRINTLN ("Use calendar to get time is:" + hour + "when" + Minute + "min" + second + "seconds");
SYSTEM.OUT.PRINTLN (WeekOfYear)//show today is the first day of the week (I do this example is exactly Tuesday, so the results show 2, if you run the Week 6, then show 6)
}
}

The results of the operation are:
Copy Code code as follows:

Display time with Calendar.getinstance (). GetTime (): Mon June 21:54:21 CST 2008
Date in Calendar: May 16, 2008
Time to use calendar is: 9:54 21 seconds

Summary: In the case, method two is the most convenient, the method of a significant score clumsy, but to see a person like.
There is also a way to use
Copy Code code as follows:

System.currenttimemillis ()
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.