JAVA 46th-Other Object API (2) Date Class & Calendar class (important), apidate
Date class (important)
During development, time is often displayed, so you must be familiar with the Date application.
<Span style = "font-family: KaiTi_GB2312; font-size: 18px;"> <strong> 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) ;}</strong> </span>
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.
<Span style = "font-family: KaiTi_GB2312; font-size: 18px;"> <strong> 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) ;}</strong> </span>
String to date object
<Span style = "font-family: KaiTi_GB2312; font-size: 18px;"> <strong> 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) ;}}</strong> </span>
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];}
I learned the basic knowledge of Java SE (I didn't know the API). Now I want to learn javaME. Do I need to learn the API in javaSE?
Yes...
P.s
Other syntaxes can be ignored.
The code that can understand se can understand me code. Because the syntax is the same, it is only different in some api functions. There is no direct connection between other functions.
Some basic operations are the same. for example, io is similar. it is not as rich as the objects and methods in se. the String object is similar. and Date .. the difference lies in the differences between functions. in short, I will look for an api manual. what method does it use to check it slowly .. people who are familiar with it won't be able to get started for three or two days. are slowly accumulating and accumulating.
How to Learn java
In what direction? Website or android?
In common, you must first learn j2se,
Then j2ee -- database -- tomcat, jsp -- servlet + javabean -- html, css, javascript, ajax -- upload -- struts2 -- hibernate3 -- spring3 -- ejb3.0 -- server optimization (cluster, cloud, server Load balancer)
In addition, we need at least two years of work accumulation.
If you don't even learn Programming
Then add c/vb, data structure, operating system, network ,,,,,