A period of time to do a countdown function, using the Date,calendar,simpledateformat, the relationship between them blurred, so in this collation, but also strengthen their understanding of the relationship between them, but also convenient for the next use:
1, Time
New // initialization time (current time is system default) // get the time in milliseconds long times = Date.gettime (); // returns the number of milliseconds that this Date object represents since January 1, 1970 00:00:00 GMT. Date.settime (long times); // set the time to the specified long time New Date (times); // initialization time (the current time is the specified time)
2, the conversion between time date and string:
1 New SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); 2 // converts the time to the specified string format 3 String time = Simpledateformat.format (new Date ()); 4 // converts the specified string format to time 5 Date date = Simpledateformat.parse (time);
2, time acquisition and addition and subtraction:
Calendar Calendar =NewGregorianCalendar ();
Returns an object that represents this Calendar
time value, from the epoch to the current millisecond offset Date
.
Date date = Calendar.gettime ();
intYear = Calendar.get (calendar.year);//acquisition year; intmonth = Calendar.get (calendar.month);//get the month; intDate = Calendar.get (calendar.date);//get the day; inthour = Calendar.get (Calendar.hour);//get hours; intminute = Calendar.get (Calendar.minute);//get minutes; intSecond = Calendar.get (Calendar.second);//get seconds; intHour_of_day = Calendar.get (Calendar.hour_of_day);//the first few hours, intDay_of_month = Calendar.get (calendar.day_of_month);//The day, within one months is the first day. intDay_of_week = Calendar.get (Calendar.day_of_week);//this day, within a week, is the first day. intDay_of_year = Calendar.get (calendar.day_of_year);//The day, within a year, is the first day. intWeek_of_year = Calendar.get (calendar.week_of_year);//this week, within a year is the first week; intWeek_of_month = Calendar.get (calendar.week_of_month);//this week is the first week of the month, with the star as the standard; intZone_offset = Calendar.get (Calendar.zone_offset);//Get time zone; intDay_of_week_in_month = Calendar.get (calendar.day_of_week_in_month);//The first week of the month, according to the number 1th, 1th is the 1th week, 8th is the 2nd week. By month days as standard //Set Month 05, which represents the month of the calendar June, since the month starts at 0. Calendar.clear ();//Note: Before using the Set method, you must clear it, otherwise many of the information will inherit from the current time of the systemCalendar.set (Calendar.month, 05); //Set calendar month is June intmonths =Calendar.get (Calendar.month); SYSTEM.OUT.PRINTLN (months); //output 05; (actually June)//set the calendar date to 2011-07-24 09:59:50Calendar.set (2011, 06, 24, 9, 59, 50); String getDate=Date_format.format (Calendar.gettime ()); System.out.println (getDate); //output 2011-07-24 09:59:50; //sets the calendar to the current time, or any date value. Calendar.settime (NewDate ()); //Plus/minus (year/month day). All the same)Calendarnew.add (calendar.date, 1);//add a day to the current date (tomorrow)Calendarnew.add (Calendar.date,-1);//the current date is reduced by one day (yesterday)//Compare the size of the day before; //gettimeinmillis () returns the time value of this Calendar, in milliseconds, of type long. if(NewDate (). GetTime () >Calendar.gettimeinmillis ()) {System.out.println ("The current date is behind!"); }Else{System.out.println ("Current date in front!"); } //Create a Calendar to compare timeCalendar calendarnew =calendar.getinstance (); //set to 5 hours ago, the latter large, show-1Calendarnew.add (Calendar.hour, 5); System.out.println ("Time Comparison:" +Calendarnew.compareto (calendar)); //set 7 hours later, the former large, showing 1Calendarnew.add (Calendar.hour, +7); System.out.println ("Time Comparison:" +Calendarnew.compareto (calendar)); //return 2 hours, same time, display 0Calendarnew.add (Calendar.hour, 2); System.out.println ("Time Comparison:" +Calendarnew.compareto (calendar));
Date,calendar,simpledateformat Time Operation Finishing