Java processing date time add minus

Source: Internet
Author: User
Tags dateformat

Java Processing Date- time add-minus Java processing date time common methods:1The . Java.util.CalendarCalendar class is an abstract class that provides methods for converting a particular moment to a set of calendar fields such as year, MONTH, Day_of_month, HOUR, and so on. and provides some methods for manipulating calendar fields, such as getting the date of the next week. A millisecond value can be expressed instantaneously, which is the distance from the calendar element (Greenwich Mean Time1970Years1Month1Day'sxx:xx:00.000, the Gregorian calendar) offset. Example: Java code1. Calendar cal = Calendar.getinstance ();//get a calendar using the default time zone and locale. 2. Cal.add (Calendar.day_of_month,-1);//the day before the current date is taken. 3. 4. Cal.add (Calendar.day_of_month, +1);//the day after which the current date is taken. 5. 6.//format output date by7. Java.text.SimpleDateFormat format =NewJava.text.SimpleDateFormat ("YYYY-MM-DD"); 8. 9. System. out. println ("Today is:"+Format.format (Calendar.getinstance (). GetTime ())); Ten.  One. System. out. println ("Yesterday is:"+Format.format (Cal.gettime ())); Get- A-25th Issue: Java code1. Calendar Calendar =NewGregorianCalendar ( -, One, -,0,0,0); 2. Date Date =Calendar.gettime (); 3. System. out. println ("Christmas is:"+Format.format (date)); The Java month is from 0- One, the month is set to minus 1. GregorianCalendar constructor method parameters are: year, month-1, day, time, minute, second. Part of date taken: Java code1.intYear =calendar.Get(calendar.year); 2. 3.intMonth=calendar.Get(Calendar.month) +1; 4. 5.intDay =calendar.Get(Calendar.day_of_month); 6. 7.intHour =calendar.Get(Calendar.hour_of_day); 8. 9.intMinute =calendar.Get(Calendar.minute); Ten.  One.intSeconds =calendar.Get(Calendar.second); Take the month to add 1. Determine the maximum number of days in the current month: Java code1. Calendar cal =calendar.getinstance (); 2.intday=Cal.getactualmaximum (Calendar.day_of_month); 3. System. out. println (day);2. Java.util.DateJava Code1. Java.util.Date today=Newjava.util.Date (); 2. System. out. println ("Today is"+Formats.format (today)); Take the first day of the month: Java code1. Java.text.SimpleDateFormat format =NewJava.text.SimpleDateFormat ("yyyy-mm-01"); 2. Java.util.Date firstday=Newjava.util.Date (); 3. System. out. println ("The month first day is"+Formats.format (FirstDay)); Take the last day of the month: Java code1. 2. Calendar cal =calendar.getinstance (); 3.intmaxday=Cals.getactualmaximum (Calendar.day_of_month); 4. Java.text.Format formatter3=NewJava.text.SimpleDateFormat ("yyyy-mm-"+maxday); 5. System. out. println (Formatter3.format (Cal.gettime ())); Find the number of days between two dates: Java code1. Java.text.SimpleDateFormat format =NewJava.text.SimpleDateFormat ("YYYY-MM-DD"); 2. Java.util.Date begindate= Format.parse ("2007-12-24"); 3. Java.util.Date enddate= Format.parse ("2007-12-25"); 4.LongDay= (Date.gettime ()-mydate.gettime ())/( -* -* -* +); 5. System. out. println ("number of days apart ="+Day ); A year ago Date: Java code1. Java.text.Format formatter=NewJava.text.SimpleDateFormat ("YYYY-MM-DD"); 2. Java.util.Date todaydate=Newjava.util.Date (); 3.LongBeforetime= (Todaydate.gettime ()/ +)- -* -* -*365; 4. Todaydate.settime (beforetime* +); 5. String beforedate=Formatter.format (todaydate); 6. System. out. println (beforedate); date after one year: Java code1. Java.text.Format formatter=NewJava.text.SimpleDateFormat ("YYYY-MM-DD"); 2. Java.util.Date todaydate=Newjava.util.Date (); 3.LongAftertime= (Todaydate.gettime ()/ +)+ -* -* -*365; 4. Todaydate.settime (aftertime* +); 5. String afterdate=Formatter.format (todaydate); 6. System. out. println (afterdate); 10 hours after the Java code1. Java.util.Calendar cal=java.util.Calendar.getInstance (); 2.   Cal.settime (Dateoper); 3. Cal.add (Java.util.Calendar.HOUR_OF_DAY,Ten); 4. System. out. println ("Date:"+Forma.format (Cal.gettime ())); 10 hours before the time Java code1. Java.util.Calendar cal=java.util.Calendar.getInstance (); 2.   Cal.settime (Dateoper); 3. Cal.add (java.util.calendar.hour_of_day,-Ten); 4. System. out. println ("Date:"+Forma.format (Cal.gettime ())); 3. Java.sql.Date inherits from Java.util.Date, is the date type Java code used to manipulate the database1. Java.sql.Date sqldate =NewJava.sql.Date (Java.sql.Date.valueOf ("2007-12-25"). GetTime ()); Date Comparison: A simple comparison can be directly compared as a string, or you can use Java.sql.Date.valueOf ("2007-03-08"). CompareTo (Java.sql.Date.valueOf ("2007-03-18") method to compare the size of the date. You can also use Java.util.Date.after (java.util.Date) to compare. Time difference:LongDifference=c2.gettimeinmillis ()-C1.gettimeinmillis (); Days of difference:Longday=difference/(3600* -* +) Difference Hour:Longhour=difference/(3600* +) Difference minutes:Longminute=difference/( -* +) Difference seconds:Longsecond=difference/ +Supplemental: Java code1. DateFormat df=NewSimpleDateFormat ("yyyy-mm-dd EE hh:mm:ss"); 2. System. out. println (Df.format (NewDate ())); 3. Date Date =NewDate (); 4. DateFormat shortdate=dateformat.getdatetimeinstance (Dateformat.short, Dateformat.short); 5. DateFormat mediumdate =dateformat.getdatetimeinstance (Dateformat.medium, Dateformat.medium); 6. DateFormat longdate =dateformat.getdatetimeinstance (Dateformat.long, Dateformat.long); 7. DateFormat fulldate =dateformat.getdatetimeinstance (Dateformat.full, dateformat.full); 8. 9. System. out. println (Shortdate.format (date)); Ten. System. out. println (Mediumdate.format (date));  One. System. out. println (Longdate.format (date));  A. System. out. println (Fulldate.format (date));  -.  -. ,-4- thePM 3: -    the. --4- the  the: -: to    -. April 15, 2008 03:24 P.M. 31 seconds -. April 15, 2008 Tuesday 03:24 P.M. 31 sec CST -.  +.  -. Calendar C =calendar.getinstance ();  +.  A. C.add (Calendar.month,1);//The current time plus 1 months at. System. out. println (Df.format (C.gettime ()));  -.  -. C.add (Calendar.hour,3);//The current time plus 3 hours -. System. out. println (Df.format (C.gettime ()));  -.  -. C.add (Calendar.year,-2);//It's now minus 2 years . in. System. out. println (Df.format (C.gettime ()));  -.  to. C.add (Calendar.day_of_week,7);//the current time plus 7 days +. System. out. println (Df.format (C.gettime ()));

Java Processing Date-time add-subtract

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.