A summary of some methods of date in Java, DateFormat

Source: Internet
Author: User
Tags dateformat locale

1:datedemo1 class

 * * Time to get the
 system
/public class DateDemo1 {public
	static void Main (String args[]) {
		System.out.println (System.currenttimemillis ());
		
	}

Output as: 1311060393171

The results show the number of milliseconds elapsed from January 1, 1970 to the time the system was taken, such as the number 1115346430703, but no one knows exactly what it means, and you can use the date class to make the number more meaningful.


2:datedemo2 class

public class DateDemo2 {@SuppressWarnings (' deprecation ') public static void Main (String args[]) {Date date = new Dat
		
		E ();
		System.out.println ("date.tostring:" + date.tostring ());
		System.out.println ("Date:" + date.getdate ());
		System.out.println ("Day:" + date.getday ());
		System.out.println ("Hour:" + date.gethours ());
		System.out.println ("minutes:" + date.getminutes ());
		System.out.println ("Month:" + (Date.getmonth () + 1));
		System.out.println ("seconds:" + date.getseconds ());
		System.out.println ("Time:" + date.gettime ());
		System.out.println ("TimeZone:" + date.gettimezoneoffset ());
		
		System.out.println ("Year:" + (Date.getyear () + 1900));
		Calendar calendar = Calendar.getinstance ();
		int year = Calendar.get (calendar.year);
		int month = Calendar.get (calendar.month) + 1;
		int day = Calendar.get (calendar.date);
		int hour = Calendar.get (Calendar.hour);
		int minute = Calendar.get (Calendar.minute);
		
		int second = Calendar.get (Calendar.second); System.ouT.println ("----------Calendar-------");
		System.out.println ("Year:" + year);
		System.out.println ("Month:" + month);
		System.out.println ("Day:" + day);
		System.out.println ("Hour:" + hour);
		System.out.println ("minute:" + minute);
		
	System.out.println ("Second:" + second); }
}
Get the time value with date, such as time, minute, second, month, day, year method is obsolete, you need to use the Calendar method to get the value of time

The output results are:

Date.toString:Tue June 15:32:42 CST
date:19
day:2
hour:15
minutes:32 month:7 seconds :
time:1311060762362
timezone: -480
year:2011
----------Calendar-------
year:2011
Month:7
day:19
hour:3
minute:32
second:42

3:datedemo3 class

public class DateDemo3 {public
	static void Main (String args[]) {
		Date date = new Date ();
		 * * DateFormat will be based on the area of the computer to display the time format, EE said the week, MM for the month, DD for the date, and YYYY is A.D., each character of the set has its own meaning
		 * from the date-->string * *
		DateFormat DateFormat = new SimpleDateFormat ("ee-mm-dd-yyyy");
		System.out.println (Dateformat.format (date));
		
		DateFormat format1 = new SimpleDateFormat ("Yyyy-mm-dd");
		System.out.println (Format1.format (date));
		
		DateFormat format2 = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
		System.out.println (Format2.format (date));
		 * * from string-->date
		/String datestr = "2011-7-19";
		try {
			Date Datetrans = Format1.parse (DATESTR);
			System.out.println (Datetrans.tostring ()); 
			System.out.println (Datetrans.tolocalestring ());  Convert to local form
		} catch (ParseException e) {
			e.printstacktrace ();
		}
}}
Use DateFormat to format date data, which is from date-->string, and the following sections are from String-->date

The output results are:

Tuesday -07-19-2011
2011-07-19
2011-07-19 15:33:27
2011-7-19 0:00:00

4:datedemo4 class

public class DateDemo4 {public

	static void Main (string[] args) {
		
		date date = new Date ();
		DateFormat Shortformat = dateformat.getdatetimeinstance (Dateformat.short, dateformat.short);
		DateFormat Mediumformat = dateformat.getdatetimeinstance (Dateformat.medium, dateformat.medium);
		DateFormat Longformat = dateformat.getdatetimeinstance (Dateformat.long, dateformat.long);
		DateFormat Fullformat = dateformat.getdatetimeinstance (Dateformat.full, dateformat.full);
		
		System.out.println (Shortformat.format (date));
		System.out.println (Mediumformat.format (date));
		System.out.println (Longformat.format (date));
		System.out.println (Fullformat.format (date));
		
	}


The output results are:

11-7-19 afternoon 3:33
2011-7-19 15:33:54
July 19, 2011 03:33 P.M. 54 sec
July 19, 2011 Tuesday 03:33 P.M. 54 sec CST

5:datedemo5 class

public class DateDemo5 {public
	static void Main (String args[]) {
		Date date = new Date ();
		
		Locale Locale = new Locale ("en", "US");
		
		DateFormat Shortdateformat = dateformat.getdateinstance (dateformat.short, locale);
		DateFormat Mediumdateformat = dateformat.getdateinstance (dateformat.medium, locale);
		DateFormat Longdateformat = dateformat.getdateinstance (Dateformat.long, locale);
		DateFormat Fulldateformat = dateformat.getdateinstance (dateformat.full, locale);
		
		System.out.println (Shortdateformat.format (date));
		System.out.println (Mediumdateformat.format (date));
		System.out.println (Longdateformat.format (date));
		System.out.println (Fulldateformat.format (date));
		
	}

The output results are:

7/19/11 June
July,
Tuesday, July 19, 2011



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.