Java Time related content: Date, SimpleDateFormat, and timestamp

Source: Internet
Author: User
Tags date1 locale month name

Date api:https://docs.oracle.com/javase/8/docs/api/java/util/date.html SimpleDateFormat api:https:// docs.oracle.com/javase/7/docs/api/java/text/simpledateformat.html DateFormatSymbols api:https://docs.oracle.com/ Javase/8/docs/api/java/text/dateformatsymbols.html


1. Get timestamp based on date class object

Set Date object to date, Date.gettime () to obtain timestamp;

The API is as follows:

Examples are as follows:

public static void Main (string[] args) throws ParseException {
		System.out.println ("timestamps =" + new Date (). GetTime ());
	}

The output is as follows:


2. Date class object obtained by timestamp

The construction method of the date class can be constructed directly as a timestamp parameter,

The API is as follows:


Examples are as follows:

	public static void Main (string[] args) throws ParseException {
		long timestamp = System.currenttimemillis ();
		System.out.println ("timestamp =" + timestamp);
		System.out.println (new Date (timestamp));
	}

The output is as follows:


3. Gets the date class variable of the specified time date

Strdate the specified date through the string type, and then resolves the date type variable by the SimpleDateFormat parse method. The code is as follows:

public static void Main (string[] args) throws ParseException {
		String strdate = "2018-01-01 00:00:00.000";
		String pat1 = "Yyyy-mm-dd HH:mm:ss. SSS ";
		SimpleDateFormat SDF = new SimpleDateFormat (PAT1);
		Date date = Sdf.parse (strdate);
		SYSTEM.OUT.PRINTLN (date);
	
The output is as follows:


4. String type time of the specified format obtained by the date type variable

With SimpleDateFormat, you can tell the date type variable date to the string type in the specified format.

public static void Main (string[] args) throws ParseException {
		Date date = new Date ();
		String pat1 = "Yyyy-mm-dd HH:mm:ss. SSS ";  Keep all data
		String pat2 = "Mm-dd hh:mm";  Only keep date and time
		simpledateformat sdf1 = new SimpleDateFormat (PAT1);
		SimpleDateFormat sdf2 = new SimpleDateFormat (PAT2);
		SYSTEM.OUT.PRINTLN ("Raw date format:" + date.tostring ());
		System.out.println ("PATTERN1:" + sdf1.format (date));
		System.out.println ("pattern2:" + sdf2.format (date));
	}

The output is as follows:


SimpleDateFormat API Reference First ref, date format Template example is as follows:



5. How to Obtain Date Object 1 by string class strdate = Date.tostring () such as ("Mon Apr 10:32:03 CST 2018") method1 (deprecated): date Class is constructed by date = new Date (strdate)


Examples are as follows:

	public static void Main (string[] args) throws ParseException {
		Date date1 = new Date ();
		String strdate = date1.tostring ();
		System.out.println ("Strdate is:" + strdate);
		Date Date2 = new Date (strdate);
		System.out.println ("Constructed date2 is:" + date2.tostring ());
	}
The output is as follows:

2) Method2: SimpleDateFormat parse strdate Get Date Object Date


public static void Main (string[] args) throws ParseException {
		Date date1 = new Date ();
		String strdate = date1.tostring ();
		SYSTEM.OUT.PRINTLN ("raw strdate =" + strdate);
		String Pat = "E MMM dd HH:mm:ss zzz yyyy";
		Set the locale for SDF to 中文版, because by default it is the Chinese language environment
		simpledateformat SDF = new SimpleDateFormat (PAT, locale.english);
		Parse and get date2
		Date date2 = Sdf.parse (strdate);
		System.out.println ("after parsing, date2 =" + Date2);
	}

The output is as follows:

3) Supplementary notes:

If you do not specify locale as 中文版 when constructing SimpleDateFormat, the default locale is the local environment Chinese, at which point the SDF resolution obtains Chinese characters, as follows:

public static void Main (string[] args) throws ParseException {
		Date date1 = new Date ();
		String strdate = date1.tostring ();
		SYSTEM.OUT.PRINTLN ("raw strdate =" + strdate);
		String Pat = "E MMM dd HH:mm:ss zzz yyyy";
		SimpleDateFormat SDF = new SimpleDateFormat (PAT);
		With default Locale--chinese, and default DateFormatSymbols
		System.out.println (Sdf.format (date1));
	
The output is as follows:



6. DateFormatSymbols class explanation

DateFormatSymbols is a public class that encapsulates the localizable date-time formatting data, such as the month name, the name of the day of the week, and time zone data. Both DateFormat and SimpleDateFormat use DateFormatSymbols to encapsulate this information.

	public static void Main (string[] args) throws IOException {
		//default locale package "Month"
		DateFormatSymbols = new DateFormatSymbols ();
		string[] Monthsdefault = Dfsdefault.getmonths ();
		System.out.println (arrays.tostring (Monthsdefault));
		Locale is the eng of the package "month"
		dateformatsymbols Dfseng = new DateFormatSymbols (locale.english);
		string[] Monthseng = Dfseng.getmonths ();
		System.out.println (arrays.tostring (Monthseng));

	}

Output:



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.