Date Class (emphasis)
Development, will often meet the situation of time display, so must be proficient in the date of the application
<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" ><strong>import java.util.*;p Ublic class Main {public static void main (string[] args) {Long L = System.currenttim Emillis ();//1414079892981system.out.println (L);D ate date = new Date ();//Current Time System.out.println (Date);D ate Date2 = new Date (1414079892981l);//encapsulates the millisecond value into an object System.out.println (Date2);}} </strong></span>
A conversion problem between a Date object and a millisecond value.
Millisecond value, Date object: 1.new date (Timemillis); 2.setTime ()
It is possible to manipulate a year, month, and day by means of a Date object
Date object, millisecond value: GetTime ();
Therefore: can be calculated by the specific numerical value, two date objects can not be reduced, into milliseconds to reduce, get a few days apart
Boolean after(Date when)
Tests whether this date is after the specified date.
Boolean before(Date when)
Tests whether this date is before the specified date.
Object clone()
Returns a copy of this object.
int compareTo(Date anotherDate)
compares the order of two dates.
Boolean equals(Object obj)
Compares the equality of two dates.
Turns the date object into 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 into a date formatted string date date = new Date ();//Gets the date object with the default style DateFormat DateFormat = Date Format.getdateinstance ();//Get date factory string str_date = Dateformat.format (date); System.out.println (str_date);//2014-10-24dateformat dateFormat2 = Dateformat.getdatetimeinstance ();// Get date Add Time factory string str_date2 = Dateformat2.format (date); System.out.println (str_date2)//2014-10-24 0:30:22//specified format full:2014 year October 24 Friday long:2014 year October 24 short : 14-10-24dateformat DATEFORMAT3 = dateformat.getdateinstance (dateformat.full);//Get date factory string str_date3 = Dateformat3.format (date); System.out.println (str_date3);//October 24, 2014 Friday 12:39 A.M. 27 seconds 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, where SimpleDateFormat is the subclass of DateFormat 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 string to date object uses the Parse method public static void Methoddemo () throws parseexception{//default style string str_date = "2014-10-24";D Ateformat DateFormat = dateformat.getdateinstance ();D ate Date = Dateformat.parse (str_date);//Throw Exception System.out.println ( date);//Custom style string str_date2 = "October 24, 2014";D ateformat DATEFORMAT2 = dateformat.getdateinstance (dateformat.long);D Ate date2 = Dateformat2.parse (STR_DATE2);//Throw abnormal System.out.println (date2); String Str_date3 = "2014--October--24th";D Ateformat dateFormat3 = dateformat.getdateinstance (Dateformat.long); DATEFORMAT3 = new SimpleDateFormat ("YYYY year--MM month--DD Day");D ate date3 = Dateformat3.parse (str_date3);// Throw exception System.out.println (Date3);}} </strong></span>
Date class Exercises
How many days are there from 2014-10-24 to 2012-12-25 days?
1. Turning a string into a Date object
2. Turn the date object into milliseconds
3. Subtract 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";D ate date1 = new Date ();D ate date2 = new Date ();D Ateformat dateFormat1 = Dateformat.getdateinst ance ();d ateFormat1 = new SimpleDateFormat ("Yyyy-mm-dd");d ate1 = Dateformat1.parse (str1);d ate2 = 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 CA = calendar.getinstance (); int = Ca.get (calendar.year); int mon = Ca.get (ca Lendar. MONTH) +1;//Note to +1,0 for January int day = Ca.get (calendar.day_of_month); int week = Ca.get (Calendar.day_of_week);//Note Foreign and we are not the same// 2014-10-24-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];}}
Practice:
Import Java.util.calendar;public class Main {public static void main (string[] args) {int year = 2014;showyear;//Judge A The February of the year has a few days//showday ();} private static void Showyear (int year) {Calendar CA = calendar.getinstance (); Ca.set (year,2,1);//Set March 1 time Ca.add ( Calendar.day_of_month,-1); the day before//3 month Methoddemo (CA);} private static void Showday () {Calendar CA = calendar.getinstance (); Ca.set (2015,10,25);//Set 2015.11.25 time Ca.add ( Calendar.day_of_month,-1);//Time Offset-"2013MethodDemo (CA);//2015-10-24-Tuesday Ca.add (Calendar.day_of_month, 10);// Over will automatically enter a month 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", "Friday", "Saturday"};return Week[i];}}
Java Learning Lesson 46th-Other Object APIs (ii) Date Class & Calendar class (Key Mastery)