JAVA 46th-Other Object API (2) Date Class & amp; Calendar class (key points)

Source: Internet
Author: User
Tags date1 dateformat

JAVA 46th-Other Object API (2) Date Class & amp; Calendar class (key points)

Date class (important)

During development, time is often displayed, so you must be familiar with the Date application.

Import java. util. *; public class Main {public static void main (String [] args) {long l = System. currentTimeMillis (); // 1414079892981System. out. println (l); Date date = new Date (); // current time System. out. println (date); Date date2 = new Date (1414079892981l); // encapsulate the millisecond value into the object System. out. println (date2 );}}


Conversion between date objects and millisecond values.

Millisecond value-> Date object: 1.new Date (TimeMillis); 2. setTime ()

Therefore, you can use the Date object method to operate a certain year, month, or day.

Date object-> millisecond value: getTime ();

Therefore, you can perform an operation by using a specific number. The two date objects cannot be subtracted, but can be subtracted in milliseconds.


Boolean after(Date when)Test whether the specified date is later than the specified date.

Booleanbefore(Date when)Test whether the specified date is earlier than the specified date.

Object clone()Returns a copy of the object.

IntcompareTo(Date anotherDate)Compare the order of two dates.

Boolean equals(Object obj)Compare the equality of two dates.


Converts a date object to a string.

Import java. text. dateFormat; import java. text. simpleDateFormat; import java. util. date; public class Main {public static void main (String [] args) {MethodDemo ();} public static void MethodDemo () {// format the Date object and convert it to The date Format String Date = new Date (); // obtain the date object, with the default style DateFormat dateFormat = DateFormat. getDateInstance (); // obtain the date factory String str_Date = dateFormat. format (date); System. out. println (str_Date); // 2014-10-24DateFormat dateFormat2 = DateFormat. getDateTimeInstance (); // get the date plus time factory String str_Date2 = dateFormat2.format (date); System. out. println (str_Date2); // October 24, 2014 0:30:22 // The specified format is FULL: Friday, January 1, October 24, 2014 LONG: SHORT: 14-10-24DateFormat dateFormat3 = DateFormat. getDateInstance (dateFormat. FULL); // obtain the date factory String str_Date3 = dateFormat3.format (date); System. out. println (str_Date3); // Friday, January 1, October 24, 2014 12:39:27 A.M. DateFormat dateFormat4 = DateFormat. getDateTimeInstance (dateFormat. FULL, dateFormat. LONG); String str_Date4 = dateFormat4.format (date); System. out. println (str_Date4); // The Custom style uses the format method in the DateFormat class. SimpleDateFormat is the subclass of DateFormat dateFormat5 = new SimpleDateFormat ("yyyy # MM # dd "); string str_Date5 = dateFormat5.format (date); System. out. println (str_Date5 );}}

String to date object

Import java. text. dateFormat; import java. text. parseException; import java. text. simpleDateFormat; import java. util. date; public class Main {public static void main (String [] args) throws ParseException {MethodDemo ();} // The parse method public static void MethodDemo () throws ParseException is used for String-to-Date conversion. {// default style String str_date = "2014-10-24"; DateFormat dateFormat = DateFormat. getDateInstance (); Date date = dateFormat. parse (str_date); // throw the System exception. out. println (date); // custom style String str_date2 = "October 24, 2014"; DateFormat dateFormat2 = DateFormat. getDateInstance (DateFormat. LONG); Date date2 = dateFormat2.parse (str_date2); // throw an exception in System. out. println (date2); String str_date3 = "August 1, -- August 24"; DateFormat dateFormat3 = DateFormat. getDateInstance (DateFormat. LONG); dateFormat3 = new SimpleDateFormat ("yyyy-MM-dd"); Date date3 = dateFormat3.parse (str_date3); // throw an exception in System. out. println (date3 );}}


Date exercises

To how many days are there?

1. Convert string to Date object

2. Convert the Date object to milliseconds

3. Minus days

Import java. text. dateFormat; import java. text. parseException; import java. text. simpleDateFormat; import java. util. date; public class Main {public static void main (String [] args) throws ParseException {MethodDemo ();} public static void MethodDemo () throws ParseException {String str1 = "2014-10-24 "; string str2 = "2012-12-25"; Date date1 = new Date (); Date date2 = new Date (); DateFormat dateFormat1 = DateFormat. getDateInstance (); dateFormat1 = new SimpleDateFormat ("yyyy-MM-dd"); date1 = dateFormat1.parse (str1); date2 = dateFormat1.parse (str2); long time1 = date1.getTime (); long time2 = date2.getTime (); long time = Math. abs (time1-time2); int day = Getday (time); System. out. println (day + "day");} public static int Getday (long time) {return (int) (time/1000/60/60/24 );}}

Calendar class

Public class Main {public static void main (String [] args) {MethodDemo ();} public static void MethodDemo () {Calendar ar ca = Calendar ar. getInstance (); int year = ca. get (Calendar. YEAR); int mon = ca. get (Calendar. MONTH) + 1; // note that + represents int day = ca. get (Calendar. DAY_OF_MONTH); int week = ca. get (Calendar. DAY_OF_WEEK); // note that foreign countries are different from ours. //-Friday System. out. println (year + "-" + mon + "-" + day + "-" + GetWeek (week);} private static String GetWeek (int I) {// TODO Auto-generated method stubString [] week = {"", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday ", "Friday", "Saturday"}; return week [I] ;}}

Exercise:

Import java. util. calendar; public class Main {public static void main (String [] args) {int year = 2014; showyear (year ); // determine the number of days in the month of a year. // showday ();} private static void showyear (int year) {Calendar ar ca = Calendar ar. getInstance (); ca. set (year,); // set the ca on January 1. add (Calendar. DAY_OF_MONTH,-1); // MethodDemo (ca);} private static void showday () {Calendar ca = Calendar. getInstance (); ca. set (, 25); // set the ca at 2015.11.25. add (Calendar. DAY_OF_MONTH,-1); // Time Offset-"2013 MethodDemo (ca); //-Tuesday ca. add (Calendar. DAY_OF_MONTH, 10); // if the value is exceeded, MethodDemo (ca);} public static void MethodDemo (Calendar ca) {int year = ca. get (Calendar. YEAR); int mon = ca. get (Calendar. MONTH) + 1; int day = ca. get (Calendar. DAY_OF_MONTH); int week = ca. get (Calendar. DAY_OF_WEEK); int hour = ca. get (Calendar. HOUR); int minute = ca. get (Calendar. MINUTE); int second = ca. get (Calendar. SECOND); System. out. print (year + "-" + mon + "-" + day + "-" + GetWeek (week) + "-" + hour + ":"); if (minute> 9) {System. out. println (minute + ":" + second);} else {System. out. println ("0" + minute + ":" + second) ;}} private static String GetWeek (int I) {String [] week = {"", "Sunday ", "Monday", "Tuesday", "Wednesday", "Thursday", "Saturday"}; return week [I];}





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.