Java gets the current date and time

Source: Internet
Author: User
Tags date now dateformat days in month time and date

Original address: http://www.blogjava.net/parable-myth/archive/2013/01/17/394364.html

There are three ways to do this:

Method one: With the Java.util.Date class to achieve, and combined with the Java.text.DateFormat class to achieve the format of time, see the following code:

Import java.util.*;
Import java.text.*;
//The following default time and date display is in Chinese language mode
//General language default Chinese is available, the format of the time date is medium style, such as: 2008-6-16 20:54:53
// The dates and times shown below are based on the date class, and can be implemented using the Calendar 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) in 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 ();//use short style to display the date and time
String STR4 = D4.format (now);

DateFormat d5 = Dateformat.getdatetimeinstance (dateformat.full,dateformat.full); Display date, week, time (accurate to seconds)
String STR5 = D5.format (now);
DateFormat d6 = dateformat.getdatetimeinstance (Dateformat.long,dateformat.long); Displays the date. Time (accurate to seconds)
String STR6 = D6.format (now);
DateFormat D7 = dateformat.getdatetimeinstance (Dateformat.short,dateformat.short); Display date, time (accurate to minute)
String STR7 = D7.format (now);
DateFormat D8 = Dateformat.getdatetimeinstance (Dateformat.medium,dateformat.medium); Display date, time (accurate to minute)
String str8 = D8.format (now);//compared with short style, this method is the most useful



System.out.println ("Show Time in Date:" + now);//The result shown by this method is the same as Calendar.getinstance (). GetTime ().


SYSTEM.OUT.PRINTLN ("formatted with Dateformat.getdateinstance () after the time:" + str1);
SYSTEM.OUT.PRINTLN ("formatted with Dateformat.getdatetimeinstance () after the time:" + str2);
SYSTEM.OUT.PRINTLN ("formatted with Dateformat.gettimeinstance () after the time:" + STR3);
SYSTEM.OUT.PRINTLN ("formatted with Dateformat.getinstance () after the time:" + STR4);

SYSTEM.OUT.PRINTLN ("Formatted time with Dateformat.getdatetimeinstance (dateformat.full,dateformat.full):" + STR5);
SYSTEM.OUT.PRINTLN ("Formatted time with Dateformat.getdatetimeinstance (Dateformat.long,dateformat.long):" + STR6);
SYSTEM.OUT.PRINTLN ("Formatted time with Dateformat.getdatetimeinstance (Dateformat.short,dateformat.short):" + STR7);
SYSTEM.OUT.PRINTLN ("Formatted time with Dateformat.getdatetimeinstance (Dateformat.medium,dateformat.medium):" + str8);
}

}

Operation Result:

Display time 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 formatting the time with Dateformat.gettimeinstance (): 20:54:53
After formatting the time with Dateformat.getinstance (): 08-6-16 8:54
Format time with Dateformat.getdatetimeinstance (dateformat.full,dateformat.full): June 16, 2008 Monday 08:54 P.M. 53 sec CST
Format time with Dateformat.getdatetimeinstance (Dateformat.long,dateformat.long): June 16, 2008 08:54 P.M. 53 seconds
After formatting the time with Dateformat.getdatetimeinstance (dateformat.short,dateformat.short): 08-6-16 8:54
Formatted time with Dateformat.getdatetimeinstance (dateformat.medium,dateformat.medium): 2008-6-16 20:54:53

Method Two: Use the Java.util.Calendar class to achieve, see below:

Import java.util.*;
Import java.text.*;
The following is the use of the Calendar class to implement date time, 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);//Acquisition 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 ("with Calendar.getinstance (). GetTime () mode display time:" + ca.gettime ());
System.out.println ("Get Date with calendar:" + 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 day of the week (I do this example is exactly Tuesday, so the results show 2, if you run again 6 weeks, then show 6)

}

}

Display time by Calendar.getinstance (). GetTime (): Mon June 21:54:21 CST 2008
Use calendar to get the date: May 16, 2008 Note: This is May, and actually June, because the calendar month is 0~11 corresponds to 1-12 months.
Use Calendar to get time is: 9:54 21 seconds Note: Here is a 12-hour system, to obtain 24-hour system, you need to use inthour=ca.get (calendar.hour_of_day);//hour

Method Three:

SimpleDateFormat 24-hour system time display

Letters date or time element represents Example
G Era Marker Text AD
y Years Year 1996;96
M Month of the Year Month July; Jul;07
w Number of weeks in the year Number 27
W Number of weeks in the month Number 2
D Number of days in the year Number 189
d Number of days in month Number 10
F Week of the Month Number 2
E Days of the Week Text Tuesday;Tue
a AM/PM Mark Text PM
H Number of hours in the day (0-23) Number 0
k Number of hours in the day (1-24) Number 24
K Number of hours in am/pm (0-11) Number 0
h Number of hours in am/pm (1-12) Number 12
m Number of minutes in hours Number 30
s Number of seconds in minutes Number 55
S Number of milliseconds Number 978
z Time General Time Zone Pacific Standard Time; PST;GMT-08:00
Z Time RFC 822 Time Zone

-0800

It has the advantage of supporting two-bit months, two-bit dates. Prefixed with 0. Date date = new Date (); SimpleDateFormat DateFormat = new SimpleDateFormat ("Yyyy-mm-dd hh:mm"); String Sdatesuffix = Dateformat.format (date); System.out.println ("[+]sdatesuffix:" +sdatesuffix);

Java gets the current date and time

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.